我不想禁用或使textBox只读.因为它会以灰色填充textBox.
我只想说,如果用户将尝试在textBox中键入任何内容,则不会发生任何事情.
所以我试过这个:
textBox1.Enabled = false;
Run Code Online (Sandbox Code Playgroud)
但我想让用户不能在里面键入任何内容而不是锁定或使其只读属性.
我试过这个例子:
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
base.OnKeyPress(e);
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
它适用于字符/字符串,但我仍然可以输入数字(数字).我怎样才能避免用户也阻止输入数字?
尝试:
textBox1.ReadOnly = true;
Run Code Online (Sandbox Code Playgroud)
或者你也可以试试
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)