简体   繁体   English

将变量与 UI 控件同步的最佳方法是什么?

[英]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.我正在开发一个 C# 桌面应用程序,其中代码中有很多变量,这些变量在 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?我的问题是,无论在何处或如何修改这些变量,使 UI 控件中的值与这些变量的值保持同步的最佳做法是什么? 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.更具体地说,我希望 UI 控件始终反映变量的值,而不必手动修改这些 UI 控件中的值。 I'm referring to textboxes, checkboxes, dropdownlist selections, etc...我指的是文本框、复选框、下拉列表选择等...

Update: I'm using WinForms更新:我正在使用 WinForms

For me, I'll change the variables' value in the changed event of the control.对我来说,我将在控件的更改事件中更改变量的值。

  • On the TextChanged event of a textbox关于文本框的 TextChanged 事件
  • On the SelectionChanged of Combobox.在 Combobox 的 SelectionChanged 上。
  • On the CheckChanged of Checkbox or Radio Button CheckChanged 上的 Checkbox 或 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.在其任何控件上更改/修改某些内容的那一刻,它就会更新变量值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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