简体   繁体   中英

What's the best way to sync variables with UI controls?

I'm working on a C# desktop application with lots of variables in the code that are visible and controlled within the UI. The values of these variables are also saved in application settings, and often modified throughout the application's logic.

My question is, what would be the best practice to keeping values within UI controls in sync with the values of these variables regardless of where or how these variables are modified? To be more specific, I'd like the UI controls to reflect the values of the variables at all times, without having to modify the values inside those UI controls manually. I'm referring to textboxes, checkboxes, dropdownlist selections, etc...

Update: I'm using WinForms

For me, I'll change the variables' value in the changed event of the control.

  • On the TextChanged event of a textbox
  • On the SelectionChanged of Combobox.
  • On the CheckChanged of Checkbox or Radio Button

Example:

string str;

public void textbox1_TextChanged(object sender, EventArgs e)
{
   str = textbox1.Text;
}

The moment something is changed/modified on any of its control, then it updates the variable value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM