1.11M Members

C#: Passing data from one windows form to another

 
0
 

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;
 
0
 

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.

 
0
 

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!!!

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article