Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Control to Form Changes Visible Property

Status
Not open for further replies.

DaveInIowa

Programmer
Dec 2, 2003
576
US
I'm wondering if anyone can explain this behavior. The example uses 2 controls on a form, comboBox1 & textBox1. Both of their Visible properties are true in the designer. When I step through the code I can see where the Visible property of each control changes to false after they are added to the form's Controls property.

Even though after the execution of the InitializeComponent() method their Visible property is false, they still will show on the form unless I set their property explicitly.

Code:
        public Form1()
        {
            // If you step through InitializeComponent(), you'll see that 
            // this.Controls.Add(this.textBox1) and
            // this.Controls.Add(this.comboBox1)
            // changes their .Visible property from true to false
            InitializeComponent();

            // The .Visible property is false but the combo box will still be displayed UNLESS...
            bool cbxVisible = comboBox1.Visible;

            // ...I set the .Visible property, then it will ACTUALLY hide the combo box
            comboBox1.Visible = cbxVisible;

            bool txtVisible = textBox1.Visible; // Is false but will still be displayed

            textBox1.Text = $"ComboBox visible: {cbxVisible}, TextBox visible: {txtVisible}";
        }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top