WebbyLass
WebbyLass

Reputation: 23

Retain values in dialog box after closing - windows forms c#

I'm relatively new to C# and Windows forms so excuse me for what may seem like an easy question.

I have a windows application which has a dialog box which when opened contains textboxes with some default values. These can be changed depending on what the user wants to use. The values in this dialog box together with the content on the main form are then used to create an XML file. The issue I have is if I go to open the dialog box again to change any values in the same session, the original values are there and not any of the new values.

How do I get it to keep the values that have been changed in that particular session?

Upvotes: 2

Views: 3692

Answers (3)

Jim Mischel
Jim Mischel

Reputation: 134055

If you want to see the user's values the next time you open the dialog box, you'll need to save those values somewhere, and then re-load them the next time the dialog is displayed (usually on Form_Load or Form_Show). And of course you'll need to save the values (probably in Form_Close?) before exiting.

Where you save those values is up to you. You can save them in some static variables in the form class if you want it to be just for that run of the program. Or you can store in a configuration file, the registry, isolated storage, etc. if you want to re-load those settings the next time the program is run.

Upvotes: 1

ppetrov
ppetrov

Reputation: 3105

If you want to keep the values the user entered the last time he used your dialog, you need to keep a reference to your dialog somewhere.

Also if you set some data in your dialog on the Load event it might erase the data previously entered by the user. Without seeing your code I can't tell more at this point.

Upvotes: 0

tmwoods
tmwoods

Reputation: 2413

If I'm understanding the question correctly it sounds like you need to make use of background variables and TextChanged events (although I prefer KeyDown events and my code uses that instead). For instance, let's call your textbox TextBox1. You can then make a global variable called string Temp and use it like this:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    temp = textBox1.Text;
}

Once your dialog box is closed you can use that variable elsewhere, in your case it sounds like you want to send it to an XML. Another option is to use the keydown event to have a temporary XML file that retains the value of your text. This is obviously more computationally expensive but it's not really that big of a deal unless this is going to be used in a processor limited environment. The last thing I'd mention is that you may run into trouble if you're using multi-threading and passing the value of that temp value. Look into using variables on other threads than you started with for help with that.

Upvotes: 1

Related Questions