按钮上的选项卡控件中打开Windows窗体单击 [英] Open A Windows Form in Tab Control on Button Click

查看:103
本文介绍了按钮上的选项卡控件中打开Windows窗体单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Button上打开一个Windows窗体。在一个Tabcontrol上。

我该怎么办?

我的COde就像....... ..................................

........ .................................................. .............................

if(e.Node.Name ==Node1)

{

TabPage tbp = new TabPage();

childForm.TopLevel = false;

tabControl1.TabPages .Add(tbp);

tbp.Controls.Add(childForm);

tbp.Text =Main;

//已添加表格到标签页

childForm.WindowState = FormWindowState.Maximized;

//如果此页面已经打开............. />
if(childForm.IsHandleCreated == true)

{

tabControl1.Focus();

tabControl1.TabPages [ TBP ] .Show();

tabControl1.TabPages [tbp]。选择();



}

else

{



childForm.Show();



}

......................................... .......................................

但这是给出错误....对象未设置为Object的实例

I want to open a windows form on Button click.in a Tabcontrol.
How should I do?
My COde Is Like.........................................
.......................................................................................
if (e.Node.Name == "Node1")
{
TabPage tbp = new TabPage();
childForm.TopLevel = false;
tabControl1.TabPages.Add(tbp);
tbp.Controls.Add(childForm);
tbp.Text = "Main";
//Added form to tabpage
childForm.WindowState = FormWindowState.Maximized;
//if this page is Already Open.............
if (childForm.IsHandleCreated == true)
{
tabControl1.Focus();
tabControl1.TabPages["tbp"].Show();
tabControl1.TabPages["tbp"].Select();

}
else
{

childForm.Show();

}
................................................................................
but this is giving error....Object not set to instance of Object

推荐答案

在按钮单击事件中,您执行以下操作:

In the button click event, you do:
Form2 frm = new Form2();  //Or whatever your form is called
frm.Show(this);



或(如果你想以模态方式打开它):


or (if you want to open it modally):

Form2 frm = new Form2();  //Or whatever your form is called
frm.ShowDialog(this);


您还需要在tabpages的控件集合中添加表单对象,如下所示:



You also need to add form object in tabpages'' control collection as below:

private void button1_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2();
    frm.TopLevel = false;
    tabPage1.Controls.Add(frm);
    frm.Dock = DockStyle.Fill;
    frm.Show();
}


检查这个

选项卡式MDI子窗体 [ ^ ]


这篇关于按钮上的选项卡控件中打开Windows窗体单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆