簡體   English   中英

如何通過單擊第一個表單中的按鈕,在第二個TabPage中創建DataGridView

[英]How can I create a DataGridView in a TabPage of 2nd from by clicking the button in the 1st Form

這是Form1

private void Btn_Create_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2(txt_LedgerName.Text);
    frm2.Show();
}

這是Form2

public partial class Form2 : Form
{
    public Form2(string message)
    {
        InitializeComponent();
        TabPage tbpg = new TabPage();
        tbpg.Text = message;
        //var dlt = tabControl1.TabPages[0];
        //dlt.Hide();
        tabControl1.TabPages.Add(tbpg);
    }
}

您可以使用此控件從Form1向Form2上的tabPage添加控件。

在Form2上創建一個函數:

// Adds a control to an existing tab page # or creates a new page if it doesn't exist 
// The value returned will be the actual page number which may not match
// the requested page number.
public int AddControl(int tabPage, Control control)
{
    TabPage tbpg;
    if (tabControl1.TabPages.Count <= tabPage)
        tbpg = tabControl1.TabPages[tabPage];
    else
    {
        tbpg = new TabPage();
        tabControl1.TabPages.Add(tbpg);
    }
    tbpg.controls.Add(control);
    return tabcontrol1.TabPages.IndexOf(tbpg);
}

暫無
暫無

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

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