我想在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);
}
但是当我删除一个中位数形式的文本框时,再次将鼠标光标移回右侧,这很糟糕。
我该怎么办?
答案 0 :(得分:2)
您尝试过Masked TextBox吗?
答案 1 :(得分:0)
您应该使用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,则可能需要使用SelectionStart
和SelectionLength
来执行类似的操作。您没有指定UI框架,这会有所帮助。
答案 2 :(得分:0)
请考虑制作一个自定义文本框,该文本框具有double
值和string
格式,并在文本框没有焦点时显示格式化的文本,而在编辑文本时显示实际值。 / p>
例如,当焦点不清晰时,带有.Formatting = "n"
进行格式化的自定义控件将如下所示:
当您在其中单击(或单击)时,它会进入编辑模式,在其中您可以设置原始编号。
请参阅下面我用于此的代码:
[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");
}
}
答案 3 :(得分:0)
我用这个:
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;
}
}