I am using this code to clear a form but it is throwing an error. Any help is appreciated.

public static void ClearForm(Control parent)
        {
            foreach (Control ctrControl in parent.Controls)
            {
                if (object.ReferenceEquals(ctrControl.GetType(), typeof(TextBox)))
                {
                    ((TextBox)ctrControl).Text = string.Empty;
                }
                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(RichTextBox)))
                {
                    ((RichTextBox)ctrControl).Text = string.Empty;
                }
                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(ComboBox)))
                {
                    ((ComboBox)ctrControl).SelectedIndex = -1;
                }
                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(CheckBox)))
                {
                    ((CheckBox)ctrControl).Checked = false;
                }
                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(RadioButton)))
                {
                    ((RadioButton)ctrControl).Checked = false;
                }
                if (ctrControl.Controls.Count > 0)
                {
                    ClearForm(ctrControl);
                }
            }
        }

Recommended Answers

All 8 Replies

Can you please let us know what the error you are getting ? because you had put only the code but no information regarding error...

I am using this code to clear a form but it is throwing an error. Any help is appreciated.

public static void ClearForm(Control parent)
        {
            foreach (Control ctrControl in parent.Controls)
            {
                if (object.ReferenceEquals(ctrControl.GetType(), typeof(TextBox)))
                {
                    ((TextBox)ctrControl).Text = string.Empty;
                }
                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(RichTextBox)))
                {
                    ((RichTextBox)ctrControl).Text = string.Empty;
                }
                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(ComboBox)))
                {
                    ((ComboBox)ctrControl).SelectedIndex = -1;
                }
                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(CheckBox)))
                {
                    ((CheckBox)ctrControl).Checked = false;
                }
                else if (object.ReferenceEquals(ctrControl.GetType(), typeof(RadioButton)))
                {
                    ((RadioButton)ctrControl).Checked = false;
                }
                if (ctrControl.Controls.Count > 0)
                {
                    ClearForm(ctrControl);
                }
            }
        }

My one question relates to the following segment:

if (ctrControl.Controls.Count > 0)
{
    ClearForm(ctrControl);
}

I'm confused as to what you're attempting to achieve here. Essentially, if I'm reading it correctly... You're creating a foreach statement at the top of your code that will cycle through (presumeably) all the controls within your form. You're creating IF checks for each type of control to handle different requirements in 'clearing' them.

Now, if I read it right, you're then saying "If the current control has subcontrols, recursively call this same procedure on the subcontrols."

I can see how this might cause a bit of an issue, particularly as you're creating a loop within a loop and utilizing the same variables for both loops which also act as controlling variables on both loops.

As indicated by rohand, however, it would be much easier to pinpoint specific issues in the code if the actual error was provided from the debugger for us to see :)

The code is fine. The only issue I see is if you are nesting it too deep. I mean a panel with some controls including another panel with controls including an additional panel, etc.

We do need to see your error. Also, I'm curious why you would choose to make this a static method. An issue could arise there if you had multiple forms hitting this same section of code at the same time. It would be better to make it a non-static call and include it in your form or, if you want to make it into a library, create an instance.

Summary, you have two issues to look at. Is it nested to deep and using an instance method instead of static. But the actual logic in the code is fine.

commented: agreed, perfectly sound recursive statement to my eyes :) +3

Instead of:

object.ReferenceEquals(ctrControl.GetType(), typeof(TextBox))

and the like, you can just do:

ctrControl.GetType() == typeof(TextBox)

Also, where you call your function recursively, I would probably make that inside an else-if rather than a second if-statement. Like so:

if (object.ReferenceEquals(ctrControl.GetType(), typeof(TextBox)))
{
  ((TextBox)ctrControl).Text = string.Empty;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(RichTextBox)))
{
  ((RichTextBox)ctrControl).Text = string.Empty;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(ComboBox)))
{
  ((ComboBox)ctrControl).SelectedIndex = -1;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(CheckBox)))
{
  ((CheckBox)ctrControl).Checked = false;
}
else if (object.ReferenceEquals(ctrControl.GetType(), typeof(RadioButton)))
{
  ((RadioButton)ctrControl).Checked = false;
}
else if (ctrControl.Controls.Count > 0)
{
  ClearForm(ctrControl);
}

Not sure if these small fixes will help fix your problem, as everyone else said it's hard to find the problem with no error message to guide us.

Changing the value of a control in code still triggers the controls change event
(e.g. TextChanged for a TextBox).
Is it due to one of these events that you are getting the error?

Also, my preference is to use the is operator to test for object types.
It reads better and makes code much tidier.
e.g.

if(ctrControl is TextBox)
{
   ((TextBox)ctrControl).Text = string.Emtpy;
}
commented: wasnt aware of the "is" syntax...nice :D +3

Thank you all very much for your answers. I apologize for not putting the error in the post. It was late last night. The error I was getting was this is an ambiguous call on the last statement that called itself. I did not try to compile it because of the error, but VS seems to have resolved the error when it was compiled.

I made some of the changes that were suggested and the code looks much cleaner.

@edepperson: The method is static because it is in a separate class called FormControl.cs that I created to handle a lot of the redundant form stuff that I have been doing. I understand the difference between static and non-static, but am I using them incorrectly? This is the first language I am learning and I am self taught. Thanks again for the help.

What you are doing is perfectly fine. But only you can answer whether this should be static or an instance. The main difference between the two is a static method is loaded into memory when the application starts and stays in memory for the life of the application. An instance is loaded into memory when the class it is in is created and then made available for garbage collection when you are finished with the class.

This is important: a static method is not thread safe. And instance method always is. However, if you have not multi-threaded your application, then either approach is ok.

Which brings us to the final factor in determining what you will want to use. If this method is called repeatedly and often, then static is better. If the method is called infrequently, then its better (memory and performance wise) to use the instance method.

commented: all good points +3
commented: Nicely said. +1

Thank you for the explanation. That helps a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.