c# - make a tab from a user control -


i'm having problems. lot of errors , annoying me when i'm trying add tabs user control. here code

public form1 f1 { get; private set; }  private void button1_click(object sender, eventargs e) {     tabpage tp = new tabpage { };     tp.text = "newtab";     tp.controls.add(new b());     f1.tabcontrol1.tabpages.add(tp);  //>>> errors here } 

image more details: code no showing errors

run-time errors

your code isn't assigning f1 why getting null reference exception @ run time.

depending on architecture of project ...

a. if usercontrol instantiated in parent form code behind inject f1 constructor of usercontrol:

private readonly myform _f1;  public b(myform f1){     initializecomponents();      _f1 = f1; }  private void button1_click(object sender, eventargs e) {     tabpage tp = new tabpage { };     tp.text = "newtab";     tp.controls.add(new b());      //  assumes 'tabcontrol1' exists publicly accessible control     _f1.tabcontrol1.tabpages.add(tp); } 

b. if using m-v-p then, using code have, presenter can assign f1 variable @ initialization make setter public or internal.


Comments