adding tabpage to another form

R

RobcPettit

Hi, I have two forms. From form1(frmMain) I want to add tabpages to
Form2. In form1 I have:
frmMain tbe = new frmMain();
Thread thread = new Thread(new
ThreadStart(tbe.AddTab));
thread.IsBackground = true;
thread.Start();

In Form2 I have:
public delegate void mydata(String e);

public void AddTab()
{
if (this.InvokeRequired)
{
this.BeginInvoke(new mydata(TestMethod),"somestring");
}

else
{
TestMethod("rob");
}

}
private void TestMethod(String mystring)
{
String TabName = "rob";
TabPage NewTab = new TabPage(TabName);
tabControl1.TabPages.Add(NewTab);
}

It biulds ok, and runs ok, but doesnt show the tabs on Form2.
The idea is that both forms are open, from form one I scan through a
list which has been added to a treeview,(this all works). Then click
the item I want, creating a new tab in form2 each time. Then close
form1 and work with form2.
Regards Robert
 
N

Nicholas Paldino [.NET/C# MVP]

Rob,

You are never showing the new form that you create. I'm assuming that
you have already created an instance of frmMain and shown it. If so, you
have to pass a delegate pointing to the AddTab method on THAT reference.
 
R

RobcPettit

Thankyou for your reply. Both forms are showing. Do you meen the
reference on the form that I want to add the tab to. If so do I have
to refer to form2 even though the code is in form2.

Regards Robert
 
N

Nicholas Paldino [.NET/C# MVP]

Rob,

Yes. When you create the new Form2, it is a separate instance, and you
are telling the delegate to execute the method on THAT instance. So you
have to keep the instance of Form2 that you showed previously and pass that
to the delegate which gets passed to the thread.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top