0

I am working on a small project where I have 2 buttons and 2 panels. panel 1 has TextBox panel 2 has a different TextBox

And there are 2 buttons. When I press button1 panel2 will show, and when I press button2 panel1 will show. Now let say I type something in panel1's textbox I want when I move to panel2 and move back to panel1 the textbox to be empty, just like if I am running the form again. Here are my codes for the 2 buttons.

This is for the button that will show panel2

 private void ShowPanel2_Click(object sender, EventArgs e)
    {
        Panel Panel2Var = new Panel();
        Panel2Var = Panel2;
        Panel1.Hide();
        Panel2.Show();
    }

This is the button that will show panel1

 private void ShowPanel1_Click(object sender, EventArgs e)
    {
        Panel Panel1Var = new Panel();
        Panel1Var = Panel1;
        Panel2.Hide();
        Panel1.Show();
    }

1 Answers1

0

You are trying to clear out the textbox when you move back? That can be accomplished by looking at all of the components in the panel, finding the textbox, and clearing it. Like:

foreach (Control p in Panel1.Controls)
  if (p is TextBox)
     p.Clear(); //or use .text like below
     p.Text = "";
Display Name is missing
  • 6,197
  • 3
  • 34
  • 46
  • what if I have an open connection? and radiobuttons and etc... I want every thing to go back to it's default value. –  Aug 08 '13 at 20:17
  • @Muhand Jumah Can't you just write a method to *initialize* and one to *reset* everything? Of course, assuming you are inserting every control manually. – KappaG3 Aug 08 '13 at 20:24
  • @KappaG3 That's What I did. I have a function that resets every control. but I was wondering if there is a function that does it automatically without having me inserting every control manually. –  Aug 08 '13 at 20:52
  • This thread asks the same question and presents a way to clear all controls. http://stackoverflow.com/questions/297526/what-is-the-best-way-to-clear-all-controls-on-a-form-c It does not look like you can do it without writing a custom function. – Display Name is missing Aug 08 '13 at 21:02