如何在TextChange上将Textbox.Enabled从false设置为true? [英] How to set Textbox.Enabled from false to true on TextChange?
问题描述
我通过两个文本框
以编程方式创建一个表单
。我的目标是禁用一个文本框
,如果我在第二个中输入一些东西,并且相反。我设法在第一个 textbox
textchange上禁用第二个文本框
,但是无法弄清楚如何在第一个 textbox
.Text为空。
I programmatically create a Form
with two textboxes
. My goal is to disable one textbox
if I type something in the second one and contrariwise. I managed to disable second textbox
on first textbox
textchange,but can't figure out how enable it when the first textbox
.Text is empty.
以下是代码:
private void metaName_TextChanged(object sender,EventArgs e)
{
var ctrl = (Control)sender;
var frm = ctrl.FindForm();
TextBox metaTxt = null;
foreach (var ctr in frm.Controls)
{
if (ctr is TextBox)
{
metaTxt = (TextBox)ctr;
if (metaTxt.Name == "metaHTTPEquiv")
{
metaTxt.Enabled = false;
}
else
if (?)
{
}
}
}
}
我想要这样做:
if(textBox3.Text == String.Empty)
{
textBox4.Enabled = true;
}
else
if(textBox3.Text != String.Empty)
{
textBox4.Enabled = false;
}
推荐答案
首先设置一个标志来启用或者基于引发事件的metaName文本框的内容禁用第二个控件,然后使用一点Linq搜索第二个文本框。
First set a flag to enable or disable the second control based on the content of the metaName textbox that raises the event, then search for the second textbox using a bit of Linq.
private void metaName_TextChanged(object sender,EventArgs e)
{
TextBox ctrl = sender as TextBox;
if(ctrl != null)
{
bool enable = !string.IsNullOrEmpty(ctrl.Text);
TextBox secondOne = this.Controls
.OfType<TextBox>()
.FirstOrDefault(x => x.Name == "metaHTTPEquiv");
if(secondOne != null)
secondOne.Enabled = enable;
}
}
相同的代码,反转文本框角色可以是用作第二个文本框的事件处理程序。
The same code, reversing the textboxes roles, could be used as the event handler of the second textbox.
这篇关于如何在TextChange上将Textbox.Enabled从false设置为true?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!