winform的TabContorl的TabPage动态添加滚动条

关键属性 AutoScrollMinSize 

 private int minWidth = 800;
 private int minHeight = 600;
 List<Form> listForm = new List<Form>();
 private Assembly assembly = Assembly.GetExecutingAssembly(); // 获取当前程序集 
        private void navMenu_MenuItemClick(string itemText, int menuIndex, int pageIndex)
        {
            string name = "";
            string title = "";
            foreach (TreeNode item in navMenu.Nodes[navMenu.SelectedIndex].Nodes)
            {
                if (item.Text == itemText)
                {
                    name = item.Name;
                    title = item.Text;
                    break;
                }
            }
            //获取屏幕除了任务栏的高宽
            int w = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Width;
            //高度减去系统标题头的高度
            int h = System.Windows.Forms.Screen.PrimaryScreen.WorkingArea.Height - 100;

            Sunny.UI.Common.ShowWaitForm();
            try
            {
    //检测当前FORM是否存在。
                Form form = listForm.SingleOrDefault(a => a.Name == name);
                if (form != null && form.IsDisposed)
                {
                    listForm.Remove(form);
                    form = null;
                }
                if (form == null)
                {
                    //新建page
                    TabPage tabPage = new TabPage();
                    tabPage.Name = name + "_tab";
                    tabPage.Tag = name;
                    tabPage.TabIndex = listForm.Count;
                    tabPage.Text = title;
                    tabPage.UseVisualStyleBackColor = true;
                    tabPage.AutoScroll = true;
                    tabPage.AutoScrollMinSize = new Size(w, h);
                    mianTabContorl.Controls.Add(tabPage);
                    //新建panel,网上听说要添加pannel。可是没用
                    //UIPanel uIPanel = new UIPanel();
                    //uIPanel.Font = new System.Drawing.Font("微软雅黑", 9F);
                    //uIPanel.Text = "";
                    //uIPanel.Dock = DockStyle.Fill;
                    //uIPanel.AutoScroll = true;
                    //uIPanel.MinimumSize = new Size(minWidth, minHeight);
                    //tabPage.Controls.Add(uIPanel);
                   //根据命名空间.类名动态创建Form对象
                    form = (Form)assembly.CreateInstance("TestProduct." + name);
                    form.MdiParent = this;
                    form.FormBorderStyle = FormBorderStyle.None;
                    //form.Parent = uIPanel;
                    form.Parent = tabPage;
                    //不可使用WindowState
                    // form.WindowState = FormWindowState.Maximized;
                    //设置最大高宽,最小高宽
                    form.MinimumSize = new Size(w, h);
                    //填充pannel
                    form.Dock = DockStyle.Fill;
                    form.Show();
                   
                    listForm.Add(form);
                    mianTabContorl.SelectedTab = tabPage;
                }
                else
                {
                    mianTabContorl.SelectedTab = GetTabPage(form.Name);
                }

            }
            catch (Exception ex)
            {
                Sunny.UI.Common.ShowErrorDialog("错误消息:" + ex.Message);
            }
            finally
            {
                Sunny.UI.Common.HideWaitForm();
            }
        }
   private TabPage GetTabPage(string name)
        {
            TabPage tb = null;
            foreach (TabPage item in mianTabContorl.TabPages)
            {
                if (item.Tag.ToString() == name)
                {
                    tb = item;
                }
            }
            return tb;
        }    

  

 

posted @ 2021-09-10 11:05  兮去  阅读(768)  评论(0编辑  收藏  举报