c# - How does Custom User Control send data to Hosting Form -


i have implemented custom user control using c#. control has button. control added winform form1 has list box.

the problem : how can add value in list box when click button in user control?

you have raise event user control when click button, , catch in form1.

you can :

user control

public event eventhandler clickfromusercontrol;  private void click_event_on_the_button() {     //null check makes sure main page attached event     if (this.clickfromusercontrol != null)        this.clickfromusercontrol(new object(), new eventargs()); } 

form1

public myapp() {      //usercontrol = control clickfromusercontrol event      this.usercontrol.clickfromusercontrol += new eventhandler(myeventhandlerfunction_clickfromusercontrol); }  public void myeventhandlerfunction_clickfromusercontrol(object sender, eventargs e) {          //add value here } 

you can pass more parameters event :

this.clickfromusercontrol(new object(), new eventargs(), param1, param2); 

and in form :

public void myeventhandlerfunction_clickfromusercontrol(object sender, eventargs e, string param 1, string param2) {          //add value here } 

or

you can create properties on user control :

public string value {   { return textbox1.text; }   set { textbox1.text = value; } } 

and access sender in form's event.

you can check out how build event : msdn


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -