tester123
tester123

Reputation: 409

Clear specific tabpage text boxes

I have 4 tabs in my tabcontrol page. I want to clear all the textboxes in the last tab. However the code I am using only clears the texbox that is selected.

foreach (Control control in tabcontrol1.SelectedTab.Controls)
{
    TextBox text = control as TextBox;
    if (text != null)
    {
        text.Text = string.Empty;
    }
}

Upvotes: 0

Views: 956

Answers (3)

Dinesh Maind
Dinesh Maind

Reputation: 349

Please, visit following link. You will get better explanation about how to use clear controls. Clear all textboxes in form with one Function. Hope, it will be helpful for you.

Upvotes: 0

Hardik Gondalia
Hardik Gondalia

Reputation: 3717

Just Try This: might be useful to you..

void ClearTextBoxes(Control parent)
    {
        foreach (Control child in parent.Controls)
        {
            TextBox textBox = child as TextBox;
            if (textBox == null)
                ClearTextBoxes(child);
            else
                textBox.Text = string.Empty;
        }
    }

    private void resetCurrentPageToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ClearTextBoxes(tabControl1.SelectedTab);
    }

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460258

So you want to find all textboxes in the last tab? You could use htis:

var allTxt = tabcontrol1.TabPages.Cast<TabPage>().Last().Controls.OfType<TextBox>();

foreach(TextBox txt in allTxt)
    txt.Text = "";

(you need to add using System.Linq)

Upvotes: 1

Related Questions