tags:

views:

150

answers:

1

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?

+2  A: 

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.

Guffa
Ok sir i agree with u but can u tell me if my application requirement to draw runtime object like textboxes. it's works by increment of textboxes as per user requirement. but i wants to keep those textboxes which contains data and remove or not want any textboxes which is empty than what to do?. can u suggest me sir.
mahesh
No Sir, I am created textboxes on runtime I am having only those textboxes who contain data. which textboxes don't having data sould be deleted.
mahesh
like textbox tb= new textbox and it should be increment as per user desire it may three or four or many more.
mahesh
@mahesh: Get the textboxes that are empty using `var tbs = flowLayoutPanel1.Controls.Cast<TextBox>.Where(t=>t.Text.Length==0);`, then loop through them and remove them from the panel and dispose them.
Guffa
thx that's what i want but sir one more confusion is that as per Mr. Hans Pasant Suggestion is that dispose onto textbox is only for being invisible it is not completly remove is that true sir.
mahesh
@mahesh: You use the `Remove` method of the `flowLayoutPanel1.Controls` collection to remove the textbox from it, then you can dispose the textbox. After that you just let the reference to the textbox go, and the garbage collector will eventually remove the managed object.
Guffa
@Guffa Sir, i am using vs-2005 and ur code looks like from LINQ basis so how to use it in vs-2005 c# application please guide me.
mahesh
@Guffa Sir, Lots thanks, and i am appreciate your idea and implement it in my project. once again thx for give me your valuable time. i will always respect u sir.
mahesh
@mahesh: To do it without LINQ you just loop through the textboxes and put the references of the empty ones in a list, then you loop through the list and remove them. The reason that this has to be done in two steps, is that you can't remove items from a list that you are currently iterating.
Guffa
@Guffa Ok sir, and I promise u that i will do it and complete my project. ur idea which is given is very useful for me. i will definately complete this above project. Once agains thx for ur valuable feedback.
mahesh
@Guffa I have Completed the above project. thx
mahesh