C#阅读文本框忽略字符 [英] C# Reading Textbox ignoring characters
问题描述
大家好,我有一个如下的按键事件
Hi everyone i have a key down event as follows
if (e.KeyCode == Keys.C && e.KeyCode == Keys.Enter || e.KeyCode == Keys.Return)
{
var amount = textBox1.Text;
var total = label3.Text;
decimal damount = Math.Round(Convert.ToDecimal(amount), 2);
decimal dtotal = Convert.ToDecimal(total);
var ntotal = dtotal - damount;
var ndue = Math.Round(Convert.ToDecimal(ntotal), 2);
var ntotal1 = Convert.ToString(ndue);
if (ndue <= 0)
{
panel4.Show();
label4.Text = ntotal1;
}
else
{
label3.Text = ntotal1;
}
textBox1.Text = "";
textBox1.Focus();
}
此事件的工作方式与我想要的一样,但这是我的问题,当我读取textbox1.text忽略字符(例如c或a)时,我该怎么办,因为键降要求c会与我的十进制转换混淆任何建议
this event works like i want it but here is my question how do i when reading the textbox1.text ignore character such as c or a , since the keydown requires c it messes with my decimal conversion any suggestions
推荐答案
要防止按键继续前进,请将e.SuppressKeyPress = true; e.Handled = true;
添加到处理程序中.
Add e.SuppressKeyPress = true; e.Handled = true;
to your handler when you want to prevent the key press from going forward.
顺便说一句,您的if语句等效于if (e.KeyCode == Keys.Return)
,因为它永远不会等于C
和Enter
.
As an aside, your if statement is equivalent to if (e.KeyCode == Keys.Return)
, since it will never be equal to both C
and Enter
.
这篇关于C#阅读文本框忽略字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!