十进制TextBox类需要修改 [英] Decimal TextBox Class Need Modification

查看:75
本文介绍了十进制TextBox类需要修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..所有CodeProject成员
我从Microsoft官方网站下载以下代码
此类用于格式化带有数字掩码的文本框.
但我发现了一些问题,需要对其进行修改
它不能接受减号(-)字符,并且我不能将十进制数字更改为4个十进制数字..
我试图修改它一百次,但仍然找不到
方式..
谁能告诉我正确的代码.然后我可以制作文本框
以减号格式..并修改了十进制数字

attn的thx:
希望CodeProjects更好.. ^^

这是代码,或者您可以从以下位置下载它:
http://download.microsoft.com/download/0/8/7/087e6a47-7306-4a5c-ad97-d1ffd58d712e/DecimalTextBox.msi [

Hi.. All CodeProject Members
I download this code below from Microsoft Official Site
this Class Use For formatting textbox with numeric mask.
but i found some problem and need modified it
which it''s can''t accepted Minus (-) Character and i can''t change the decimal digits into 4 decimal digits..
i have tried to modificated it hundred''s time but still not find the
way..
Could anybody show me how the right code. then i can make the textbox
in Minus Format..and modified the decimal digits

thx for attn :
hope CodeProjects Better.. ^^

here is the code or you can download it at :
http://download.microsoft.com/download/0/8/7/087e6a47-7306-4a5c-ad97-d1ffd58d712e/DecimalTextBox.msi[^]

static NumberFormatInfo nfi = NumberFormatInfo.CurrentInfo;
/// <devdoc>
///     Overridden to validate and format the text.
/// </devdoc>
public override string Text
{
    get
    {
        return base.Text;
    }
    set
    {
        if (value != base.Text)
        {
            SetFormattedText(new StringBuilder(value));
        }
    }
}
/// <devdoc>
///     Overridden to handle unsupported RETURN key.
/// </devdoc>
protected override bool IsInputKey(Keys keyData)
{
    if ((keyData & Keys.KeyCode) == Keys.Return)
    {
        return false;
    }
    return base.IsInputKey(keyData);
}

/// <devdoc>
///     Handle input filtering and text insertion.
/// </devdoc>
protected override void OnKeyPress( KeyPressEventArgs e )
{
    base.OnKeyPress( e );

    e.Handled = true;  // This disables undersired keys. ProcessCmdKey is used to enabled supported shortcuts.
    // Filter allowed input: integers or decimal separator only.
    if( !char.IsDigit( e.KeyChar ) && e.KeyChar != nfi.NumberDecimalSeparator[0] )
    {
        return;
    }
    int caretPos = this.SelectionStart;
    StringBuilder txt = new StringBuilder( this.Text );

    if (e.KeyChar == nfi.NumberDecimalSeparator[0])
    {
        // Go to decimal input location.
        this.SelectionStart = txt.Length - nfi.NumberDecimalDigits;
        return;
    }
    if( caretPos == txt.Length ) // caret at the end of the box
    {
        // input ignored.
        return;
    }
    int originalTextLength = txt.Length;
    if( originalTextLength - caretPos <= nfi.NumberDecimalDigits ) // caret at decimal location
    {
        // Replace the current location's value with the input.
        txt[caretPos] = e.KeyChar;
        this.Text = txt.ToString();
        this.SelectionStart = caretPos + 1;
    }
    else // caret at a whole number's location or at the decimal separator's location.
    {
        // If caret is at pos 0 and the number is zero, we need to move caret so it can be inserted after the 0
        // and get properly formatted; otherwise, the input would be inserted at zero (effect = multiplied by 10).
        if( caretPos == 0 && txt[0] == '0' )
        {
            caretPos++;
        }
        // Insert value.
        txt.Insert( caretPos, e.KeyChar );
        if( SetFormattedText( txt ) )
        {
            this.SelectionStart = caretPos + ( txt.Length - originalTextLength );
        }
    }
}

/// <devdoc>
/// </devdoc>
public override void ResetText()
{
    SetFormattedText(new StringBuilder("0"));
}
/// <devdoc>
///     Sets the decimal-formatted text into the control's text.
///     Returns false if an exception is thrown during text formatting due to overflow or unexpected characters in
///     the specified text.
///     Methods calling this method should check for the return value before doing further processing.
///     The input should contain interger numbers and decimal group separator characters only; it does not expect
///     the string to represent a well formatted decimal number.
/// </devdoc>
private bool SetFormattedText( StringBuilder txt )
{
    // Remove group separators to re-format the string.
    txt.Replace( nfi.NumberGroupSeparator, "" )
    try
    {
        double value = double.Parse( txt.ToString() ); // parse decimal value.
        txt.Length = 0; // clear text.
        txt.Append( value.ToString( "N", nfi ) ); // set new text.
        base.Text = txt.ToString(); // set control's text.
        return true;

    }
    catch( System.OverflowException )
    {
        // Input too big.
    }
    catch( System.FormatException )
    {
        Debug.Fail( "Input was not in a correct format." );
        // Input not formatted properly.
    }
    return false;
}


[删除了与您的问题无关的代码]


[removed the code that has nothing to do with your problem]

推荐答案

在找到我们之前,您应该仔细阅读代码,看看是否不能弄清楚它在做什么以及如何只允许数字和小数处理.

您会看到一条评论,内容为:
before coming to us, you should actually read through the code and see if you can''t figure out what it''s doing and how it''s handling only allowing numbers and decimals.

You''d have seen a comment that says:
// Filter allowed input: integers or decimal separator only.


因此,在评论之后,您将需要弄清楚如何也允许减号.

然后,要限制小数位数,您必须添加自己的代码以检查小数点后已经有多少位数.如果大于4,则只允许进一步输入.


So, after the comment, you would then need to figure out how to also allow minus signs.

Then, to limit the number of decimal digits, you would have to add your own code to check to see how many digits are already after the decimal. If there''s more than 4, you would just disallow further entry.


很容易允许减号.
也许我可以这样想
it''s easy to allow minus signs.
maybe i can figure like this
protected override void OnKeyPress( KeyPressEventArgs e )
        {
            base.OnKeyPress( e );
string negativeS = NumberFormatInfo.CurrentInfo.NegativeSign;
            e.Handled = true;  // This disables undersired keys. ProcessCmdKey is used to enabled supported shortcuts.
            // Filter allowed input: integers or decimal separator only.
            if( !char.IsDigit( e.KeyChar ) && e.KeyChar != nfi.NumberDecimalSeparator[0] && e.Keychar.equals=negativeS)
            {
                return;
            }


但是,
有问题 private bool SetFormattedText( StringBuilder txt )
如果键入减号,则会出错.
您如何尝试允许减号并尝试重新编译它....
然后你会看到我的问题.


.But there is a problem at
private bool SetFormattedText( StringBuilder txt )
it''s make an error if we type the minus sign.
how about u try to allow the minus sign and try to recompile it....
and u will see my problem..


这篇关于十进制TextBox类需要修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆