簡體   English   中英

調整 TabPage、控件和表單的大小

[英]Resize TabPage,Controls and Form

我想調整標簽頁的大小,它的內部控件是 dataGridview,最后調整包含它們的表單的大小。

我已經實現了 tabpages 的拖動功能。現在我想根據 DatagridviewRows 增加 tabPage 大小。

if(dgv.Rows.count<=15)
  Resize tabPage to show  data to show 'n' No. Of Rows
else if(dgv.Rows.count>15)
  Resize to show 15 Rows data then Scroll bar.

我已經嘗試設置 gridview 的 Dock 和 Anchor 屬性。但只填充了標簽頁。我希望標簽頁隨着行數的增加而調整大小,並最終調整包含它的表單的大小。

請幫忙。

我認為更好的方法是根據您的表單調整大小更改其他控件的大小。

    private void Form1_Resize(object sender, EventArgs e) //form resize event
    {
      grdView1.SetBounds(Left,Top, this.Width-10,this.Height-10);

      grdView1.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
      grdView1.Columns[0].FillWeight = 45;

      //same for other columns according to your requirments.

    }

並根據表單大小設置標簽頁的大小。

我使用了下面的代碼並且它起作用了。 我將 datagridview 保留在 splitcontainer.Made 的 Splitcontainer 的停靠屬性中以填充並將第二個面板保持為固定面板。根據行數和面板高度計算高度並更新表單高度。這樣它就起作用了。在此處輸入圖片說明

    int height = this.Height;
    CalculateFormHeight(ref height);
    this.Size = new Size(this.Width, height);

    private void CalculateFormHeight(ref int height)
    {
        if (dataGridViewToDisplay != null && dataGridViewToDisplay.Rows != null)
        {
            if (dataGridViewToDisplay.Rows.Count >= 15)
            {
                height = dataGridViewToDisplay.Rows[0].Height * 18 + splitContainer1.Panel2.Height;
            }
            else if (dataGridViewToDisplay.Rows.Count < 15)
            {
                height = dataGridViewToDisplay.Rows[0].Height * (dataGridViewToDisplay.Rows.Count + 3) + splitContainer1.Panel2.Height;
            }
        }
    }

要做到這一點沒有太大問題,您必須從選項卡中刪除表單,然后重新放置

 private void frmMaster_Resize(object sender, EventArgs e)
{
  foreach (TabPage tab in tabWindows.TabPages)
            {
                foreach (Control con in tab.Controls)
                {

                    if (con is Form)
                    {

                        this.Controls.Add(con);

                      //  Thread.Sleep(20);
                        con.Size = (new Size(tab.Width, tab.Height));
                        
                        tab.Controls.Add(con);

                        //con.Width = tab.Width;
                        //con.Height=  tab.Height;
                    }

                }
            }

}

暫無
暫無

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

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