Can I access a user form object programmatically?
-
I did this in VB and would like to do this in C# now. UserForm1.Controls("chkbx_ManProp_" & CStr(j)).Value = True any ideas how to convert this to C# in a windows form application? Thanks
-
I did this in VB and would like to do this in C# now. UserForm1.Controls("chkbx_ManProp_" & CStr(j)).Value = True any ideas how to convert this to C# in a windows form application? Thanks
Crazy VB code, so you want to access the property of a control on another form? If so, add a method to the forms class that will modify the control. Then you can call the method.
My current favourite word is: Waffle Cheese is still good though.
-
Crazy VB code, so you want to access the property of a control on another form? If so, add a method to the forms class that will modify the control. Then you can call the method.
My current favourite word is: Waffle Cheese is still good though.
I wrote the VB app in a short amount of time, so that snippet was pretty much done on the fly ;o the property of the control(s) that I want to access is within the same form. Unfortunately, there isn't a direct correlation of VB and C# methods so I'm not sure where to look. I was thinking about something like this: start a loop Application.getControlbyName("object_name_as_string" & integer_variable).value = false integer_variable++; end loop in essance what this should do is programmatically loop through all the controls with similar naming conventions, and set its member value to false. Any suggestions / alternate ideas on how to accomplish this in c#??? Thanks in advance Humble.
-
I wrote the VB app in a short amount of time, so that snippet was pretty much done on the fly ;o the property of the control(s) that I want to access is within the same form. Unfortunately, there isn't a direct correlation of VB and C# methods so I'm not sure where to look. I was thinking about something like this: start a loop Application.getControlbyName("object_name_as_string" & integer_variable).value = false integer_variable++; end loop in essance what this should do is programmatically loop through all the controls with similar naming conventions, and set its member value to false. Any suggestions / alternate ideas on how to accomplish this in c#??? Thanks in advance Humble.
ControlName.PropertyName = NewValue;
Not sure why you want to loop through the control list when the above template should work within the same form. Of course, you could do the looping but it takes a bit more skill and would look something like the code below. I would stick with The Undefeated's advice.
public static Control FindControl(Control root, string nameToFind) { Control retval = null; foreach (Control c in root.Controls) { if (c.Name == NameToFind) { retval = c; break; } retval = FindControl(c, nameToFind); if (retval != null) { break; } } return retval; }
-
ControlName.PropertyName = NewValue;
Not sure why you want to loop through the control list when the above template should work within the same form. Of course, you could do the looping but it takes a bit more skill and would look something like the code below. I would stick with The Undefeated's advice.
public static Control FindControl(Control root, string nameToFind) { Control retval = null; foreach (Control c in root.Controls) { if (c.Name == NameToFind) { retval = c; break; } retval = FindControl(c, nameToFind); if (retval != null) { break; } } return retval; }
Heh. Agreed that it's easier just to assign a value to a control. However, I've got 20 controls that need to be assigned the same value. I declared the control names with the express notion that I would be able to loop through all of them and assign with the same value. I guess I can modularize it and put it in a method, but I'm slightly surprised I"m not able to do what I could do with VBA code! :P
-
Heh. Agreed that it's easier just to assign a value to a control. However, I've got 20 controls that need to be assigned the same value. I declared the control names with the express notion that I would be able to loop through all of them and assign with the same value. I guess I can modularize it and put it in a method, but I'm slightly surprised I"m not able to do what I could do with VBA code! :P
That is because it is inefficient. There is little reason to loop through all controls everytime you want to update a specific one. VB tends to save lines of code not cpu cycles.