[英]Textbox dispose not working
我正在使用C#和Visual Studio 2005。
我在运行时在FlowlayoutPanel中创建了多个Texbox。 它工作正常,但是当我尝试处理空文本框并按如下所示放置消息时。
void tbb_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Keys)e.KeyChar == Keys.Enter)
{
listBox2.Visible = false;
button4.Visible = false;
if (tbb.Text!="")
{
bb.Visible = true;
bb.Focus();
}
else
{
//tbb.Visible = false;
tbb.Dispose();
bb.Dispose();
textBox2.Visible = true;
textBox2.Focus();
}
}
}
上面的代码可以正常运行,并且可以在运行时很好地处理。 数据保存代码为:
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if ((Keys)e.KeyChar == Keys.Enter)
{
if (bb.Text == "")
{
MessageBox.Show("Sorry Empty.Row");
this.flowLayoutPanel1.Controls.Clear();
label13.Text = "";
textBox1.Text = "";
textBox2.Text = "";
maskedTextBox1.Text = "";
maskedTextBox1.Enabled = true;
maskedTextBox1.Focus();
textBox1.Enabled = true;
}
else
{
string connstr = "server=.;initial catalog= maa;uid=mah;pwd=mah";
SqlConnection con = new SqlConnection(connstr);
con.Open();
SqlCommand cmd1 = new SqlCommand("insert into debankA(companyID,transID,date,bank,totdepo,narrat) values " +
"(@companyID,@transID,Convert(datetime,'" + maskedTextBox1.Text.ToString() + "',103),@bank,@totdepo,@narrat)", con);
cmd1.Parameters.Add("@bank", SqlDbType.VarChar).Value = textBox1.Text;
cmd1.Parameters.Add("@totdepo", SqlDbType.Decimal).Value = label13.Text;
cmd1.Parameters.Add("@narrat", SqlDbType.VarChar).Value = textBox2.Text;
cmd1.Parameters.Add("@companyID", SqlDbType.Int).Value = label6.Text;
cmd1.Parameters.Add("@transID", SqlDbType.Int).Value = textBox4.Text;
cmd1.ExecuteNonQuery();
string pparticulars = null;
double? depo = null;
string messs = "Record Save Successfully";
foreach (Control ctl in this.flowLayoutPanel1.Controls)
{
if (ctl.Name.Contains("tbb") && ctl is TextBox)
{
pparticulars = ctl.Text;
}
if (ctl.Name.Contains("bb") && ctl is TextBox)
{
double ddepo = 0;
if (double.TryParse(ctl.Text, out ddepo))
depo = ddepo;
if (pparticulars != null && depo != null)
{
SqlCommand cmd = new SqlCommand("insert into debankB(particulars,deposit,companyID,transID)values" +
"(@particulars,@deposit,@companyID,@transID)", con);
cmd.Parameters.Add("@particulars", SqlDbType.VarChar).Value = pparticulars;
cmd.Parameters.Add("@deposit", SqlDbType.Decimal).Value = depo;
cmd.Parameters.Add("@companyID", SqlDbType.Int).Value = label6.Text;
cmd.Parameters.Add("@transID", SqlDbType.Int).Value = textBox4.Text;
cmd.ExecuteNonQuery();
pparticulars = null;
depo = null;
MessageBox.Show(messs);
messs = null;
this.flowLayoutPanel1.Controls.Clear();
label13.Text = "";
textBox1.Text = "";
textBox2.Text = "";
maskedTextBox1.Text = "";
maskedTextBox1.Enabled = true;
maskedTextBox1.Focus();
textBox1.Enabled = true;
}
即使我已经处置了两个空的文本框,消息也始终只显示如上设置的“ empty.records”。
这意味着没有处理空的文本框。 但是,如果这是真的,那么当我运行该应用程序并创建有可用数据的文本框时,它保持不变并且不显示空文本框。 配置进入。
我不明白是什么问题。 如果在运行时处理了文本框,那么如何将其显示为空?
您要通过布置控件来完成什么?
处置对象意味着您告诉它删除所有非托管资源,因为您将不再使用该对象。 对于像TextBox
这样的winform TextBox
这意味着它释放了实际的Windows控件,但这并不意味着TextBox
对象消失了。
如果要从页面中删除控件,则应首先从控件树中删除该对象,然后再进行处理。 如果只处理它,则将控件对象保留在页面中,但没有要显示的相应窗口控件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.