简体   繁体   中英

How can I separate digits with comma (,) in textbox in C# winform?

I want to separate digits with comma in textbox in Winforms. I wrote this function and it works:

public string setComma(double number)
{
    string x = string.Format("{0:n0}", number);
    return x;
}

private void txtPayani_TextChanged(object sender, EventArgs e)
{
    txtPayani.Text = setComma(payani);
}

but the problem is that when I start to type number in textbox, the mouse cursor comes to left of the text. But I want it will be normal and put in right of number.

To solve this problem, I used this code:

private void txtPayani_TextChanged(object sender, EventArgs e)
{
    txtPayani.Text = setComma(payani);
    txtPayani.Select(txtPayani.Text.Length, 0);
}

but when I delete one middle digit form textbox, again the mouse cursor back to right and it is bad.

What should I do?

您是否尝试过Masked TextBox

You should use TextBox.CaretIndex to set the insertion position so that when you modify TextBox.Text the insertion position doesn't change. You probably don't want to use TextBox.Select() . Untested code below:

private void txtPayani_TextChanged(object sender, EventArgs e)
    {
        int Position = txtPayani.CaretIndex;
        int Length = txtPayani.Text.Length;

        txtPayani.Text = setComma(payani);

        if(Position == Length) // If it was at the end, keep it there
        {
            txtPayani.CaretIndex = txtPayani.Text.Length;
        }
        else // Else, keep it in the same spot
        {
            txtPayani.CaretIndex = Position;
        }
    }

If you're using WinForms you may need to use SelectionStart and SelectionLength to do something similar. You didn't specify your UI framework, which would be helpful.

Consider making a custom textbox that holds a double value and a string formatting and displays the formatted text when the textbox doesn't have the focus, and the actual value when text is edited.

For example when not in focus, such a custom control with .Formatting = "n" for formatting will look like this:

1号

and when you tab (or click) inside it goes into edit mode like this where you set the raw number.

2号

See below the code I used for this:

[DefaultProperty("Value"), DefaultBindingProperty("Value")]
public class NumericTextBox : TextBox
{
    string formatting;
    double value;

    public NumericTextBox()
    {
        this.formatting = "g";
        this.value = 0.0;

        base.ReadOnly = true;
        base.Text = "0.0";
    }

    [Category("Data")]
    [SettingsBindable(true)]
    [DefaultValue("g")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [Bindable(BindableSupport.Yes)]
    public string Formatting
    {
        get => formatting;
        set
        {
            if (formatting == value)
            {
                return;
            }
            this.formatting= value;
            OnFormattingChanged(this, EventArgs.Empty);
            try
            {
                base.Text = Value.ToString(value);
            }
            catch (FormatException ex)
            {
                Trace.WriteLine(ex.ToString());
                base.Text = Value.ToString();
            }
        }
    }
    public event EventHandler FormattingChanged;

    protected void OnFormattingChanged(object sender, EventArgs e) => FormattingChanged?.Invoke(sender, e);

    [Category("Data")]
    [SettingsBindable(true)]
    [DefaultValue(0.0)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    [Bindable(BindableSupport.Yes)]
    public double Value
    {
        get => this.value;
        set
        {
            if (this.value == value)
            {
                return;
            }
            this.value=value;
            OnValueChanged(this, EventArgs.Empty);
            base.Text = value.ToString(Formatting);
        }
    }

    public event EventHandler ValueChanged;

    protected void OnValueChanged(object sender, EventArgs e) => ValueChanged?.Invoke(sender, e);

    protected override void OnLeave(EventArgs e)
    {
        base.OnLeave(e);
        base.ReadOnly = true;
        if (double.TryParse(base.Text, out double x))
        {
            base.Text = x.ToString(Formatting);
            this.Value = x;
        }
    }
    protected override void OnEnter(EventArgs e)
    {
        base.OnEnter(e);
        base.ReadOnly = false;
        base.Text = Value.ToString("R");
    }

}

I use this:

private void txtPayani_TextChanged(object sender, EventArgs e)
{
    if (txtPayani.Text.Length > 0)
    {
        txtPayani.Text = Convert.ToDouble(txtPayani.Text).ToString("N0");
        txtPayani.SelectionStart = txtPayani.Text.Length;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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