Change value of control on a form from class (C#) -
this should quite simple - not sure problem is.
i have c# class (public.cs) , windows form (form1.cs). through function in public.cs, want value of control on form1 (without having use object parameters).
// code appears in public.cs public string myfunction(int num_val) { if (chk_num.checked == true) { // here... } }
the issue class cannot find control on form. there way must reference in c#?
thank you.
i suggest exposing checked
property via specific property on form1 (perhaps more meaningful name). hide implementation details (i.e. control structure) of form1 it's caller , instead expose logic required other consumers job
for example:
public bool isnumberrequested { { return chk_num.checked; } }
or alternatively, if still want access control directly, designer can select control , change it's modifier
property public (or else) enabling access control object using code wrote above.
edit: (response based on comment)
public.cs still need reference form1 , call isnumberrequested
property of object.
// public.cs public class public { private form1 _ui; public public(form1 ui) { _ui = ui }; public string myfunction(int num_val) { if (_ui.isnumberrequested) { // stuff } // else, default stuff } }
alternatively, pass form parameter myfunction
rather using instance variable.
Comments
Post a Comment