如何在文本框中包含一些数据时禁用文本框,而在c#中没有数据时则不禁用 [英] how to disable a textbox when it has some data and not disable if it has no data in c#
问题描述
嘿,任何人都可以告诉我,我正在搜索一条记录并将其显示在文本框中.一些文本框已填充,但某些文本框未填充,因为它们在数据库中不支持这些列的数据.我的问题是我想禁用具有数据的文本框,并保持启用没有日期进行进一步更新的那些文本框.
请事先帮助
hey can any body tell me that i am searching a record and showing them in textbox. Some textboxes Are filled but some not because their is no data avilable in the datbase to those column. My problem is that i want to disable the textboxes which has data and keep enable those textboxes which have no date for further update.
pls help thanks in advance
推荐答案
不是textbox.Enabled = false
吗?我在这里缺少什么吗?
Doesn''t textbox.Enabled = false
do it? Am I missing something here?
textBox.Enable = (string.IsNullOrEmpty(textBox.Text));
其他答案还不够.您应该以某种方式触发对内容的检查.唯一的方法是处理事件
The other answers are not enough. You should trigger the check of the content somehow. The only way to do it is handling the event
myTextBox.TextChanged += delegate(object sender, EventArgs e) {
TextBox textBox = (TextBox)sender;
textBox.Enabled = string.IsNullOrEmpty(textBox.Text);
};
现在,让我告诉您:这是一个陷阱.删除文本后,将无法再编辑文本框.你不怕:-)?
我不确定您是否要以这种方式触发这些操作,至少我是给您这个主意的.毕竟,您可以使用textBox.Enabled = true
使用其他控件重新启用文本框.我希望(从您的问题中可以看到),一些更新来自数据库.在这种情况下,获取非空数据可以根据需要重新启用文本框.
Now, let me tell you: this is a trap. As soon as you delete the text, your text box cannot be edited anymore. Are you not afraid :-)?
I''m not sure you want to trigger this stuff in this way, at least I gave you the idea. After all, you can re-enable the text box using some other control with textBox.Enabled = true
. I hope (as much as I can see from your Question), some updates are coming from the database; in this case getting non-empty data can re-enable the text box as you need.
这篇关于如何在文本框中包含一些数据时禁用文本框,而在c#中没有数据时则不禁用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!