Domanda

i have created a usercontrol, then i have a tabcontrol where in there is a tabpage that contains a 2 buttons, when button1 is clicked, it creates a new tabpage and the user control is added to its controls through

tab = new TabPage();
UserControl1 uc = new UserControl1();           
tab.Controls.Add(uc);
tab.Name = "0";
tab.Text = tab.Name;
tabControl1.TabPages.Add(tab);

now when i click the button2, it should put text in the textbox inside the usercontrol-tabpage that was just created, i implemented it with this code,

TextBox sel = (TextBox)tabControl1.TabPages["0"].Controls["textBox1"];
sel.Text = "ssss";

but it returns a runtime error, saying that it cannot find the said control, so i tried

TextBox sel = (TextBox)tabControl1.TabPages["0"].Controls[0];
sel.Text = "ssss";

but it still returns a runtime error, saying that the cast usercontrol cannot be applied to textbox. i dont know what that means.. pls help me in this.. i also tried putting in Controls[1] but it returned a runtime error, of which is a OutofBounds exception. i dont know what to do, or how to find the control inside the usercontrol in the tabpage... pls hellp

È stato utile?

Soluzione

It's a little bit unclear if the TextBox already exists in the UserControl, so I will assume it does. In that case, you have to reference the UserControl first:

UserControl1 uc1 = tabControl1.TabPages["0"].Controls[0] as UserControl1;
if (uc1 != null) {
  TextBox sel = uc1.Controls["textBox1"] as TextBox;
  if (sel != null) {
    sel.Text = "ssss";
  }
}

Altri suggerimenti

UserControl uc = NameTabPages.Controls[0] as UserControl; // it's work
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top