簡體   English   中英

如何在 C# winform 的文本框中用逗號 (,) 分隔數字?

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

我想在 Winforms 的文本框中用逗號分隔數字。 我寫了這個函數並且它有效:

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);
}

但問題是當我開始在文本框中鍵入數字時,鼠標光標位於文本的左側。 但我希望它是正常的,並放在數字的右邊。

為了解決這個問題,我使用了這個代碼:

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

但是當我刪除一個中間數字形式的文本框時,鼠標光標又回到了右邊,這很糟糕。

我該怎么辦?

您是否嘗試過Masked TextBox

您應該使用TextBox.CaretIndex設置插入位置,以便在修改TextBox.Text插入位置不會改變。 您可能不想使用TextBox.Select() 未經測試的代碼如下:

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;
        }
    }

如果您使用的是WinForms,則可能需要使用SelectionStartSelectionLength進行類似的操作。 您沒有指定您的UI框架,這會有所幫助。

考慮制作一個自定義文本框,該文本框具有double值和string格式,當文本框沒有焦點時,將顯示格式化后的文本;而在編輯文本時,則顯示實際值。

例如,當焦點不.Formatting = "n"時,使用.Formatting = "n"進行格式化的自定義控件將如下所示:

1號

當您在其中選擇標簽(或單擊)時,它會進入像這樣設置原始編號的編輯模式。

2號

參見下面我用於此的代碼:

[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");
    }

}

我用這個:

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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM