Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an application with some textboxes. My user fills the textboxes and runs some methods, when they close the application data is lost (normally).

I want to keep the value of a couple of textboxes and some local variables. It's not worth it to use database, and simple .txt files are not clean enough, is there any other simple and brief way of storing little volumes of data between application runs?

I'm not sure but have heard some wisps about resource files, are they good for this case?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
443 views
Welcome To Ask or Share your Answers For Others

1 Answer

Simplest way is binding your textboxes to application settings:

  • select texbox you want to preserve
  • go to Properties > Data > (ApplicationSettings)
  • add application settings binding to Text property
  • on FormClosed event save application settings

Saving settings:

private void Form_FormClosed(object sender, FormClosedEventArgs e)
{
    Settings.Default.Save();
}

Next time when user will start your application, settings will be loaded from user-specific file, and textboxes will be filled with same data as it was before user closed an application last time.

Also in application settings you can store local variables, but you will have to add settings for them manually, and manually read that setting on application start:

  • open Properties folder under project > Settings.settings
  • add settings you want to store (e.g. MyCounter)
  • set MyCounter type, scope, and default value (e.g. int, User, 0)
  • read setting to your local variable var x = Settings.Default.MyCounter
  • on form closed save setting Settings.Default.MyCounter = x just before calling Settings.Default.Save()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...