[英]How to increase a ProgressBar Value according to the content of TextBoxes?
所以我有 4 個文本框,並且我已經將 ProgressBar 的最大值設置為4
。
ProgressAttr.Maximum = 4;
我想做的是每次填寫文本框時將我的 ProgressBar 值增加1
。
我的代碼現在看起來像這樣:
if (!string.IsNullOrEmpty(Name_txtBox.Text))
{
ProgressAttr.Value += 1;
}
if (!string.IsNullOrEmpty(Serial_TxtBox.Text))
{
ProgressAttr.Value += 1;
}
if (!string.IsNullOrEmpty(Cap_TxtBox.Text))
{
ProgressAttr.Value += 1;
}
if (!string.IsNullOrEmpty(IDprk_TxtBox.Text))
{
ProgressAttr.Value += 1;
}
這不會增加我的 ProgressBar 的值。
我也試過這個:
if (textbox.Text.Length > 0)
{
ProgressAttr.Value += 1;
}
這些都不適合我,我已經嘗試了幾個小時尋找解決方案。 我非常感謝您的幫助,並期待看到你們提出的解決方案!
我建議您使用 DataBindings 將 TextBox 的內容與 ProgressBar 的Value
屬性同步的方法。
class object 可以通知與其實現INotifyPropertyChanged接口的屬性值相關的更改。 引發其公共PropertyChanged
事件以通知綁定控件數據提供者的屬性已更改。
然后將所有綁定的屬性更新為新值。
這使您可以將所有邏輯放在一個地方,並且對用戶界面(此處為您的表單)的更改不會以任何方式影響數據綁定。
您可以在 UI 中添加或刪除控件。 綁定過程不會更改或需要跟蹤 UI 中發生的更改。
例如,將ProgressBar.Value
屬性綁定到ProgressBarController.Value
屬性。 您使用要包含的 TextBox(或 RichTextBox)控件的實例初始化 ProgressBarController,添加 Binding 以鏈接屬性,僅此而已。 所有 rest 都會自動發生。
ProgressBarController pbarController = null;
// Form Constuctor
public SomeForm()
{
InitializeComponent();
// [...]
// These TextBoxes could be child of a Container (e.g., a Panel), so you could
// also get all the child Controls of this Container to build the array
var textBoxes = new[]{ Name_txtBox, Serial_TxtBox, Cap_TxtBox, IDprk_TxtBox}
ProgressAttr.Maximum = textBoxes.Length;
pbarController = new ProgressBarController(textBoxes);
ProgressAttr.DataBindings.Add("Value", pbarController, "Value", false,
DataSourceUpdateMode.OnPropertyChanged);
}
protected override void OnFormClosed(FormClosedEventArgs e)
{
pbarController.Dispose();
base.OnFormClosed(e);
}
在這里,兩個 TextBox 在加載表單時已經包含一些文本,因此 ProgressBar 顯示了一個進度。 如果您刪除設計器中的所有文本,當然顯示的進度最初是0
:
ProgressBarController
class 使用其構造函數中傳遞的控件數組進行初始化。
► 然后它構建一個Dictionary<TextBoxBase, int>
來跟蹤與 Control 關聯的進度值:如果 Text 為空,則為0
,否則為1
。
TextBoxBase所以你也可以使用 RichTextBox 控件。
► 這些控件的TextChanged
事件使用單個處理程序訂閱。 sender
object 將是引發事件的控件。
► 如果/當關聯值已更改(控制文本 state 確定更改)時,將引發PropertyChanged
事件並且 DataBinding 通知 ProgressBar 更新其Value
屬性。
► 當父窗體關閉時,調用此 class 的Dispose()
方法以刪除對TextChanged
事件的訂閱。
using System.Runtime.CompilerServices;
private class ProgressBarController : INotifyPropertyChanged, IDisposable
{
public event PropertyChangedEventHandler PropertyChanged;
private Dictionary<TextBoxBase, int> states;
private int m_Value = 0;
public ProgressBarController(params TextBoxBase[] tboxes) {
states = new Dictionary<TextBoxBase, int>();
for (int i = 0; i < tboxes.Length; i++) {
states.Add(tboxes[i], tboxes[i].Text.Length > 0 ? 1 : 0);
tboxes[i].TextChanged += TextChanged;
}
m_Value = states.Values.Sum();
}
public int Value {
get => m_Value;
private set {
if (value != m_Value) {
m_Value = value;
OnPropertyChanged();
}
}
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
protected void TextChanged(object sender, EventArgs e)
{
var tbx = sender as TextBoxBase;
int state = tbx.Text.Length > 0 ? 1 : 0;
if (states[tbx] != state) {
states[tbx] = state;
Value = states.Values.Sum();
}
}
public void Dispose() {
foreach (var tb in states.Keys) {
tb.TextChanged -= this.TextChanged;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.