Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make TextBox controls read-only when they are inside a disabled GroupBox?

I have a GroupBox control in my Windows Forms application, inside of which I have placed some TextBox controls. How can I disable the GroupBox control while making the individual TextBox controls read-only?

Whenever I disable the GroupBox, all of the TextBox controls inside of it get disabled as well. I can't figure out how to make them read-only.

like image 744
Prakash Kunwar Avatar asked Dec 05 '22 22:12

Prakash Kunwar


2 Answers

When you disable a container control (such as a GroupBox), all of its children become disabled as well. That's just how it works in Windows; it's not possible to change that behavior.

Instead, you need to set the ReadOnly property of each individual TextBox control to true. If you disable the entire GroupBox, each TextBox control it contains will be disabled as well, which overrides the state of the ReadOnly property and prevents the user from copying its contents.

Once you've fixed the section of your code that disables the GroupBox, you can use a simple foreach loop to do the dirty work of setting the property on each TextBox control:

foreach (TextBox txt in myGroupBox.Controls)
{
    txt.ReadOnly = true;
}
like image 170
Cody Gray Avatar answered Dec 24 '22 11:12

Cody Gray


This will make all the textboxes in the groupbox readonly.

foreach(TextBox t in groupBox1.Controls)
            {
                t.ReadOnly = true;
            }
like image 45
Marginean Vlad Avatar answered Dec 24 '22 10:12

Marginean Vlad