I have a form called MainForm with controls on it say a combobox and textbox.

I have a separate class called routines which hold my methods.
I am simply trying to control the visibility etc of the controls on MainForm by a method in Routines.

The MainForm class is public.

I have tried this on MainForm.


in the Routines class
I have a method

Public void static SetUpEntryScreen(MainForm Frm1)

It is here I would expect to be able to access the controls but it doesn't work what am I missing please.

7 Years
Discussion Span
Last Post by ddanbe
Featured Replies
  • 1
    ddanbe 2,525   7 Years Ago

    Please use code tags if you post code. I think [B]foreach (Control Ctl in MainForm.Controls)[/B] should read [B]foreach (Control Ctl in Frm1.Controls)[/B], also CmbRecords should resolve to a string. Read More


There are many ways to get the control on a form say Form1 in another method. Based on your need you can use one.
1. You can declare the control definition public, which is private by default or better way would be to create a property with only get method in the form to expose that control. If you need few controls from form this is preferable.
2. You can get all the controls using Form1.Controls property. This will return collection of all the control in form. And you can loop throw it and identify the control using name property of control.


Thank you Danger,
Since I will have a lot of controls to change I though a for each loop with a switch might be the way to go.

I have tried this on the MainForm to call the method


In the Routines class

public static void SetUpEntryForm(MainForm Frm1)
            foreach (Control Ctl in MainForm.Controls)
                switch (Ctl.Name)
                case CmbRecords:
                Ctl.Visible = false;
                Ctl.Text = null;

It's obviously not right as Intellisense does not give me the Control names. Your further help will be welcome.

Edited by mike_2000_17: Fixed formatting


Please use code tags if you post code.
I think foreach (Control Ctl in MainForm.Controls) should read foreach (Control Ctl in Frm1.Controls), also CmbRecords should resolve to a string.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.