Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The values entered on the win forms are deleting once i close the form and reopen it.
how i can the save the entered values

What I have tried:

private button1_click(object sender,EventArgs e)
{
Form2 F2 = new Form2();
F2.show();
}
Posted
Updated 29-Jul-16 2:39am
Comments
NagaNimesh 11474558 29-Jul-16 5:52am    
please put your code
NagaNimesh 11474558 29-Jul-16 5:56am    
generally what ever the values you assignedto textbox r listbox in form load will never disappear even form closed.
BillWoodruff 29-Jul-16 7:48am    
"generally what ever the values you assignedto textbox r listbox in form load will never disappear even form closed."

Absolutely wrong ! You close a Form, it is 'Disposed, all internal state is garbage-collected.
NagaNimesh 11474558 29-Jul-16 5:57am    
because again while it reopen , will reload with form load data
BillWoodruff 29-Jul-16 7:48am    
I suggest you study a little further before posting any more incorrect advice, like this.

Form data isn't preserved unless you specifically save it somewhere, and restore it when the form is re-displayed, just as it isn't when you create a FileOpen dialog: the filename you used last time isn't displayed, but the folder remains the same as last time. That's because the file name isn't preserved, but the folder is.

If you want your forms to show existing data, then you have to pass it the data (or a token which lets it know which data to use) each time you create the form instance. This can be done via the Form Constructor, or via properties in the form.
C#
private string userName = "OriginalGriff";
...
MyForm mf = new MyForm();
mf.UserName = userName;
if (mf.Show() == DialogResult.OK)
   {
   userName = mf.UserName;
   }

Exactly where you store such info is up to you - the example above just holds it at class level in the Form that displays the new form - it depends on what is available to you and how persistent you want the data to be!
 
Share this answer
 
v2
Comments
Member 12659926 29-Jul-16 6:49am    
please can u explain in detail how to do that for combo box,listbox and text box.
In form 1 I have did this
private void bCRTToolStripMenuItem_Click(object sender, EventArgs e)
{
Form4 F4 = new Form4();
F4.Show();
}

on pressing bCRTToolStripMenuItem button Form 4 will be displayed.
in form4 I have a combo box and text box and listbox.
on a click of button I will add value to combo box.(before that while creating form 4 .I have added 4 default values to combo box using the collections properties) .after adding the new value for combobox I will press ok button.the ok button code is this.close();but when I reopen the form on clicking the button on form1.the newly added values doesnot reflect in the combobox list.so I need how to save that values.
here is add new item to combo box button code.
private void button1_Click(object sender, EventArgs e)
{


i = i + 1;

comboBox1.SelectedIndex = comboBox1.Items.Add("Message" + i);
F5.listBox1.Items.Add("Message" + i);

F5.Show();

if (comboBox2.SelectedIndex == 0)
{
comboBox7.SelectedIndex = 1;
comboBox8.SelectedIndex = 0;
comboBox9.SelectedIndex = 0;
radioButton4.Enabled = false;
textBox3.Enabled = false;
}

}
as I am new to c# and visual studio ,kindly help me to come out of it
Thanks in advance
OriginalGriff 29-Jul-16 7:05am    
No - it's all the same principle as I showed in the example I gave - it doesn't matter what the source is.
Give it a try, and see how far you get with a little thinking!
Ralf Meier 29-Jul-16 7:14am    
I agree with OG.
You should realize how computing generell work.
If you have a Form with controls on it this is only the User-Interface - that means : it gives the User the ability to see or enter data. The data, which should be displayed comes from somewhere (perhaps a file). If you enter data and want that it is memorized it must be stored somewhere (perhaps also in a file - or a database - or a place in the Computers Memory) - that depends on you and your programming.
Member 12659926 29-Jul-16 7:25am    
Yes ralf I agree with u,this is the UI only,i want the user entered data to be stored and restored while opening the form.how to do that is confusing me,if u have any example share with me
OriginalGriff 29-Jul-16 7:27am    
You have an example!
All you have to do is think about it and extend that to work with your specific circumstances.
If we just hand it to you on a plate you don't learn anything, so you can't do it next time!
This really isn't difficult - look at what I wrote and give it a try.
In your code example, you create an instance of a Form in a Button Click EventHandler. That "instance" (object) will cease to exist once the EventHandler code is exited; you will have no reference to it.

Assuming you want the user to enter some information on a Form, and then you want to get that information from another Form, there are several strategies, and OriginalGriff has an excellent series of three articles here on some of those strategies: [^].

An example of a strategy ... which I hope will complement the ones outlined by OriginalGriff follows:

1. in the Main Form:
C#
private Form2 f2;

private void Form1_Load(object sender, EventArgs e)
{
    f2 = new Form2();

    // subscribe to the Action/Event on Form2
    f2.SendDataAction += SendDataAction;
}

private void btnShowForm2_Click(object sender, EventArgs e)
{  
    f2.Show();     
}

// Action/Event to "broadcast" data
private void SendDataAction(string tboxtxt, int cboxitmndx, string cbxitmtxt)
{
    // handle the incoming data here
}
2. In the second Form ... assume there's a TextBox 'textBox1, a Button 'btnSendData, and a ComboBox 'comboBox1 :
C#
public Action<string,> SendDataAction {set; get; }

private void btnSendData_Click(object sender, EventArgs e)
{
    if (SendDataAction != null)
    {
        SendDataAction(textBox1.Text, comboBox1.SelectedIndex, comboBox1.SelectedText);
    }
}
What happens here:

1. in Form2 a Delegate (Action) that takes three parameters, a string, an int, and a string, is defined as a Public Property.

1.a. when the 'btnSendData is clicked, if the Action is not null (has subscribers), the Action is invoked, passing the values from the TextBox and ComboBox Controls.

2. in Form1, you'll note that after creating the instance of Form2 'f2, then we can subscribe to the Action/Event on Form2 by passing a reference to the method we create 'SendDataAction ... by using the += operator, we add a method to its InvocationList.

2.a. so, the 'SendDataAction method we defined in Form1 will get invoked by the Button click in Form2, and will receive the data.

Keep in mind that "one size does not fit all:" in other circumstances you may wish to retrieve the data in the second Form "on demand" when some event occurs in the first (Main) form. That calls for another strategy. You may wish to force the second Form to notify the first Form when the user closes it, passing whatever data has been entered.

One reason for "formalizing" transfer of data from one context (Form) to another ... as shown here ... is to achieve "encapsulation," to avoid any possibility that anything that happens on the second Form can somehow "screw up" the first Form, and, the reverse.

The second Form "has no idea" what/how-many subscribers there are to the 'SendDataAction Action/Event: it's just going to "broadcast" to all of them.

Note: 'Action and 'Func are alternative ways of writing (syntax for) a Delegate added to .NET with .NET 3.5. As with all Delegates, they are multi-cast (support multiple subscribers).
 
Share this answer
 
v2

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