可以使用以下代码实现:

  1. 在控件的KeyPress事件中,限制输入只能为数字和小数点:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != (char)Keys.Back)
    {
        e.Handled = true;
    }
}
  1. 在控件的TextChanged事件中,将小写字母转换成大写字母并显示:
private void textBox1_TextChanged(object sender, EventArgs e)
{
    textBox1.Text = textBox1.Text.ToUpper();
}

完整代码:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!char.IsDigit(e.KeyChar) && e.KeyChar != '.' && e.KeyChar != (char)Keys.Back)
    {
        e.Handled = true;
    }
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    textBox1.Text = textBox1.Text.ToUpper();
}
C# winform 控件只能输入COM+数字允许输入小写转换成大写显示

原文地址: https://www.cveoy.top/t/topic/b78u 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录