user3787313
user3787313

Reputation: 11

how to change size of controls in different panels or forms (winform) c#

There is some checkboxes in my forms and some of them in panels in a specific form(two different matter).
I placed a button that when user click on that, the size of all checkboxes should change(in all panels and forms).

And another question is that,how can I found that what is the type of controls in my app Programmatically?

Thanks for your help.
I placed a button and added a event handler.
and for founding the the type of control,I used the name of control Here is my try:

            foreach (Control ctrl in this.Controls)
                if (ctrl.Name.Contains("combo"))  
                    checkbox1.SetSize = new Size(40,40);

Upvotes: 1

Views: 295

Answers (3)

ntl
ntl

Reputation: 1299

Change according to comment:

You can loop through all controls to find all checkboxes:

    private void ProcessControls(Control containerControl)
    {
        foreach (Control control in containerControl.Controls)
        {
            if (control is CheckBox)
            {
                ChangeCheckBoxProperties((CheckBox)control);
            }
            else
            {
                ProcessControls(control);
            }
        }
    }

    private void ChangeCheckBoxProperties(CheckBox cb)
    {
        // ...
    }

You can call this method for your main form.

On mind, you should think about your aproach in general, because if you use such loops, it seems that something is not right in your solution.

Upvotes: 0

Sayse
Sayse

Reputation: 43330

The simplest way would be to use this extension method to loop over all checkboxes

foreach(var checkBox in this.GetAll<CheckBox>())
    checkBox.Size = new Size(40,40);

Without this you would have to loop over the panels seperately

foreach(var panel in this.Controls.OfType<Panel>())
    foreach(var checkbox in panel.Controls.OfType<CheckBox>())
        //set size in panel

foreach(var checkbox in this.Controls.OfType<CheckBox>())
    //set size in form

Upvotes: 0

christian.s
christian.s

Reputation: 447

If you plan to use one Function to handle multiple OnClick - events, you can get the origin out of the sender (the sender is a given parameter for the handler implementation)

(If this was the question, you should clearify what exactly you want to do and show your relevant code)

Upvotes: 1

Related Questions