简体   繁体   English

Windows Forms C# - 动态掩码文本框钱输入

[英]Windows Forms C# - Dynamic mask textbox money Input

Windows Forms C# - I would like to make a textbox that automatically changes each time a user types or deletes one key from the textbox. Windows Forms C# - 我想创建一个文本框,每当用户从文本框中键入或删除一个键时,该文本框就会自动更改。 I developed part of code. 我开发了部分代码。

    //This will convert value from textbox to currency format when focus leave textbox
    private void txtValormetrocubico_Leave(object sender, EventArgs e)
    {
        decimal cubic = Convert.ToDecimal(txtValormetrocubico.Text);
        txtValormetrocubico.Text = string.Format("{0:c}", Convert.ToDecimal(cubic));
        MessageBox.Show(txtValormetrocubico.Text);
    }


    //this only allow numbers and "." and "," on textimbox imput
    private void txtValormetrocubico_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsControl(e.KeyChar)
    && !char.IsDigit(e.KeyChar)
    && e.KeyChar != '.' && e.KeyChar != ',')
        {
            e.Handled = true;
        }

        // only allow one decimal point
        if (e.KeyChar == '.'
            && (sender as TextBox).Text.IndexOf('.') > -1)
        {
            e.Handled = true;
        }

            if (e.KeyChar == ','
                && (sender as TextBox).Text.IndexOf(',') > -1)
            {
                e.Handled = true;
            }          
    }

The first time I enter a value in the text box, the value is converted to currency format perfectly, like 300 to $ 300.00 . 我第一次在文本框中输入值时,该值将完美地转换为货币格式,例如300$ 300.00 But I edit this textbox value again and press enter, it gives an error: "Input String was not in a Correct Format" pointing to the line below: 但我再次编辑此文本框值并按Enter键,它给出一个错误:“输入字符串不是正确的格式”指向下面的行:

decimal cubic = Convert.ToDecimal(txtValormetrocubico.Text);

I think the problem is caused by the fact that the value is already in decimal format. 我认为问题是由于该值已经是十进制格式的事实。 So when I click on the field and press enter again, it causes an error because the value cannot be parsed. 因此,当我单击该字段并再次按Enter键时,会导致错误,因为无法解析该值。 How do I avoid this error ? 如何避免此错误?

EDIT: My previous question was my first. 编辑:我之前的问题是我的第一个问题。 As I am new user and don't have much knowledge in C#, I forgot to post my code. 由于我是新用户并且对C#知之甚少,我忘了发布我的代码。 After studying some more, I made part of it work. 在研究了一些之后,我做了部分工作。 Only this little problem remains. 只剩下这个小问题。 Please vote up, I was banned and cant make new questions because I had 7 down votes. 请投票,我被禁止,不能提出新的问题因为我有7票。

Thanks guys. 多谢你们。

The problem is that the string contains the currency symbol 问题是该字符串包含货币符号

private void TextBox_LeaveEvent(object sender, EventArgs e)
{
    var tb = sender as TextBox;
    if(tb.Text.Length>0){
        decimal cubic = Convert.ToDecimal(tb.Text);
        tb.Text = string.Format("{0:c}", Convert.ToDecimal(cubic));
        label1.Text = tb.Text;
    }
}

Above the textbox.Text is set to contain the currency information: textbox.Text上方设置为包含货币信息:

tb.Text = string.Format("{0:c}", Convert.ToDecimal(cubic));

Since the textbox now contains a currency symbol (like € or $) the Convert.ToDecimal fails as soon as the TextBox_LeaveEvent fires again: 由于文本框现在包含货币符号(如€或$),因此只要TextBox_LeaveEvent再次触发, Convert.ToDecimal就会失败:

decimal cubic = Convert.ToDecimal(tb.Text);

If you bing for c# masked textbox you can find articles about masked textboxes. 如果你想要使用c#蒙面文本框,你可以找到有关蒙版文本框的文章 You cold also test if the string contains any non-number-charakters ( if(tbText.IndexOf(" ") >-1){...} ) 你冷也测试字符串是否包含任何非数字字符串( if(tbText.IndexOf(" ") >-1){...}

Update with basic example 更新基本示例

I uploaded a very basic example to remove the currency formating to github: 我上传了一个非常基本的例子来删除格式化为github 的货币

string RemoveCurrencyFormating(string input)
{    
    if(input.IndexOf(" ") !=-1){
       var money = input.Substring(0, input.IndexOf(" ")-1);            
       return String.Format("{0:D0}", money);               
    }
    return ""; // Todo: add Error Handling
}

On TextBox Enter Event you can do the following: 在TextBox上输入事件,您可以执行以下操作:

void TextBox_EnterEvent(object sender, EventArgs e)
{
    var tb = sender as TextBox;
    tb.Text = RemoveCurrencyFormating(tb.Text);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM