繁体   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