c# - Usercontrol set TextBox text for other usercontrol -
i have 2 diffrent usercontrol classes. i'm trying set textbox text 1 usercontrol usercontrol. of property working set doesn't anything. how solve this? i've posted related code snippets below.
incidentcategorysearchcontrol.cs
public partial class incidentcategorysearchcontrol : usercontrol { private void datagridview_celldoubleclick(object sender, datagridviewcelleventargs e) { incidentcategorychange inccatchange = new incidentcategorychange(); //textbox1.text = inccatchange.textboxcategory; // works inccatchange.textboxcategory="test"; // doesn't work } }
incidentcategorychange.cs
public partial class incidentcategorychange : usercontrol { public incidentcategorychange() { initializecomponent(); } public string textboxcategory { { homecoming incidentcategorytextbox.text; } set { incidentcategorytextbox.text = value; } } }
the value default value, because line before have constructed incidentcategorychange
. both getter , setter aren't working.
to communicate between user controls 1 possibility provide somehow instance of one, textbox
(or other property) want get/set, another.
this can done having instance saved somewhere, example, using static
property of same class (this require one instance of user command exists, it's simple demonstrate idea):
public partial class incidentcategorychange : usercontrol { public static incidentcategorychange instance {get; private set;} public incidentcategorychange() { initializecomponent(); instance = this; } public string textboxcategory { { homecoming incidentcategorytextbox.text; } set { incidentcategorytextbox.text = value; } } }
now can do
incidentcategory.instance.textboxcategory = "test";
another solution utilize events (see this question). incidentcategorychange
subscribe event categoryvaluechanged(string)
of other user command , in event handler can alter value of textbox
.
c# user-controls
No comments:
Post a Comment