AI智能
改变未来

C#文本输入限制仅可输入浮点型

public void limitInput(TextBox tb, KeyPressEventArgs e, int Int, int Dou){int keychar = (int)e.KeyChar;int maxlen = 8;             //文本框最大长度if (tb.TextLength <= 0){if (!(keychar >= 48 && keychar <= 57))       //字符限制{e.Handled = true;}}else if (tb.TextLength <= maxlen){if (!((keychar >= 46 && keychar <= 57) || keychar == 46 || keychar == 8)){e.Handled = true;}else if (keychar == 46){if (tb.Text.LastIndexOf(\".\") > 0){e.Handled = true;}if (tb.SelectionLength == tb.TextLength || tb.SelectionStart == 0){e.Handled = true;}}else if (keychar >= 46 && keychar <= 57){if (tb.Text.LastIndexOf(\".\") == -1 && tb.TextLength > Int - 1)        //限制整数位{e.Handled = true;}if (tb.TextLength - tb.Text.LastIndexOf(\".\") > Dou && tb.Text.LastIndexOf(\".\") != -1)   //限制小数位{e.Handled = true;}if (tb.TextLength == tb.SelectionLength){e.Handled = false;}}else if (keychar == 8){if (tb.TextLength == 0)tb.Text = \"0\";}}}

缺点:如果先输小数点,再向小数点前输入整数无法限制整数位。
如有大佬有解决方案,望留言,感激不尽。

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » C#文本输入限制仅可输入浮点型