C# Clear Controls

Hello,

I have written a class file to clear the controls on the form. This works fine for win form controls.

I have written a custom control text box. This code does not clear this text box. How do i resolve this ?

 public static void Clearcontrol(Control parent)
        {
            foreach (Control subcontrol in parent.Controls)
            {
                switch (subcontrol.GetType().ToString().Replace("System.Windows.Forms.", ""))
                {
                    case "TextBox":
                        var textBox = (TextBox)subcontrol;
                        textBox.Text = null;
                        break;

                    case "ComboBox":
                        var comboBox = (ComboBox)subcontrol;
                        if (comboBox.Items.Count > 0)
                            comboBox.SelectedIndex = 0;
                        break;

                    case "CheckBox":
                        var checkBox = (CheckBox)subcontrol;
                        checkBox.Checked = false;
                        break;

                    case "ListBox":
                        var listBox = (ListBox)subcontrol;
                        listBox.Items.Clear();
                        break;

                    case "RadioButton":
                        var radioButton = (RadioButton)subcontrol;
                        radioButton.Checked = false;
                        break;

                    case  "DataGridView":
                        var dgridview = (DataGridView) subcontrol;
                        dgridview.DataSource = null;
                        break;

                    case "CheckedListBox":
                        var chklstbox = (CheckedListBox)subcontrol;
                        for (var i = 0; i < chklstbox.Items.Count; i++)
                        {
                            chklstbox.SetItemCheckState(i, CheckState.Unchecked);
                        }
                        break;
                }
                if (subcontrol.HasChildren)
                {
                    Clearcontrol(subcontrol);
                }
            }
        }

Open in new window

GRChandrashekarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Your're using the GetType method. This will return the immediate type of your object. If you have derived from another control, then you will not see that base type. I think that is your issue, but without knowing more about your custom control that is just a guess. I would suggest abandoning the switch in favor of an if that uses the is keyword. It will check the inheritance hierarchy for a compatible type.

e.g.

foreach (Control subcontrol in parent.Controls)
{
        if (subcontrol is TextBox)
        {
            var textBox = (TextBox)subcontrol;
            textBox.Text = null;
        }
        else if (subcontrol is ComboBox)
        {
            var comboBox = (ComboBox)subcontrol;
            if (comboBox.Items.Count > 0)
                comboBox.SelectedIndex = 0;
        }
        else if (subcontrol is CheckBox)
            var checkBox = (CheckBox)subcontrol;
            checkBox.Checked = false;
        }
        else if (subcontrol is ListBox)
        {
            var listBox = (ListBox)subcontrol;
            listBox.Items.Clear();
        }
        else if (subcontrol is RadioButton)
        {
            var radioButton = (RadioButton)subcontrol;
            radioButton.Checked = false;
        }
        else if (subcontrol is DataGridView)
        {
            var dgridview = (DataGridView) subcontrol;
            dgridview.DataSource = null;
        }
        else if (subcontrol is CheckedListBox)
        {
            var chklstbox = (CheckedListBox)subcontrol;
            for (var i = 0; i < chklstbox.Items.Count; i++)
            {
                chklstbox.SetItemCheckState(i, CheckState.Unchecked);
            }
        }
    }

    if (subcontrol.HasChildren)
    {
        Clearcontrol(subcontrol);
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GRChandrashekarAuthor Commented:
Your assumption is right. I tried with your code.

This line on wards, it all shows errors.
 var checkBox = (CheckBox)subcontrol;

Open in new window


Error: Invalid Embedded Statement

Also for "is" re sharper says "Type check and direct cast can be replaced with try cast and check for null"
0
käµfm³d 👽Commented:
Normally I'd agree with what ReSharper is saying, but I don't think it would save you anything here. If I understand what ReSharper is trying to say, then it is suggesting you use the as keyword instead:

var textBox = subcontrol as TextBox;

if (textBox != null)
{
    textBox.Text = null;
}

Open in new window


You'd have to do that for each control type that you are checking for. You can certainly do this, but it's going to make your code a tad more bulky. That is why I went with is instead.

Are you sure they are errors? Perhaps they are just warnings.
0
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

GRChandrashekarAuthor Commented:
for check box it says
only assignment, call, increment, decrement and new object expressions can be used as a statement.
This an ERROR
0
käµfm³d 👽Commented:
The CheckBox code is missing an opening brace:

...

else if (subcontrol is CheckBox)
{
    var checkBox = (CheckBox)subcontrol;
    checkBox.Checked = false;
}

...

Open in new window

0
GRChandrashekarAuthor Commented:
else if (subcontrol is CheckedListBox)
Now this says expression is always false and rest of the code of this loop is heuristically unreachable
0
käµfm³d 👽Commented:
Are you certain that is the line of code raising the error? The logic works fine for me:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication21
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void TestControl(Control parent)
        {
            if (parent is CheckedListBox)
            {
                MessageBox.Show("Control is a CheckedListBox");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TestControl(this.checkedListBox1);
        }
    }
}

Open in new window


Screenshot
0
GRChandrashekarAuthor Commented:
Yes Tried again. This portion of code does not work

else if (subcontrol is CheckedListBox)
        {
            var chklstbox = (CheckedListBox)subcontrol;
            for (var i = 0; i < chklstbox.Items.Count; i++)
            {
                chklstbox.SetItemCheckState(i, CheckState.Unchecked);
            }
        }
    }

Open in new window

0
käµfm³d 👽Commented:
Move the CheckListBox logic above the ListBox check. A CheckedListBox is an instance of a ListBox, hence the error.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.