So I'm working on a program "Master Directory Control", it gets all the information about all files in a directory (name, location, size, exists, etc.) and puts them into ListBoxes. That all works fine. There's a feature where you right click on selected file in the Name ListBox, and you can view the file. Viewing text containing files works perfectly, but I'm making an Image viewer for it too, and it opens a new Window to view the picture.

I have to pass the directory and the value of the Selected Item in the "File Name ListBox". That way it can show the picture in the directory + filename (file name is selected from the listbox). So how can I pass those values to the new Window? I've searched and found solutions all over websites, but I can't find any working things...

I've found stuff like:

string idk = this.txtPATH.Text + "\\" + this.listBox.SelectedItem.ToString();
Form Form2 = new Form2();
Form2.Show(); //THIS WORKS, IT OPENS THE WINDOW, BUT THIS DOESN'T:
Image img = idk;
Form2.pictureBox.Image = img;

Recommended Answers

All 2 Replies

You can do it by creating public properties or methods on your new form (in your example, Form2). You would then show the form afterward.

Form2 myForm = new Form2();
myForm.setFilename = sFile; // or, myForm.setFilename(sFile);
myForm.Show();

In the property/method, you could also call the "FromFile" method on the Image object to load the image data from the file and set it into the PictureBox on your form:

// In Form2
public void setFilename(string sFile)
{
    MyPicBox.Image = Image.FromFile(sFile);
}

Of course, you would want to check (with exceptions) to ensure it found the file, etc.

You can do it by creating public properties or methods on your new form (in your example, Form2). You would then show the form afterward.

Form2 myForm = new Form2();
myForm.setFilename = sFile; // or, myForm.setFilename(sFile);
myForm.Show();

In the property/method, you could also call the "FromFile" method on the Image object to load the image data from the file and set it into the PictureBox on your form:

// In Form2
public void setFilename(string sFile)
{
    MyPicBox.Image = Image.FromFile(sFile);
}

Of course, you would want to check (with exceptions) to ensure it found the file, etc.

Oh cool thanks!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.