可以使用正则表达式限制输入,然后在文本框的 TextChanged 事件中将小写字母转换成大写字母。/n/n代码示例:/n/ncsharp/nprivate void textBox1_TextChanged(object sender, EventArgs e)/n{/n // 将小写字母转换成大写字母/n textBox1.Text = textBox1.Text.ToUpper();/n}/n/nprivate void textBox1_KeyPress(object sender, KeyPressEventArgs e)/n{/n // 只允许输入COM+数字/n if (!Regex.IsMatch(e.KeyChar.ToString(), @'^[cC][oO][mM]/d$'))/n {/n e.Handled = true;/n }/n}/n/n/n在 KeyPress 事件中使用正则表达式限制输入,只允许输入 'COM' 加数字,如果输入不符合要求,则将 Handled 属性设置为 true,表示事件已经处理完毕,不再继续传递。在 TextChanged 事件中将小写字母转换成大写字母,保证显示的内容都是大写字母。

C# WinForms 控件限制输入:只允许输入 COM+数字并自动转换为大写

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

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