簡體   English   中英

如何將按鈕從代碼添加到標簽頁?

[英]How to add buttons from code to a tabpage?

我有一個私有的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個沒有加載任何內容。

要檢查的第一個問題是在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 );
        }
    }

希望這可以幫助。

這是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++;
        }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM