C#控制TextBox输入的源代码:
public
static
void
KeyPressUFloat(
object
sender, KeyPressEventArgs e,
int
decCount)
...
{
TextBox txtBox = (TextBox)sender;
int curPos = txtBox.SelectionStart; // 当前光标位置
int pointPos = txtBox.Text.LastIndexOf('.'); // 小数点位置
// 输入位置在两位小数点后,不允许输入
if ((pointPos > 0) && (curPos > (pointPos + decCount)))
...{
if (e.KeyChar != '')
...{
e.Handled = true;
}
return;
}
try
...{
string strInt = txtBox.Text.Trim().Substring(0, curPos);
string strDec = "";
if (pointPos < 0)

这篇博客分享了如何使用C#实现对TextBox输入的控制,提供了详细的源代码,适用于限制用户输入特定格式的数据。代码由C_B_Lu编写,并在2007年9月4日的编程论坛首发。
最低0.47元/天 解锁文章
7535





