C# 当用户在文本框中键入任意金额的现金时,设置货币值的格式
当用户输入任意数量的现金时,自动设置货币值格式的最佳方法是什么 例如,我有一个文本框,如果用户输入“30”,它认为是0.30C# 当用户在文本框中键入任意金额的现金时,设置货币值的格式,c#,wpf,C#,Wpf,当用户输入任意数量的现金时,自动设置货币值格式的最佳方法是什么 例如,我有一个文本框,如果用户输入“30”,它认为是0.30 如果他输入“300”,它将考虑3.00。制作一个文本框,并在TextChanged事件中处理它 <TextBox x:Name="money" Text="00.00" TextChanged="TextBox_TextChanged"/> 您使用的UI框架可能存在重复?到目前为止您尝试了什么?选项1:构建自定义控件并侦听keydown事件。选项2:在后端有
如果他输入“300”,它将考虑3.00。制作一个文本框,并在
TextChanged
事件中处理它
<TextBox x:Name="money" Text="00.00" TextChanged="TextBox_TextChanged"/>
您使用的UI框架可能存在重复?到目前为止您尝试了什么?选项1:构建自定义控件并侦听keydown事件。选项2:在后端有一个单独的隐藏文本框控件。每当显示文本框获得焦点时,只需将输入焦点传递到该隐藏框,并在那里执行逻辑
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (money.Text.Length == 3)
{
money.TextChanged -= TextBox_TextChanged;
money.Text = money.Text.Replace("00.", "00.0");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
if (money.Text.Length == 1)
{
money.TextChanged -= TextBox_TextChanged;
if(money.Text.Contains(".")) money.Text = money.Text.Replace(".", "");
money.Text = money.Text.Insert(0, "00.0");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
else if (money.Text.StartsWith("00.0") && money.Text.Length == 6)
{
money.TextChanged -= TextBox_TextChanged;
money.Text = money.Text.Replace("00.0", "00.");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
else if (money.Text.StartsWith("00.") && money.Text.Length == 6)
{
money.TextChanged -= TextBox_TextChanged;
money.Text = money.Text.Replace("00.", "0");
money.Text = money.Text.Insert(money.Text.Length - 2, ".");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
else if (money.Text.StartsWith("0") && money.Text.Length == 6)
{
money.TextChanged -= TextBox_TextChanged;
money.Text = money.Text.Replace("0", "");
if(money.Text.Contains(".")) money.Text = money.Text.Replace(".", "");
money.Text = money.Text.Insert(money.Text.Length - 2, ".");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
else if(!money.Text.StartsWith("0"))
{
money.TextChanged -= TextBox_TextChanged;
if (money.Text.Length == 3 && money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00");
if(money.Text.Length == 4 && money.Text.Contains(".")) money.Text = money.Text.Insert(0, "0");
if(money.Text.Length == 1 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00.0");
if(money.Text.Length == 2 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00.");
if (money.Text.Length == 3 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "0");
if (money.Text.Contains(".")) money.Text = money.Text.Replace(".", "");
money.Text = money.Text.Insert(money.Text.Length - 2, ".");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
else
{
money.TextChanged -= TextBox_TextChanged;
if (money.Text.Length == 3 && money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00");
if (money.Text.Length == 4 && money.Text.Contains(".")) money.Text = money.Text.Insert(0, "0");
if (money.Text.Length == 1 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00.0");
if (money.Text.Length == 2 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "00.");
if (money.Text.Length == 3 && !money.Text.Contains(".")) money.Text = money.Text.Insert(0, "0");
if (money.Text.Contains(".")) money.Text = money.Text.Replace(".", "");
money.Text = money.Text.Insert(money.Text.Length - 2, ".");
money.CaretIndex = money.Text.Length;
money.TextChanged += TextBox_TextChanged;
}
}