Iman
Iman

Reputation: 145

GroupBox.Controls.count does not Update in C#

I'm trying to Search through a series of contacts. (saved in an xml file, and then recovered to the variable myContacts) the code for searching is fine, it gets the right results, my problem is Showing the contact in the text boxes. This is my code for showing the results in the text boxes:

private void showContact(int index)
    {
        txtNameResult.Text = myContacts[index].getName();
        txtFNameResult.Text = myContacts[index].getFName();
        txtNumberResult1.Text = myContacts[index].getSingleNumber(0);
        int position = 30;
        for (int i = 1; i < myContacts[index].getNumCount(); i++)
        {
            TextBox formtxtBox = txtNumberResult1;
            TextBox txtMyNum = new TextBox();
            gbResult.Controls.Add(txtMyNum);
            txtMyNum.Size = formtxtBox.Size;
            txtMyNum.Location = new Point(formtxtBox.Location.X, formtxtBox.Location.Y + position);
            position += 30;
            txtMyNum.Text = myContacts[index].getSingleNumber(i);
            txtMyNum.ReadOnly = true;
            txtMyNum.Name = "txtNumberResult" + (i + 1).ToString();
            txtMyNum.TabIndex = i + 2;
            if (this.Height < 414)
            {
                gbResult.Height += 30;
                this.Height += 30;
            }

        }//end of for
    }//end of show contact

and this is my form:

![search Form][1]

each contact can have up to 5 numbers and with each extra number a text box is added under the txtNumberResult1 and the groupbox and form height are extended. the problem is that when I search for a contact with 5 numbers it shows it correctly but if I search for another contact AFTER that, the extra textboxes don't delete. this is rather odd, but after an hour of debugging I finally found out that the problem is in my clearResults() code the gbResult.Controls.count (gbResult is the groupbox) is 8 (it should be 11 now) this is my clearResults() code(the code that executes at the beginning of Save button click event, it resets everything):

private void clearResults()
    {
        gbResult.Focus();
        txtNameResult.Text = "";
        txtFNameResult.Text = "";
        txtNumberResult1.Text = "";
        foreach (Control txtBox in gbResult.Controls)
        {
                if (txtBox.GetType() == typeof(TextBox))
                {
                    if (txtBox.Name != "txtNumberResult1" && txtBox.Name != "txtNameResult" && txtBox.Name != "txtFNameResult")
                    {
                        //remove the controls
                        txtBox.Text = "";
                        gbResult.Controls.Remove(txtBox);
                        txtBox.Dispose();
                    }//end of if
                }//end of if
        }//end of foreach
        //shrink the form
        while (this.Height > 294 && gbResult.Height > 129)
        {
            this.Height -= 30;
            gbResult.Height -= 30;
        }
    }//end of clear results

thank you in advance and sorry if it seems a bit confusing! PS: I can't post images as I don't have the required rep :|

EDIT: This is the correct clearResults() method:

private void clearResults()
    {
        gbResult.Focus();
        txtNameResult.Text = "";
        txtFNameResult.Text = "";
        txtNumberResult1.Text = "";
        //foreach (Control txtBox in gbResult.Controls)
        for (int i = 15; i > -1; i--)
        {

            try
            {
                var txtBox = gbResult.Controls[i];
                if (txtBox.GetType() == typeof(TextBox))
                {
                    if (txtBox.Name != "txtNumberResult1" && txtBox.Name != "txtNameResult" && txtBox.Name != "txtFNameResult")
                    {
                        //remove the controls
                        txtBox.Text = "";
                        gbResult.Controls.Remove(txtBox);
                        txtBox.Dispose();
                    }//end of if
                }//end of if
            }//end of try
            catch (Exception)
            {

                continue;
            }
        }//end of for

        //shrink the form
        while (this.Height > 294 && gbResult.Height > 129)
        {
            this.Height -= 30;
            gbResult.Height -= 30;
        }
    }//end of clear results

For those who have the same problem :)

Upvotes: 1

Views: 492

Answers (1)

Jeremy Thompson
Jeremy Thompson

Reputation: 65702

In your clearResult() method try removing the controls in reverse order:

for (int i = 20; i > -1; i--)

Upvotes: 1

Related Questions