繁体   English   中英

C#文本在tabcontrol的选项卡中不会更改

[英]C# The text does not change in tabpage of tabcontrol

我有一些选项卡处于打开状态,并且每个选项卡页面都处于监禁状态。 我想通过单击表单中的按钮在当前选项卡页面中打开另一个,当前选项卡的文本将更改。 我使用下面的代码,以便如果按下按钮,则当前标签页的名称将更改:

private void buttonNewForm_Click(object sender, EventArgs e)
        {
            newForm childForm = new newForm(tbc1);
            //TopLevel for form is set to false
            childForm.TopLevel = false;
            //select current TabPage
            int curr = tbc1.SelectedIndex;
            TabPage tbp = tbc1.TabPages[curr];
            tbc1.TabPages[curr].Text = "name of new form";
            tbp.Controls.Add(childForm);
            //Added form to tabpage
            childForm.WindowState = FormWindowState.Maximized;
            childForm.Show();
            Refresh();

        }

在我将代码放入主要形式之前,它一直工作良好,这样可以防止我忽悠选项卡的通过:

protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
                return cp;
            }
        } 

如何在不删除防止闪烁的代码的情况下再次工作?

为防止闪烁,请尝试此

SuspendLayout();
// Add control and make size changes
childForm.Show();
Refresh();
ResumeLayout();

您应该考虑使用UserControl而不是表单来嵌入选项卡页面。

最后我解决了这个问题。 相反, refresh()我不得不使用tbc1.refresh()

tbc1是我使用的tabcontrol的名称。

在将所有表单替换为主表单之外的usecontrols之后,我没有了,我需要对以上代码进行一些更改:

private void buttonNewForm_Click(object sender, EventArgs e)
        {
            newForm childForm = new newForm(tbc1);
            //select current TabPage
            int curr = tbc1.SelectedIndex;
            TabPage tbp = tbc1.TabPages[curr];
            tbc1.TabPages[curr].Text = "name of new form";
            tbp.Focus();
            tbp.Controls.Clear();
            tbp.Controls.Add(childForm);
            tbc1.Refresh();

        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM