Ian Marks Ian Marks - 3 months ago 8
C# Question

How can I clear the contents of a "comboBox" after I have submitted a form

I am trying to clear the contents of a Windows form in C# after I have submitted the data.

I have managed to do this for text boxes using the following code:

foreach (Control c in Controls)
{
if (c is TextBox)
{
c.Text = "";


However, I am struggling to complete the same task for the "comboBoxes" within the form. I have tried to use a variation of the code as follows but this does not seem to work.

if (c is ComboBox)
{
c.Text = "";


So full code looks as follows:

foreach (Control c in Controls)
{
if (c is TextBox)
{
c.Text = "";
}
if (c is ComboBox)
{
c.Text = "";
}


Can anyone suggest a resolution, what am I missing?

Kind Regards

Ian

Answer

Your code goes like this:

//your submission of the form code here...

foreach (Control c in this.Controls)
{
    if (c is TextBox)
    {                    
        ((TextBox)c).Clear();
        //c.Text = String.Empty;
    }
    if (c is ComboBox)
    {
        ((ComboBox)c).Items.Clear();
    }
} 
Comments