Winform:在 TextBox 中切换控件在 TextChanged 上的可见性的简洁方法
Winform: Clean way to toggle visibility of a control on TextChanged in TextBox
我有一个包含许多 TextBox
和一条消息的 winform,我希望消息在任何 TextBox
更改时消失。有没有一种干净的方法可以在不向所有 TextBox
添加 TextChanged
EventHander 的情况下实现这一目标?
我乱七八糟的做法如下:
public static DialogResult ShowDialog()
{
var inputBox = new Form { ClientSize = new Size(520, 225), FormBorderStyle = FormBorderStyle.FixedDialog };
var input1 = new TextBox { Location = new Point(25, 25)};
var input2 = new TextBox { Location = new Point(25, 60) };
// Many more text boxes...
var label = new Label { Text = "Text", Location = new Point(25, 90), Visible = true };
input1.TextChanged += new EventHandler((sender, e) => label.Visible = false);
input2.TextChanged += new EventHandler((sender, e) => label.Visible = false);
// Add handler for all TextBoxes...
inputBox.Controls.Add(input1);
inputBox.Controls.Add(input2);
inputBox.Controls.Add(label);
return inputBox.ShowDialog();
}
你可以尝试写一个函数来创建TextBox
.
让TextBox
函数中的初始设置和事件绑定代码。
private static TextBox CreateTextBox(int xPos,int yPos,Label label){
var input1 = new TextBox { Location = new Point(xPos, yPos)};
input1.TextChanged += new EventHandler((sender, e) => label.Visible = false);
return input1;
}
你只需要调用inputBox.Controls.Add
方法中的函数,并传递你需要的参数即可。
public static DialogResult ShowDialog()
{
var inputBox = new Form { ClientSize = new Size(520, 225), FormBorderStyle = FormBorderStyle.FixedDialog };
var label = new Label { Text = "Text", Location = new Point(25, 90), Visible = true };
inputBox.Controls.Add(CreateTextBox(25, 25,label));
inputBox.Controls.Add(CreateTextBox(25, 60,label));
inputBox.Controls.Add(label);
return inputBox.ShowDialog();
}
注意
如果参数太多,你可以尝试使用class来传递这些参数。
我有一个包含许多 TextBox
和一条消息的 winform,我希望消息在任何 TextBox
更改时消失。有没有一种干净的方法可以在不向所有 TextBox
添加 TextChanged
EventHander 的情况下实现这一目标?
我乱七八糟的做法如下:
public static DialogResult ShowDialog()
{
var inputBox = new Form { ClientSize = new Size(520, 225), FormBorderStyle = FormBorderStyle.FixedDialog };
var input1 = new TextBox { Location = new Point(25, 25)};
var input2 = new TextBox { Location = new Point(25, 60) };
// Many more text boxes...
var label = new Label { Text = "Text", Location = new Point(25, 90), Visible = true };
input1.TextChanged += new EventHandler((sender, e) => label.Visible = false);
input2.TextChanged += new EventHandler((sender, e) => label.Visible = false);
// Add handler for all TextBoxes...
inputBox.Controls.Add(input1);
inputBox.Controls.Add(input2);
inputBox.Controls.Add(label);
return inputBox.ShowDialog();
}
你可以尝试写一个函数来创建TextBox
.
让TextBox
函数中的初始设置和事件绑定代码。
private static TextBox CreateTextBox(int xPos,int yPos,Label label){
var input1 = new TextBox { Location = new Point(xPos, yPos)};
input1.TextChanged += new EventHandler((sender, e) => label.Visible = false);
return input1;
}
你只需要调用inputBox.Controls.Add
方法中的函数,并传递你需要的参数即可。
public static DialogResult ShowDialog()
{
var inputBox = new Form { ClientSize = new Size(520, 225), FormBorderStyle = FormBorderStyle.FixedDialog };
var label = new Label { Text = "Text", Location = new Point(25, 90), Visible = true };
inputBox.Controls.Add(CreateTextBox(25, 25,label));
inputBox.Controls.Add(CreateTextBox(25, 60,label));
inputBox.Controls.Add(label);
return inputBox.ShowDialog();
}
注意
如果参数太多,你可以尝试使用class来传递这些参数。