I have a class i.e., a form, in C# that can be instantiated multiple times. Each time that the form is instantiated, all of its textboxes are readonly. I also have a Menubar that has an edit_button. What should happen is; when I focus on a single form and press the edit_button on the menu bar, the readonly textboxes of that particular form becomes editable but only for that form that has the focus. The rest, which does not have focus, will not be affected. I can't give a sample code because I do not know how to go about it. Can you help? Thank you.
Asked
Active
Viewed 31 times
0
-
Unless you have class variables (static fields), that is the usual behavior with windows forms. It won't affect other form(s). Show us your code. – Hari Prasad Jun 29 '16 at 03:52
-
I don't have any code yet because I don't know how to do it. If you can point me the way, I can probably create the codes. What I have are the codes for the unrelated behavior such as, Save, Edit, make the textboxes readonly attribute to true, etc. It has nothing to do with what I am having trouble with, – Ibanez1408 Jun 29 '16 at 03:58
1 Answers
1
Given your description it seems the menubar is separate from all the multiple forms... is this an MDI application?
If so, in your edit button click handler, you can do something like this:
var activeForm = this.ActiveMdiChild; // assuming 'this' is the parent MDI form
foreach(var control in activeForm.Controls) {
// do something here (enable textboxes)
}

Nailbite
- 201
- 1
- 2
- 5
-
-
I am pretty sure that this will work the only problem is; I forgot to mention that my textboxes are in one group and in different panel so the code didn't work for me. Thank you. – Ibanez1408 Jun 29 '16 at 04:48
-
That is a separate issue related to control hierarchy. Quick-and-dirty would be to make a public method in your form that manually enables the text boxes one by one, and you can call it like (this.ActiveMdiChild as MyForm).EnableTextBoxes(). However, a more robust solution is to traverse the form's control hierarchy and get all the text boxes. – Nailbite Jun 29 '16 at 05:02
-
Here's a link showing one way of getting all children in a form (or any container): http://stackoverflow.com/questions/8595425/how-to-get-all-children-of-a-parent-control – Nailbite Jun 29 '16 at 05:04
-
I took the dirty approach and it worked! I'll study the link you provided some other time. Thanks! – Ibanez1408 Jun 29 '16 at 05:22