[英]C# winform how textbox became null on button click second time?
在一個表單中我有組Box,其中包含帶有4個選項卡的選項卡控件。
在第二個選項卡中,我有一些文本框,在保存數據之前,我需要驗證在這些textbx中輸入的輸入。 請注意我的保存按鈕位於最后一個標簽中。
以下測試方案有效:
但是,在以下測試方案中,拋出了“未設置為對象實例的對象引用”異常:
我已經設置了斷點,它顯示特定的文本框為空。 它發生在我打破訂單的每個文本框中。
請指導我不正確的地方以及我如何解決它。
下面是我按下按鈕時運行的代碼。
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);
}
}
您確定要將文本框本身設置為null而不是.Text
或其他成員嗎?
txtUnitPrice = null;
稱之為預感,但這些會更好嗎?
txtUnitPrice.Text = null;
....
txtProfitRatio.Text = null;
出現此問題的原因是您將文本框設置為NULL值。
else
{
txtUnitPrice = null;
}
您應該將Text屬性設置為String.Empty,如下所示:
else
{
txtUnitPrice.Text = String.Empty;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.