[英]C# winform how textbox became null on button click second time?
In a form I have group Box which contains tab control with 4 tabs. 在一个表单中我有组Box,其中包含带有4个选项卡的选项卡控件。
In the second tab I have some textboxes and before saving data I need to validate input entered in these textbxes. 在第二个选项卡中,我有一些文本框,在保存数据之前,我需要验证在这些textbx中输入的输入。 Please note my save button is in last tab. 请注意我的保存按钮位于最后一个标签中。
The following test scenario works: 以下测试方案有效:
However, in the following test scenario an "Object refrence not set to an instance of a object" exception is thrown: 但是,在以下测试方案中,抛出了“未设置为对象实例的对象引用”异常:
I have placed break point and it shows that particular textbox is null. 我已经设置了断点,它显示特定的文本框为空。 It happens with every textbox where I break order. 它发生在我打破订单的每个文本框中。
Please guide me where I am incorrect and how I can fix it. 请指导我不正确的地方以及我如何解决它。
Below is my code that I am running on button press. 下面是我按下按钮时运行的代码。
private void btnOrderSave_Click(object sender, EventArgs e)
{
SaveOrder();
}
private void SaveOrder()
{
try
{
decimal? _marketRate, _bankcharges, _portcharges, _handlingcharges, _othercharges, _taxratio, _profitratio;
if (!String.IsNullOrEmpty(txtUnitPrice.Text))
{
if (valCtr.IsDecimal(txtUnitPrice.Text))
{
_marketRate = Convert.ToDecimal(txtUnitPrice.Text);
}
else
{
ErrorMessage("Rate is invalid");
return;
}
}
else
{
txtUnitPrice = null;
}
if (!String.IsNullOrEmpty(txtProfitRatio.Text))
{
if (valCtr.IsDecimal(txtProfitRatio.Text))
{
_marketRate = Convert.ToDecimal(txtProfitRatio.Text);
}
else
{
ErrorMessage("Profit ratio is invalid");
return;
}
}
else
{
txtProfitRatio = null;
}
}
catch (Exception ex)
{
AlertMessage(ex.InnerException + " :" + ex.Message + " : " + ex.StackTrace + " : " + ex.Source);
}
}
Are you sure you mean to set the textbox itself to null not the .Text
or other member? 您确定要将文本框本身设置为null而不是.Text
或其他成员吗?
txtUnitPrice = null;
Call it a hunch, but will these work better? 称之为预感,但这些会更好吗?
txtUnitPrice.Text = null;
....
txtProfitRatio.Text = null;
The problem occurs because you are setting your Textboxes to NULL-value. 出现此问题的原因是您将文本框设置为NULL值。
else
{
txtUnitPrice = null;
}
you should instead set the Text property to String.Empty like so: 您应该将Text属性设置为String.Empty,如下所示:
else
{
txtUnitPrice.Text = String.Empty;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.