将TabPage添加到tabControl时不显示 [英] TabPages do not display when added to the tabControl

查看:79
本文介绍了将TabPage添加到tabControl时不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我对tabControl有问题.我有一个包含MainForm并带有tabControl的表单.有一个名为Main Menu的TabPage,它显示一个名为MainMenu的UserControl.在MainMenu上,有几个按钮应该将一个新的TabPage添加到MainForm上的tabControl上,或者如果已经存在则显示TabPage.
MainForm上的tabControl的修饰符设置为public,并且MainForm使用以下方法在MainMenu上声明: MainForm MainForm = new MainForm();

如果将按钮和代码直接放置在TapPage的主菜单上,则下面的代码可以正常工作.

Hi,

I have a problem with a tabControl. I have a form that consists of MainForm with a tabControl on it. There is a TabPage called Main Menu that displays a UserControl called MainMenu. On MainMenu there are several buttons that should either add a new TabPage to the tabControl on MainForm or display the TabPage if it already exists.

The tabControl on MainForm has its modifier set to public and MainForm is declared on MainMenu using: MainForm MainForm = new MainForm();

I have the following code that works fine if the button and code are placed directly onto the TapPage, Main Menu.

public void TabButtons(object sender, EventArgs e)
  {
   string TabName;
   bool Exists;
   Exists = false;

   Button clickedButton = (Button)sender; //Get the name of the button that was clicked
   TabName = clickedButton.Text; //Save the button text to use later

   TabControl.TabPageCollection TabCollection = MainForm.tabControl.TabPages;

   foreach (TabPage Page in TabCollection) //loop through the TabCollection to check if the tab already exists
   {
    if (Page.Text == TabName) //If the TabPage already exists select the TabPage
    {
     MainForm.tabControl.SelectTab(Page);
     Exists = true;
    }
   }
   if (Exists == false) //If the TabPage oes not exist create it
   {
    TabPage newPage = new TabPage(TabName);    //Create new tab and name it
    MainForm.tabControl.TabPages.Add(newPage);   //add the new tab to the tabcontrol
    MainForm.tabControl.SelectTab(newPage);    //select the new tab
   }
  }



问题是,当我将其移至用户控件时,进入代码,可以看到正在创建TapPage,因为它已在TabCollection中列出,但是TapPage从未显示在tabControl上.

我遇到的问题是,从未在GUI上创建过选项卡头(或者未更新GUI),所以我无法手动或以编程方式选择它.

因此,基本上会发生什么情况,即使创建了TabPage并将其添加到TabCollection上,屏幕看起来也好像从未单击过userControl上的按钮.

希望有人能帮助我.

谢谢
Darren



The problem is when I move it to the user control, stepping into the code I can see the TapPage is being created as it is listed in the TabCollection but the TapPage never shows up on the tabControl.

The issue I have is that the tab header is never created on the GUI (or the GUI is not updated), so I am not able to select it either manually or programically.

So basically what happens is the screen looks as if the buttons on the userControl have never been clicked, even though the TabPage is created and added to the TabCollection.

Hope some one here can help me.

Thanks
Darren

推荐答案

您还需要将该页面添加到控件列表中
you need to add the page to the controls list aswell
TabPage pageToAdd = new TabPage();
// set page properties here
// ....
MainForm.Controls.Add(pageToAdd)



来自msdn
的示例



example from msdn

<br />
public class Form1 : Form<br />
{<br />
    private TabControl tabControl1;<br />
    private TabPage tabPage1;<br />
<br />
    public Form1()<br />
    {<br />
        this.tabControl1 = new TabControl();<br />
        this.tabPage1 = new TabPage();<br />
<br />
        // Gets the controls collection for tabControl1.<br />
        // Adds the tabPage1 to this collection.<br />
        this.tabControl1.TabPages.Add(tabPage1);<br />
<br />
        this.tabControl1.Location = new Point(25, 25);<br />
        this.tabControl1.Size = new Size(250, 250);<br />
<br />
        this.ClientSize = new Size(300, 300);<br />
        this.Controls.Add(tabControl1);<br />
    }<br />
<br />
    static void Main() <br />
    {<br />
        Application.Run(new Form1());<br />
    }<br />
}<br />


我设法通过在MainForm上声明公共静态变量来解决此问题:

public static MainForm staticVar = null;


并在MainForm上使用了它:

staticVar = this;

创建我的用户控件时

并且:

I''ve managed to fix this by declairing a public static variable on MainForm:

public static MainForm staticVar = null;


and used this on MainForm:

staticVar = this;

when creating my user control

And:

<br />
MainForm.staticVar.tabControl.Controls.Add(newPage);<br />
MainForm.staticVar.tabControl.SelectTab(newPage);<br />



添加我的标签,所有操作现在都可以按预期进行了.

谢谢



to add my tabs, it all works as expected now.

Thanks


这篇关于将TabPage添加到tabControl时不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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