C#UWP如何键入数字字符串并按顺序实时获取逗号?
我试图在文本框中键入数字,并让它在每三个数字后实时添加逗号。我需要将数字串转换成实数,因为我还需要实时地做一个简单的数学方程。我遇到的一个问题是,如果我在点击4后按1234567的顺序键入,它会添加逗号,然后输入框跳转到字符串的开头。因此,我从1输入到7,得到5671234C#UWP如何键入数字字符串并按顺序实时获取逗号?,c#,uwp,C#,Uwp,我试图在文本框中键入数字,并让它在每三个数字后实时添加逗号。我需要将数字串转换成实数,因为我还需要实时地做一个简单的数学方程。我遇到的一个问题是,如果我在点击4后按1234567的顺序键入,它会添加逗号,然后输入框跳转到字符串的开头。因此,我从1输入到7,得到5671234 private void PriceBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args) { if (
private void PriceBox_TextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
{
if (!String.IsNullOrEmpty(PriceBox.Text))
{
int x = Int32.Parse(PriceBox.Text, NumberStyles.AllowThousands);
float y = x * .50f;
Half.Text = y.ToString("N0");
PriceBox.Text = x.ToString("N0");
}
}
如前所述,您可以格式化一个数字,使其每3位后面都有逗号。一种方法是挂接
TextChanged
事件,将当前数字转换为逗号分隔的数字,并用逗号分隔的数字替换当前文本
此外,要停止ovetflow异常,您必须取消订阅TextChanged
事件,然后再次订阅
总事件处理程序如下所示:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
/// unsubscribe, so that the replacing doesn't invoke this handler again
textBox.TextChanged -= TextBox_TextChanged;
if(double.TryParse(textBox.Text, out double value))
{
textBox.Text = value.ToString("N0");
}
/// put the cursor in the end of the text
textBox.Select(textBox.Text.Length, 0);
/// subscribe again
textBox.TextChanged += TextBox_TextChanged;
}
希望有帮助
编辑:
要仅允许使用数值,请钩住PreviewKeyDown
事件,仅允许使用数值键。像这样:
private void TextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
bool proceed =
(e.Key >= Windows.System.VirtualKey.Number0 && e.Key <= Windows.System.VirtualKey.Number9) ||
(e.Key >= Windows.System.VirtualKey.NumberPad0 && e.Key <= Windows.System.VirtualKey.NumberPad9);
e.Handled = !proceed;
}
private void TextBox\u PreviewKeyDown(对象发送方,KeyRoutedEventArgs e)
{
继续=
(e.Key>=Windows.System.VirtualKey.Number0&&e.Key=Windows.System.VirtualKey.NumberPad0&&e.Key您可以按照说明格式化一个数字,使其在每3位数字后都有逗号。一种方法是挂接TextChanged
事件,将当前数字转换为逗号分隔的数字,并用逗号分隔的数字替换当前文本
此外,要停止ovetflow异常,您必须取消订阅TextChanged
事件,然后再次订阅
总事件处理程序如下所示:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textBox = sender as TextBox;
/// unsubscribe, so that the replacing doesn't invoke this handler again
textBox.TextChanged -= TextBox_TextChanged;
if(double.TryParse(textBox.Text, out double value))
{
textBox.Text = value.ToString("N0");
}
/// put the cursor in the end of the text
textBox.Select(textBox.Text.Length, 0);
/// subscribe again
textBox.TextChanged += TextBox_TextChanged;
}
希望有帮助
编辑:
要仅允许使用数值,请钩住PreviewKeyDown
事件,仅允许使用数值键。如下所示:
private void TextBox_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
bool proceed =
(e.Key >= Windows.System.VirtualKey.Number0 && e.Key <= Windows.System.VirtualKey.Number9) ||
(e.Key >= Windows.System.VirtualKey.NumberPad0 && e.Key <= Windows.System.VirtualKey.NumberPad9);
e.Handled = !proceed;
}
private void TextBox\u PreviewKeyDown(对象发送方,KeyRoutedEventArgs e)
{
继续=
(e.Key>=Windows.System.VirtualKey.Number0&&e.Key=Windows.System.VirtualKey.NumberPad0&&e.Key谢谢你的回复。我现在正在工作,但我一回到家就会试试。如果它起作用,我一定会投票给你,并标记为正确答案。非常感谢你。你的代码工作了。它最多有3个逗号,然后就停止了。它也允许使用字母和特殊字符,但我会尽力解决。谢谢againI,我已经编辑了答案,以满足附加要求。检查它。谢谢你的回复。我现在正在工作,但我一回到家就会尝试一下。如果行得通,我一定会投你一票,并标记为正确答案。非常感谢。你的代码成功了。它最多有3个逗号,然后停止。它还允许字母和特殊字符,但我会尝试解决这个问题。感谢againI编辑了答案,以满足其他要求。检查它。