[英]How can I separate digits with comma (,) in textbox in C# winform?
I want to separate digits with comma in textbox in Winforms.我想在 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. 您应该使用TextBox.CaretIndex
设置插入位置,以便在修改TextBox.Text
插入位置不会改变。 You probably don't want to use TextBox.Select()
. 您可能不想使用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. 如果您使用的是WinForms,则可能需要使用SelectionStart
和SelectionLength
进行类似的操作。 You didn't specify your UI framework, which would be helpful. 您没有指定您的UI框架,这会有所帮助。
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. 考虑制作一个自定义文本框,该文本框具有double
值和string
格式,当文本框没有焦点时,将显示格式化后的文本;而在编辑文本时,则显示实际值。
For example when not in focus, such a custom control with .Formatting = "n"
for formatting will look like this: 例如,当焦点不.Formatting = "n"
时,使用.Formatting = "n"
进行格式化的自定义控件将如下所示:
and when you tab (or click) inside it goes into edit mode like this where you set the raw number. 当您在其中选择标签(或单击)时,它会进入像这样设置原始编号的编辑模式。
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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.