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 within which I placed TextBox controls. How do I disable a GroupBox control while an individual TextBox control is read-only?

Whenever I disable GroupBox , all TextBox controls inside it will also be disabled. I cannot figure out how to make them read-only.

+4
source share
4 answers

When you turn off container management (for example, GroupBox ), all of its children will also be disabled. This is how it works on Windows; this behavior cannot be changed.

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

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

 foreach (TextBox txt in myGroupBox.Controls) { txt.ReadOnly = true; } 
+11
source

This will make all text fields in the group box read-only.

 foreach(TextBox t in groupBox1.Controls) { t.ReadOnly = true; } 
+3
source

Actually, if you set the Enabled property of the group box to false. All content in it (text box, etc.) will also be disabled. But you can still do this using:

 foreach (Control ctrl in groupBox1.Controls) { if (ctrl is TextBox) { ((TextBox)ctrl).ReadOnly = true; } } 
+2
source

I think that you mean that you want to disable the group mailbox and at the same time you don’t want the text fields to appear gray ... If this is what you want to achieve, better place their text fields in the panel, and then set them to ReadOnly in the same way as for any control.

0
source

Source: https://habr.com/ru/post/1345512/


All Articles