簡體   English   中英

根據面板上項目的大小在面板上添加垂直滾動條和“換行”

[英]Adding a vertical scrollbar and “line break” on a Panel depending on its items' sizes

我想以幾個動態groupBox的形式顯示SqlDataReader對象的每一行的結果,以將其添加到現有面板。

當這些組框之一在包含面板中引起水平溢出時,我也希望自動換行,而不是一直從左到右依次添加。

因此,當組框超出面板的水平范圍時,它會被放置在新的“行”上,而不是在先前的組框下。

這是我的代碼:

SqlCommand cmd = new SqlCommand(sqlString, cnx); //return;
SqlDataReader myReader;
myReader = cmd.ExecuteReader(); 
int i = 0; int j = 0;
int sw = Screen.PrimaryScreen.Bounds.Width;//sw=1920
int sh = Screen.PrimaryScreen.Bounds.Height;//sh=1080

panel1.HorizontalScroll.Maximum = 0;
panel1.AutoScroll = false;
panel1.VerticalScroll.Visible = false;
panel1.AutoScroll = true;

while (myReader.Read())
{
    MessageBox.Show("Results : \n Distinct values : " + myReader["column1"].ToString() + " Occurence number  : " + myReader["nbre_column1"].ToString());                        
    //I can show all values including on myReader on this message box

    GroupBox resultGroupBox = new GroupBox();
    resultGroupBox.Size = new System.Drawing.Size((sw * 20) / 100, (sh * 20) / 100);
    resultGroupBox.Location = new Point(44 + i, 36);
    i = i + 390;
    //j = j + 220;
    panel1.Controls.Add(resultGroupBox);
}

總而言之,我想說明兩點:

  1. 動態添加的組框的“換行符”,一旦它們的組合寬度超過面板的寬度,並且沒有水平滾動條
  2. 當組合框的所有“行”的總高度超過面板的高度時的垂直滾動條。

提供的代碼不會阻止水平滾動條出現,並且如果j = j + 220行未注釋,則兩個滾動條都會出現,但不會出現換行符。

在此先感謝您的幫助。

使用FlowLayoutPanel而不是Panel ,並以這種方式設置其屬性(如果尚未設置):

  • 自動滾動: True ; 在需要時添加垂直滾動條
  • WrapsContent: True ; 允許內容在需要時水平包裝

暫無
暫無

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

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