C#: Passing data from one windows form to another

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 36
Reputation: amerninja2 has a little shameless behaviour in the past 
Solved Threads: 0
amerninja2 amerninja2 is offline Offline
Light Poster

C#: Passing data from one windows form to another

 
0
  #1
Mar 10th, 2009
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:

  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;
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 70
Reputation: mcriscolo is an unknown quantity at this point 
Solved Threads: 18
mcriscolo mcriscolo is offline Offline
Junior Poster in Training

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

 
0
  #2
Mar 11th, 2009
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.
  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:
  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.
-Mike
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 36
Reputation: amerninja2 has a little shameless behaviour in the past 
Solved Threads: 0
amerninja2 amerninja2 is offline Offline
Light Poster

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

 
0
  #3
Mar 11th, 2009
Originally Posted by mcriscolo View Post
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.
  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:
  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!!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC