Link to home
Start Free TrialLog in
Avatar of onebite2
onebite2

asked on

Fire an event with button to clear all the values in the controls on a winform application.

I have 2 buttons one search for the values of all the controls(listbox,textbox,groupbox,datagridview) on a screen and the other should clear all the values when a user clicks on it and return the values when the user clicks again on Search button.

I have the code written for Search Button.But for the Clear button I embedded the code ,it is trying to clear the values but when the user tries to Search for new values by entering his firstname in a textbox.....its not pulling the details instead everything is blank..

What shoud be the code to clear values in a Datagrid view???


Here goes my sample try for clearing values:

Need help with the code!

private void button2_Click(object sender, EventArgs e)
        {
            ltemp1.Visible = false;
            ltemp.Visible = false;
            ClearAll();
        }
 
  private void ClearAll()
        {
            textBox1.Text = string.Empty;
            textBox2.Text = string.Empty;
            textBox3.Text = string.Empty;
            textBox4.Text = string.Empty;
       
            foreach (Control c in groupBox2.Controls)
            {
                c.Visible = true;
            }
            ltemp1.Visible = false;
            ltemp.Visible = false;
            groupBox2.Controls.Clear();
 
            foreach (Control c in groupBox3.Controls)
            {
                c.Visible = false;
            }
 
            ltemp.Text = "No Credit Card Stored";
            ltemp.Location = new Point(50, 50);
 
            groupBox3.Controls.Clear();
            ltemp.Visible = true;
 
 
            foreach (Control c in groupBox4.Controls)
            {
                c.Visible = true;
            }
            groupBox4.Controls.Clear();
 
 
            // lstSerListBox.Items.Clear();
            
 
        }

Open in new window

Avatar of Roopesh_7
Roopesh_7

gridview.datasource = null;
Avatar of onebite2

ASKER

Thanks a lot!This worked for a datagridview.

How about clearing the values in a list box which has datasource to it as

Listbox1.datasource=proreg;

Listbox1.items.clear();does not work as listbox has datasource to it...
listbox1.selecteditems.clear();also is not working..

I appreciate if u can tell me a work around for listbox too...
Thanks!
ASKER CERTIFIED SOLUTION
Avatar of Roopesh_7
Roopesh_7

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial