如何在C#中禁用文本框 [英] How do I disable textbox in C#
问题描述
嗨!这是Atta Ur Rahman。我有两个问题。第一个是禁用文本框。在形式上我有单选按钮男和女。和一个文本框丈夫名字和丈夫NIC。当我检查男性按钮。我想要禁用丈夫姓名和丈夫的texbox。
另一个问题是这个,我想要允许texbox只有字母表。
我尝试过:
i试图在tex框中允许数字,但我不知道如何不允许数字。
Hi! This is Atta Ur Rahman. I have Two Question. First one is to disable textbox. in form i have radio Button "Male" and "Female". and a textbox " Husband Name" and Husband NIC". when I check "Male" button. I want to disable both the texboxes of husband name and husband nic.
Another question is this, i want to allow the texboxes only alphabets.
What I have tried:
i tried to allow the numeric in tex boxes but i don't know how to not allow the numerics.
private void txbxralativenic_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsDigit(e.KeyChar) && e.KeyChar != '-' && !char.IsControl(e.KeyChar))
{
e.Handled = true;
}
if (txbxralativenic.Text.Length > 14 && !char.IsControl(e.KeyChar))
{
e.Handled = true;
}
}
推荐答案
如果textBox1是丈夫姓名和radioButton1的文本框是男性选择的广播,然后
If textBox1 is the text box for husband name and radioButton1 is the radio for male option, then
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
textBox1.Enabled = !radioButton1.Checked;
}
对于下一个问题,请查看: Windows窗体中的用户输入验证 [ ^ ]
For the next question, check this out: User Input Validation in Windows Forms[^]
尝试:
Try:
MyTextBox.Enabled = false;
对于仅数字输入,我不会使用TextBox - 你可以做到,但它有点像PITA,因为你必须防止粘贴和键盘输入。
相反,我建议你使用NumericUpDown控件 - 它只允许数字,并允许您设置最大值和最小值。
For numeric only input, I wouldn't use a TextBox - you can do it, but it's a bit of a PITA because you have to prevent Pasting as well as keyboard entry.
Instead, I'd suggest you use a NumericUpDown control - it will only allow numbers, and allows you to set a maximum and minimum value as well.
这篇关于如何在C#中禁用文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!