I am using C# and Visual Studio 2005.
I have created multiple Texboxes at runtime in FlowlayoutPanel. It works fine, but when I am trying to dispose null textboxes and put message like below.
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();
}
}
}
The above code works fine and disposing at runtime well. The data saving code is:
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;
}
Even though I have disposed both empty textboxes message always show only "empty.records" as above I set.
That means the empty textboxes are not disposed. But if it is true then when I have run the application and created textboxes where is data available it remains the same and empty textboxes are not visible. Disposing on enter.
I don't understand what is the problem. If the textbox is disposed during runtime then how can it show as empty?
What are you trying to accomplish by disposing the controls?
Disposing an object means that you tell it to remove all unmanaged resources, because you are not going to use the object any more. For a winform control like a TextBox
this means that it frees the actual windows control, but it doesn't mean that the TextBox
object goes away.
If you want to remove controls from the page, you should first remove the object from the control tree, then you can dispose it. If you just dispose it, you leave a control object in the page, but without a corresponding window control to be displayed.
精彩评论