Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi i wanted to display the file name inside a textbox after the form is closed and then opened.I have an application with 2 forms.I can open Form 2 from form 1 and close it also .In Form 2 i have a textbox where the file name appears after i browse a file and select it .But when i close the file and open it the textbox is empty.I tried to use a public string variable within the form2 class and use that string while i load the form but it shows empty textbox .How could i retain the file-name till the application is closed .
Posted
Comments
lukeer 21-May-14 5:53am    
Improve your question to show the relevant code part (remember to enclose it it such tags:
<pre lang="c#">YourCodeHere();</pre>
With Form2's code, I expect us to find why the public string (better use a property) didn't work.

Use static classes instead for data shared between forms. Let the above code be the same. Add a new class using "Project->Add Class..". Then make the class static by adding static before class. Then add the FileName variable which should be both public and static.

Let the class name be "Shared". The class should look like the following:

C#
public static class Shared
{
     public static string FileName;
}


Then in the constructor of Form2 change
C#
this.textBox1.Text = Filename1;

to
C#
this.textBox1.Text = Shared.FileName;


And then in the method "button1_Click" change:
C#
Filename1 = openFileDialog1.FileName; 

to
C#
Shared.FileName = openFileDialog1.FileName; 


Delete these lines as they are no longer needed:
C#
private string Filename;
public string Filename1
{
get { return Filename; }
set { Filename = value; }
}
 
Share this answer
 
Comments
Member 10408516 22-May-14 1:52am    
Thank-you Manikandan it is working the way I want
There are plenty of ways through which you can share data between forms. You can make use of constructors, properties and event handlers.

Here[^] is some sample code to get you started.
 
Share this answer
 
Comments
Member 10408516 21-May-14 7:51am    
private string Filename;
public string Filename1
{
get { return Filename; }
set { Filename = value; }
}
public Form2()
{
InitializeComponent();
this.textBox1.Text = Filename1;

}
private void button1_Click(object sender, EventArgs e)
{
result = openFileDialog1.ShowDialog();
openFileDialog1.AddExtension = true;
openFileDialog1.Title = "Open peripheral programming sequence file";
openFileDialog1.Filter = "Sequence XML File (*.xml)|*.xml";

openFileDialog1.RestoreDirectory = true;

if (result == DialogResult.OK)
{
this.textBox1.Text = openFileDialog1.FileName;
Filename1 = openFileDialog1.FileName;
}
}



Thankyou.I tried using the property method as shown above but not sure if i have done it in the correct way but the filename was not shown in the textbox1 when the form2 was opened .Is there any method where the string is not passed through the constructor but it is stored in a string variable that can be accessed until the application is stopped i.e even when the form 2 is closed we should be able to access that string variable .
dan!sh 21-May-14 8:17am    
See you are creating the string variable in form which will not exist once form is closed. You can refer to this article for better details: http://www.codeproject.com/Articles/325000/Sharing-data-among-Windows-Forms

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900