"Input String is not in Correct Format" withTextbox Selection Changed 事件
"Input String is not in Correct Format" withTextbox Selection Changed Event
private void txtdiscount_SelectionChanged(object sender, RoutedEventArgs e)
{
try
{
string dis = txtdiscount.Text.ToString();
double isid = double.Parse(dis);
isid = isid + 10;
MessageBox.Show(isid.ToString());
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
我想在文本框 txtdiscount
和文本框的 SelectionChanged
事件中输入(双精度类型),MessageBox
应该在增量 10 后显示输入的值它的价值。但是使用上面的代码,我得到一个例外:
"Input String was not correct format"
行:
string dis = txtdiscount.Text.ToString()
文本框 SelectionChanged
事件中的这段代码有什么问题,因为在按钮单击事件中执行相同的代码可以正常工作?
<TextBox x:Name="txtdiscount" HorizontalAlignment="Left" Height="33" Margin="831,97,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="154" SelectionChanged="txtdiscount_SelectionChanged"/>
Use Double.TryParse()
& instead of SelectionChanged
use TextChanged
event.
As per MSDN
SelectionChanged :- This event occurs whenever there is a change to a selection. A selection can be changed not only by user interaction
but also by binding as well as other set values.
TextChanged :- This event is raised if the Text property is changed by either a programmatic modification or user interaction.
string dis = txtBox.Text;
double isId;
if (Double.TryParse(dis, out isId))
{
isId = isId + 10;
MessageBox.Show(isId.ToString());
}
else
{
MessageBox.Show("Please Only enter Number");
}
您的问题是 SelectionChanged
事件在您单击 TextBox
时立即触发。此时内部没有值,所以 double.Parse()
得到一个空字符串作为输入并抛出这个异常。当您从 TextBox
.
中删除最后一位数字时,它也会抛出异常
要解决这种情况,您可以检查空值:
private void txtdiscount_SelectionChanged(object sender, RoutedEventArgs e)
{
try
{
if (!string.IsNullOrWhiteSpace(txtdiscount.Text))
{
string dis = txtdiscount.Text;
double isid = double.Parse(dis);
isid = isid + 10;
MessageBox.Show(isid.ToString());
}
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
same code works fine when performed in a button click event
因为你在输入有效值后点击。这就是区别
private void txtdiscount_SelectionChanged(object sender, RoutedEventArgs e)
{
try
{
string dis = txtdiscount.Text.ToString();
double isid = double.Parse(dis);
isid = isid + 10;
MessageBox.Show(isid.ToString());
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
我想在文本框 txtdiscount
和文本框的 SelectionChanged
事件中输入(双精度类型),MessageBox
应该在增量 10 后显示输入的值它的价值。但是使用上面的代码,我得到一个例外:
"Input String was not correct format"
行:
string dis = txtdiscount.Text.ToString()
文本框 SelectionChanged
事件中的这段代码有什么问题,因为在按钮单击事件中执行相同的代码可以正常工作?
<TextBox x:Name="txtdiscount" HorizontalAlignment="Left" Height="33" Margin="831,97,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="154" SelectionChanged="txtdiscount_SelectionChanged"/>
Use
Double.TryParse()
& instead ofSelectionChanged
useTextChanged
event.As per MSDN
SelectionChanged :- This event occurs whenever there is a change to a selection. A selection can be changed not only by user interaction but also by binding as well as other set values.
TextChanged :- This event is raised if the Text property is changed by either a programmatic modification or user interaction.
string dis = txtBox.Text;
double isId;
if (Double.TryParse(dis, out isId))
{
isId = isId + 10;
MessageBox.Show(isId.ToString());
}
else
{
MessageBox.Show("Please Only enter Number");
}
您的问题是 SelectionChanged
事件在您单击 TextBox
时立即触发。此时内部没有值,所以 double.Parse()
得到一个空字符串作为输入并抛出这个异常。当您从 TextBox
.
要解决这种情况,您可以检查空值:
private void txtdiscount_SelectionChanged(object sender, RoutedEventArgs e)
{
try
{
if (!string.IsNullOrWhiteSpace(txtdiscount.Text))
{
string dis = txtdiscount.Text;
double isid = double.Parse(dis);
isid = isid + 10;
MessageBox.Show(isid.ToString());
}
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
same code works fine when performed in a button click event
因为你在输入有效值后点击。这就是区别