| | |
C#: Passing data from one windows form to another
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 36
Reputation:
Solved Threads: 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:
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)
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;
•
•
Join Date: Apr 2008
Posts: 70
Reputation:
Solved Threads: 18
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.
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:
Of course, you would want to check (with exceptions) to ensure it found the file, etc.
C# Syntax (Toggle Plain Text)
Form2 myForm = new Form2(); myForm.setFilename = sFile; // or, myForm.setFilename(sFile); myForm.Show();
C# Syntax (Toggle Plain Text)
// In Form2 public void setFilename(string sFile) { MyPicBox.Image = Image.FromFile(sFile); }
-Mike
•
•
Join Date: Oct 2008
Posts: 36
Reputation:
Solved Threads: 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.
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)
Form2 myForm = new Form2(); myForm.setFilename = sFile; // or, myForm.setFilename(sFile); myForm.Show();
Of course, you would want to check (with exceptions) to ensure it found the file, etc.C# Syntax (Toggle Plain Text)
// In Form2 public void setFilename(string sFile) { MyPicBox.Image = Image.FromFile(sFile); }
Oh cool thanks!!!
![]() |
Similar Threads
- pass tableadapter as parameter to module (VB.NET)
- How to launch a Crystal 11 report from VB6 (Visual Basic 4 / 5 / 6)
- Send data on a serial port (C++)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- C# ODBC Data Sources Passing Variables (C#)
- About how we shown a value in data grid (VB.NET)
- Simple ASP.Net Login Page (Using VB.Net) (ASP.NET)
- Error message when passing property changes between forms (VB.NET)
- Microsoft .NET FAQ (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: how to avoid code repetition here?
- Next Thread: link to code file
| Thread Tools | Search this Thread |
.net 7 access ado.net algorithm app application array barchart bitmap box broadcast buttons c# cast check checkbox client color combo combobox control conversion csharp custom database datagrid datagridview dataset datatable datetime degrees development draganddrop drawing encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener localization mandelbrot math microsoftc#visualexpress mouseclick mysql operator path photoshop picturebox pixelinversion post problem programming radians regex remote remoting richtextbox saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer update usercontrol validation visualstudio webbrowser windows winforms wpf xml





