如何将按钮从代码添加到标签页?

时间:2019-08-11 15:29:28

标签: c# winforms

我有一个私有的void AddButtons(),仅在加载表单时才尝试将其添加到tabPage1。

我尝试过:

   public frmMain()
    {
        TabControl tabControl = new TabControl();
        if (tabControl.SelectedTab == tabPage1)
        {
            AddButtons();
        }
    }
  private void tabControl_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (tabControl.SelectedTab == tabControl.TabPages[1])
        {
            AddButtons();
        }
    }

    private void tabControl_Selected(object sender, TabControlEventArgs e)
    {
        if (tabControl.SelectedTab == tabControl.TabPages[1])
        {
            AddButtons();
        }
    }
   private void tabControl_Selected(object sender, TabControlEventArgs e)
    {
        if (e.TabPage == tabPage1)
        {
            AddButtons();
        }
    }

第一个在所有选项卡上加载按钮。接下来的3个没有加载任何内容。

2 个答案:

答案 0 :(得分:0)

要检查的第一个问题是在C#中数组是从零开始的。这意味着 tabPage1 可能是tabControl.TabPages[0],而不是您尝试的内容。

为了正确回答您的问题,我们应该能够看到 form1.AddButtons()的代码。有趣的是,将按钮添加到所有选项卡页面。你确定吗。如果您犯了与上述相同的错误,则会在第二个标签页中看到按钮。

已编辑Controls.Add(pnlButtons);->使用此语句,您将带有按钮的面板添加到表单,而不是 tabPage1

已编辑:由于似乎没有任何效果,因此我创建了一个像您自己的项目。

首先,我创建了一个 MainWindow

public class MainWindow: Form {
    public MainWindow()
    {
        this.Build();
    }

    TabControl BuildTabs()
    {
        this.Tabs = new TabControl { Dock = DockStyle.Fill };
        this.Tabs.SelectedIndexChanged += (o, evt) => this.OnTabChanged();
        this.Tabs.TabPages.Add( new TabPage{ Text = "Tab1" } );
        this.Tabs.TabPages.Add( new TabPage{ Text = "Tab2" } );

        return this.Tabs;
    }

    void Build()
    {
        this.MinimumSize = new Size( 580, 460 );
        this.Text = "Programmatic creation";
        this.Controls.Add( this.BuildTabs() );
    }

    public TabControl Tabs {
        get; private set;
    }
}

MainWindow AddButtons()方法与您编写的完全相同,我不想更改任何内容(除了上面提到的行)。但是请考虑到,使用 TableLayoutPanel 可以在没有绝对定位的情况下获得相同的结果(绝对定位是不可取的,因为它不适用于不同的窗口大小)。

因此,现在,您只需要事件处理程序:

    void AddButtons()
    {
        // many things
    }

    void OnButtonClicked()
    {
        MessageBox.Show( "Button clicked!" );
    }

    void OnTabChanged()
    {
        if ( this.Tabs.SelectedIndex == 1 ) {
            this.AddButtons();
        }
    }

一切正常。我怀疑您有很多微小的错误会导致您的项目无法正常工作。

整个代码如下:

public class MainWindow: Form {
    public MainWindow()
    {
        this.Build();
    }

    void AddButtons()
    {
        var pnlButtons = new Panel();
        pnlButtons.Dock = DockStyle.Fill;
        pnlButtons.Location = new Point(370, 59);
        pnlButtons.BackColor = Color.White;

        int xPos = 0;
        int yPos = 0;
        // assign number of buttons = 27
        var btnArray = new System.Windows.Forms.Button[27];

        // Create (27) Buttons:
        int n = 0;
        while (n < 27)
        {
            btnArray[n] = new System.Windows.Forms.Button();
            btnArray[n].Tag = n + 1; // Tag of button
            btnArray[n].Width = 28; // Width of button
            btnArray[n].Height = 24; // Height of button

            // Location of button:
            btnArray[n].Left = xPos;
            btnArray[n].Top = yPos;
            // Add buttons to a Panel:
            //To change Panel Buttons from Vertical to Horizontal move the multiplier from pnlButtons.Height to pnlButtons.Width
            pnlButtons.Height = btnArray[n].Height * 27;
            pnlButtons.Width = btnArray[n].Width;
            pnlButtons.Controls.Add(btnArray[n]); // Let panel hold the Buttons
            yPos = yPos + btnArray[n].Height; //For Vertical buttons
            //xPos = xPos + btnArray[n].Width; // For Horizontal buttons

            // Write English Character:

            char[] Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 
            'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 
            'W', 'X', 'Y', 'Z', '*'};
            btnArray[n].Text = Alphabet[n].ToString();
            this.Tabs.TabPages[ 1 ].Controls.Add(pnlButtons);

            // the Event of click Button
            btnArray[n].Click += (o, evt) => this.OnButtonClicked();
            n++;
        }
    }

    TabControl BuildTabs()
    {
        this.Tabs = new TabControl { Dock = DockStyle.Fill };
        this.Tabs.SelectedIndexChanged += (o, evt) => this.OnTabChanged();
        this.Tabs.TabPages.Add( new TabPage{ Text = "Tab1" } );
        this.Tabs.TabPages.Add( new TabPage{ Text = "Tab2" } );

        return this.Tabs;
    }

    void Build()
    {
        this.MinimumSize = new Size( 580, 460 );
        this.Text = "Programmatic creation";
        this.Controls.Add( this.BuildTabs() );
    }

    void OnButtonClicked()
    {
        MessageBox.Show( "Button clicked!" );
    }

    void OnTabChanged()
    {
        if ( this.Tabs.SelectedIndex == 1 ) {
            this.AddButtons();
        }
    }

    public TabControl Tabs {
        get; private set;
    }
}

我终于决定修改您的 AddButtons(),因此它也可以更好地工作:

    void AddButtons()
    {
        TabPage tabPage1 = this.Tabs.TabPages[ 1 ];

        if ( tabPage1.Controls.Count == 0 ) {
            var pnlButtons = new TableLayoutPanel();
            pnlButtons.Dock = DockStyle.Fill;
            pnlButtons.AutoScroll = true;

            // assign number of buttons = 27
            var btnArray = new Button[27];

            // Create (27) Buttons:
            int n = 0;
            while( n < 26 ) {
                btnArray[n] = new Button{ Tag = n + 1 };

                // Add buttons to a Panel:
                //To change Panel Buttons from Vertical to Horizontal move the multiplier from pnlButtons.Height to pnlButtons.Width
                pnlButtons.Controls.Add( btnArray[n] ); // Let panel hold the Buttons

                // Write English Character:
                btnArray[n].Text = char.ToString( (char) ( 'a' + n ) );

                // the Event of click Button
                btnArray[n].Click += (o, evt) => this.OnButtonClicked();

                ++n;
            }

            btnArray[n] = new Button {  Tag = n + 1 };
            btnArray[n].Text = char.ToString( '*' );
            pnlButtons.Controls.Add( btnArray[n] );
            tabPage1.Controls.Add( pnlButtons );
        }
    }

希望这会有所帮助。

答案 1 :(得分:0)

这里是AddButtons的副本。几年前,我得到了我正在开发的VB.Net程序,并在第一次使用C#进行转换时对其进行了转换。最后单击按钮仅更改用于填充DataGridView的SQL选择语句。

     private void AddButtons()
    {
        Panel pnlButtons = new Panel();
        pnlButtons.Location = new System.Drawing.Point(370, 59);
        pnlButtons.BackColor = Color.White;

        int xPos = 0;
        int yPos = 0;
        // assign number of buttons = 27
        btnArray = new System.Windows.Forms.Button[27];
        // Create (27) Buttons:
        for (int i = 0; i < 27; i++)
        {
            // Initialize one variable
            btnArray[i] = new System.Windows.Forms.Button();
        }
        int n = 0;
        while (n < 27)
        {
            btnArray[n].Tag = n + 1; // Tag of button
            btnArray[n].Width = 28; // Width of button
            btnArray[n].Height = 24; // Height of button

            // Location of button:
            btnArray[n].Left = xPos;
            btnArray[n].Top = yPos;
            // Add buttons to a Panel:
            //To change Panel Buttons from Vertical to Horizontal move the multiplier from pnlButtons.Height to pnlButtons.Width
            pnlButtons.Height = btnArray[n].Height * 27;
            pnlButtons.Width = btnArray[n].Width;
            pnlButtons.Controls.Add(btnArray[n]); // Let panel hold the Buttons
            yPos = yPos + btnArray[n].Height; //For Vertical buttons
            //xPos = xPos + btnArray[n].Width; // For Horizontal buttons

            // Write English Character:

            char[] Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 
            'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 
            'W', 'X', 'Y', 'Z', '*'};
            btnArray[n].Text = Alphabet[n].ToString();
            Controls.Add(pnlButtons);
            // the Event of click Button
            btnArray[n].Click += new System.EventHandler(ClickButton);
            n++;
        }

    }