locked
How to Clear the User control's Control on a Button Click? RRS feed

  • Question

  • I have a User control with CheckedListBox, Combobox and Textbox.

    This User control is added dynamically.

    Now i want to clear these User control's control like CheckedListBox, Combobox and Textbox on click of a Clear button.

     

    How to do that?

    Pls Help.

    Tuesday, July 13, 2010 5:29 PM

Answers

  • Thanks for your replies dude.

    I want to set the combobox selectedIndex to 0 and Uncheck all the items from the Checkedlistbox and make the textbox text to string.Empty.

    But my problem is that these controls are there in a user control and also i am adding this user control dynamically which like adding rows to a datagridview.

    Now i want to iterate all the user control which is placed in the form and make the above changes.

    Thats it.


    Keep track of all the user controls you dynamically created, perhaps in a List<>.  When you want to clear them, iterate through the list and call a public ClearControls method defined on the user control class. In ClearControls, add code to clear individual controls as shown above.
    http://blog.voidnish.com
    • Marked as answer by Helen Zhou Monday, July 19, 2010 6:42 AM
    Wednesday, July 14, 2010 11:26 AM
  • Hello,

    Once and for all, I make a program for all the adding possibilities, including textbox or combobox etc. are added dynamically or statically, and myUserControl added dynamically or by drag-and-drop.

    Supposing you have a class MyUserControl, and then you created N instances of the MyUserControl. Once you click the button1, you want all the instance of the MyUserControl be resetted, no matter what controls inside each instance usercontrol. Here is the code:

           private void button1_Click(object sender, EventArgs e)
            {
                for(int k = 0; k< this.Controls.Count;k++)
                {
                    if(this.Controls[k] is MyUserControl)
                    {
                        MyUserControl _obj = this.Controls[k] as MyUserControl;
                        for (int i = 0; i < _obj.Controls.Count; i++)
                        {
                            if (_obj.Controls[i].GetType().ToString() == "TextBox")
                                (_obj.Controls[i] as TextBox).Clear();

                            if (_obj.Controls[i].GetType().ToString() == "ComboBox")
                                (_obj.Controls[i] as ComboBox).SelectedIndex = -1;              

                        //.....
                        }
                    }
                }
            }

    In the first circulation, find all the instance of MyUserControl, in the second sirculation, find all the inner controls inside the MyUserControl instance,and reset all of them.

    Regards,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Monday, July 19, 2010 7:35 AM
    Monday, July 19, 2010 7:34 AM

All replies

  • For the CheckedListBox/ComboBox, call obj.Items.Clear() and for the TextBox, set the Text property to String.Empty.
    http://blog.voidnish.com
    • Proposed as answer by Vladimir.Ilic Tuesday, July 13, 2010 8:03 PM
    Tuesday, July 13, 2010 5:34 PM
  • Huh, I might have proposed this one too soon ... I apologize ... if this answer does not meet your needs could you please elaborate on the problem ... what do you mean by clear combobox .. remove all items or remove just make it as if no item is selected ?
    Tuesday, July 13, 2010 8:06 PM
  • Thanks for your replies dude.

    I want to set the combobox selectedIndex to 0 and Uncheck all the items from the Checkedlistbox and make the textbox text to string.Empty.

    But my problem is that these controls are there in a user control and also i am adding this user control dynamically which like adding rows to a datagridview.

    Now i want to iterate all the user control which is placed in the form and make the above changes.

    Thats it.

    Wednesday, July 14, 2010 5:06 AM
  • But my problem is that these controls are there in a user control and also i am adding this user control dynamically which like adding rows to a datagridview.

    Now i want to iterate all the user control which is placed in the form and make the above changes.

    What user control? Can you please explain this a liitle better?

    And what are you trying to achive with all those controls? Please give us some more reasonable explanation. Now I really have no clue about what are you talkin about.

    Wednesday, July 14, 2010 6:51 AM
  • But my problem is that these controls are there in a user control and also i am adding this user control dynamically which like adding rows to a datagridview.

    Hello Turbulent Geek,

        Are you adding UserControl dynamically in the Form?

                   OR

       You are adding Controls to UserControl dynamically?

     

    Thanks,

    Paras Sanghani

    Mark As Answer if it helped you.

    Wednesday, July 14, 2010 6:54 AM
  • Hi,

    well, just add a method to your UserControl like this:

     

    public void ClearControls()

    {

      myTextBox.Text = "";

      myComboBox.Checked = false;

      // ...

    }

    Call this method on Button-click.

     

    Bye and kind regards,

     

    Matthias (CTO)

    PFGrid.com

    Wednesday, July 14, 2010 7:04 AM
  • I will show you how to clear comboBox, textBox and CheckedListBox:

        private void buttonClear_Click(object sender, EventArgs e)
        {
          //1. CLEARING COMBOBOX:
          comboBox1.Items.Clear();
    
          //2. CLEARING TEXTBOX:
          textBox1.Text = String.Empty; //or textBox1.Text = "";
    
          //3. CLEARING CHECKEDLISTOBX:   
          for (int i = 0; i <= checkedListBox1.Items.Count - 1; i++)
          {
            // Get the selected item's check state.
            CheckState chkstate = default(CheckState);
            chkstate = checkedListBox1.GetItemCheckState(i);
            // Toggle the item state.
            if ((chkstate == CheckState.Checked))
              chkstate = CheckState.Unchecked;
            //Set the new state.
            checkedListBox1.SetItemCheckState(i, chkstate);
          }
        }

     

    Wednesday, July 14, 2010 7:33 AM
  • Thanks for your replies dude.

    I want to set the combobox selectedIndex to 0 and Uncheck all the items from the Checkedlistbox and make the textbox text to string.Empty.

    But my problem is that these controls are there in a user control and also i am adding this user control dynamically which like adding rows to a datagridview.

    Now i want to iterate all the user control which is placed in the form and make the above changes.

    Thats it.


    Keep track of all the user controls you dynamically created, perhaps in a List<>.  When you want to clear them, iterate through the list and call a public ClearControls method defined on the user control class. In ClearControls, add code to clear individual controls as shown above.
    http://blog.voidnish.com
    • Marked as answer by Helen Zhou Monday, July 19, 2010 6:42 AM
    Wednesday, July 14, 2010 11:26 AM
  • Hi turbulentGeek,

    If the controls are dynamicly add to the usercontrol, you can do the reset as Nishant told, to travel through all the controls in the usercontrol and reset them as Mitjia told. Something like this:


        for(int i = 0;i<usercontrol.controls.count;i++)
        {
            if(usercontrol.controls[i].GetType().ToString() = "")
                (usercontrol.controls[i] as Combobox).Clear();

           .....
        }

    If the controls inside the usercontrol statically, you can do it like this: 

        public class usercontrol : Control
        {
            public void ResetControl()
            {
                this.TextBox1.Text = "";
                this.ComboBox1.SeltectedIndex = -1;
                ....
            }
        }

    Then, when some action was taken, eg.button1.click, you can all the usercontrol1.ResetControl(); to reset the usercontrol.

    Hope this helps.

    Sincerely,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Monday, July 19, 2010 7:12 AM
  • Hello,

    Once and for all, I make a program for all the adding possibilities, including textbox or combobox etc. are added dynamically or statically, and myUserControl added dynamically or by drag-and-drop.

    Supposing you have a class MyUserControl, and then you created N instances of the MyUserControl. Once you click the button1, you want all the instance of the MyUserControl be resetted, no matter what controls inside each instance usercontrol. Here is the code:

           private void button1_Click(object sender, EventArgs e)
            {
                for(int k = 0; k< this.Controls.Count;k++)
                {
                    if(this.Controls[k] is MyUserControl)
                    {
                        MyUserControl _obj = this.Controls[k] as MyUserControl;
                        for (int i = 0; i < _obj.Controls.Count; i++)
                        {
                            if (_obj.Controls[i].GetType().ToString() == "TextBox")
                                (_obj.Controls[i] as TextBox).Clear();

                            if (_obj.Controls[i].GetType().ToString() == "ComboBox")
                                (_obj.Controls[i] as ComboBox).SelectedIndex = -1;              

                        //.....
                        }
                    }
                }
            }

    In the first circulation, find all the instance of MyUserControl, in the second sirculation, find all the inner controls inside the MyUserControl instance,and reset all of them.

    Regards,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Helen Zhou Monday, July 19, 2010 7:35 AM
    Monday, July 19, 2010 7:34 AM