簡體   English   中英

孩子長到左上角而不是右下角

[英]Childs grow to top-left instead of bottom-right

僅當控件在底部或右側過度增長而在左上方過度autoscroll時,如何處理autoscroll

我將解釋:將面板放置在winform中,然后在面板內部放置一個按鈕。 使按鈕的位置為負數,例如-20,-20。 滾動條不出現

這個人也有同樣的疑問 ,但是回答建議改用WPF,這不是該項目的選擇。

那不是滾動的工作原理。 面板的邏輯左上角始終為(0,0)。 並且始終在左上角顯示,滾動條為0。

只需將面板的AutoScrollMinSize屬性增大20x20,然后將所有控件移動+ 20,+ 20,即可得到與您期望的結果完全相同的結果。 當然,現在哪個按鈕可見。 並調整了滾動條,它們的范圍更大。 如果使用AutoScroll,則僅移動控件就足夠了。

控件必須始終具有正的Location.X和Y值,以使其在容器內可見。

您可以通過在(0,0)處進行添加,然后將面板的顯示區域上下左右移動,來模擬在面板的左上方區域中添加按鈕。

而不是將按鈕的位置(-20,-20)設置為(0,0)。 接下來,遍歷面板中的所有其他控件,並分別向右移動20個像素和向下移動20個像素。 最后,向下和向右滾動面板。

    private void Form1_Load(object sender, EventArgs e)
    {
        btnResetPosition_Click(sender, e);
    }

    private void btnMoveToUpperLeft_Click(object sender, EventArgs e)
    {
        //Set Panel View to upper-left before moving around buttons
        panel1.VerticalScroll.Value = panel1.VerticalScroll.Value = panel1.VerticalScroll.Minimum;
        panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Minimum;

        //Move button1 to "upper-left"
        button1.Location = new Point(0, 0);

        //Adjust "static" controls right and down to simulate moving button1
        button2.Location = new Point(button2.Location.X + 200, button2.Location.Y + 200);
        button3.Location = new Point(button3.Location.X + 200, button3.Location.Y + 200);

        //Scroll to show "static" controls in panel
        panel1.VerticalScroll.Value = panel1.VerticalScroll.Value = panel1.VerticalScroll.Maximum;
        panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Maximum;
    }

    private void btnResetPosition_Click(object sender, EventArgs e)
    {
        //Set Panel View to upper-left before moving around buttons
        panel1.VerticalScroll.Value = panel1.VerticalScroll.Value = panel1.VerticalScroll.Minimum;
        panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Value = panel1.HorizontalScroll.Minimum;

        //Line up all three buttons
        button1.Location = new Point(19, 17);  // 19 and 17 are used so that when panel scrollbars appear, "static" controls appear to stay in the same place
        button2.Location = button1.Location;
        button2.Location = new Point(button1.Location.X, button1.Location.Y + 29);
        button3.Location = button2.Location;
        button3.Location = new Point(button2.Location.X, button2.Location.Y + 29);
    } 

要運行示例代碼,請將“ panel1”添加到名為“ Form1”的表單中。 將panel1的大小更改為(111,115)。 將三個按鈕添加到panel1中,分別稱為“ button1”,“ button2”和“ button3”。將兩個按鈕添加到名為“ btnMoveToUpperLeft”和“ btnResetPosition”的表單中。將示例代碼粘貼到Form1.cs中。

請注意,用於移動滾動條的代碼看起來很有趣,因為存在一個錯誤,即僅將滾動條設置為等於該值會導致滾動條不更新。 如何手動滾動面板?

暫無
暫無

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

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