943,879 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 23411
  • C# RSS
Mar 10th, 2009
0

C#: Passing data from one windows form to another

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  1. string idk = this.txtPATH.Text + "\\" + this.listBox.SelectedItem.ToString();
  2. Form Form2 = new Form2();
  3. Form2.Show(); //THIS WORKS, IT OPENS THE WINDOW, BUT THIS DOESN'T:
  4. Image img = idk;
  5. Form2.pictureBox.Image = img;
Similar Threads
Reputation Points: 5
Solved Threads: 0
Light Poster
amerninja2 is offline Offline
36 posts
since Oct 2008
Mar 11th, 2009
0

Re: C#: Passing data from one windows form to another

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.
C# Syntax (Toggle Plain Text)
  1. Form2 myForm = new Form2();
  2. myForm.setFilename = sFile; // or, myForm.setFilename(sFile);
  3. 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:
C# Syntax (Toggle Plain Text)
  1. // In Form2
  2. public void setFilename(string sFile)
  3. {
  4. MyPicBox.Image = Image.FromFile(sFile);
  5. }
Of course, you would want to check (with exceptions) to ensure it found the file, etc.
Reputation Points: 57
Solved Threads: 64
Posting Whiz in Training
mcriscolo is offline Offline
218 posts
since Apr 2008
Mar 11th, 2009
0

Re: C#: Passing data from one windows form to another

Click to Expand / Collapse  Quote originally posted by mcriscolo ...
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.
C# Syntax (Toggle Plain Text)
  1. Form2 myForm = new Form2();
  2. myForm.setFilename = sFile; // or, myForm.setFilename(sFile);
  3. 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:
C# Syntax (Toggle Plain Text)
  1. // In Form2
  2. public void setFilename(string sFile)
  3. {
  4. MyPicBox.Image = Image.FromFile(sFile);
  5. }
Of course, you would want to check (with exceptions) to ensure it found the file, etc.

Oh cool thanks!!!
Reputation Points: 5
Solved Threads: 0
Light Poster
amerninja2 is offline Offline
36 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: how to avoid code repetition here?
Next Thread in C# Forum Timeline: link to code file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC