locked
Docking problems RRS feed

  • Question

  • How can I dock an auto sized panel to the right of a form and insert an auto sized buttons docked to the top of that panel. When ever I try to do that the panel disappears.

    Try this:

    1-Create a panel control and dock to the right.

    2-Create a button inside that panel and dock it to the top.

    3-For both controls set AutoSize = True, and AytoSizeMode = GrowAndShrink

    The panel will disappear!!

    Monday, December 22, 2014 4:16 AM

Answers

  • Finally I've found the best solution....

    I've created a control inherited from panel and then overrided the GetPreferedSize method to return the accurate size of the panel..... here is the code

    Public Class PanelEx
        Inherits Panel
        Public Overrides Function GetPreferredSize(proposedSize As System.Drawing.Size) As System.Drawing.Size
            Dim size As Size = MyBase.GetPreferredSize(proposedSize), s As Size
            If AutoSize AndAlso (Dock = DockStyle.Left OrElse Dock = DockStyle.Right) Then
                For Each c As Control In Controls
                    If c.AutoSize AndAlso (c.Dock = DockStyle.Bottom OrElse c.Dock = DockStyle.Top) Then
                        s = c.GetPreferredSize(proposedSize)
                        If size.Width < s.Width Then size.Width = s.Width
                    End If
                Next
            End If
            Return size
        End Function
    End Class

    Now if i used the new PanelEx control instead of normal panel every thing will work fine and i need just to change  the buttons dock to get the effect....

    Thanks guys for your big big help.....

    • Marked as answer by eiadxp Tuesday, December 23, 2014 3:21 PM
    Tuesday, December 23, 2014 3:20 PM

All replies

  • Greetings.

    I've tried following your steps, and can confirm that what you say is correct.

    I think what's happening (I'm not sure), is that you are getting a feedback loop where the panel is being resized (shrunk) to match its contents (the button) and the button is being shrunk to match the panel that it's docked to, and this process continues until the whole thing disappears.

    I suspect that if you set AutoSize to false for the Panel, you will get the effect you're after. If you don't, let us know what you are ultimately trying to achieve so that we can look for other suggestions.
    Monday, December 22, 2014 4:53 AM
  • Hi,

    I think what you are trying to do is that the button is always on the right with autoresize? so whenever you want to resize the form, the objects will always follow.

    You have to have a combination like below.

    Anchor = top, left
    Auto Size = true
    Auto side mode = GrowAndShrink
    Dock = Right


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 22, 2014 5:57 AM
  • Thanks for the reply giovhan.....

    What i want is to dock some buttons to the side of the window and give the user the ability to arrange them vertically or horizontally as in the following images:


    • Edited by eiadxp Monday, December 22, 2014 8:11 AM
    Monday, December 22, 2014 8:10 AM
  • Thanks Ante Meridian

    Your  idea will solve the hidding problem but does notwork with me cozi need the panel to have the buttons size as i reply for giovhan

    Monday, December 22, 2014 8:13 AM
  • My guess is that you should not be using the panel. Place the buttons directly on the form, then anchor them to the top and right as per giovhan's suggestion. Then you will need to write code to reposition them horizontally or vertically according to the user's choice.
    Monday, December 22, 2014 10:50 PM
  • And here's some sample code for repositioning the buttons.

    If you have three buttons called button1, button2, and button3, this code (triggered by another button click, button4) will toggle between horizontal and vertical layout. If the three buttons have their anchors set to top and right, this should give you what you want.

          bool horizontal = true;
          private void button4_Click(object sender, EventArgs e)
          {
             int gap = 3;
             if (horizontal)
             {
                button3.Location = new Point(this.ClientRectangle.Right - button3.Width - gap, gap);
                button2.Location = new Point(button3.Location.X - button2.Width - gap, gap);
                button1.Location = new Point(button2.Location.X - button1.Width - gap, gap);
             }
             else
             {
                button1.Location = new Point(this.ClientRectangle.Right - button1.Width - gap, gap);
                button2.Location = new Point(button1.Location.X, button1.Location.Y + button1.Height + gap);
                button3.Location = new Point(button2.Location.X, button2.Location.Y + button2.Height + gap);
             }
             horizontal = !horizontal;
          }

    Monday, December 22, 2014 11:09 PM
  • Hi,

    I don't think that the designer has the capability to do this as a default; maybe you need a little bit of code.

    To start with, create a main panel dock to the right; inside the main panel has 3 panel (all dock=top) with 1 button inside.. then during form resize, you change the dock behaviour. after that you can play with it.

      protected override void OnResize(EventArgs e)
            {
                base.OnResize(e);
                if (panel1.Height < 200)
                {
                    panel1.Dock = DockStyle.Top;
                    panel2.Dock = DockStyle.Right;
                    panel3.Dock = DockStyle.Right;
                    panel4.Dock = DockStyle.Right;
    
                }
                else
                {
                    panel1.Dock = DockStyle.Right;
                    panel2.Dock = DockStyle.Top;
                    panel3.Dock = DockStyle.Top;
                    panel4.Dock = DockStyle.Top;
    
                }
            }

    Good luck.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 23, 2014 1:55 AM
  • Thanks my friend for your efforts to help me..... but i prefer to use wniform layout engine and that's why i was trying to use panel in a general way that allows me to add as many buttons as i need.....

    I've found a solution that suit my needs.... But i think that odd behavior of panel is a bug in the layout engine ...

    I'll write my solution at the end....

    Tuesday, December 23, 2014 3:07 PM
  • Thank you giovhan 

    It is a nice work around but i've found a less complicated solution.... i'd use your code if i did not found the next code

    Tuesday, December 23, 2014 3:14 PM
  • Finally I've found the best solution....

    I've created a control inherited from panel and then overrided the GetPreferedSize method to return the accurate size of the panel..... here is the code

    Public Class PanelEx
        Inherits Panel
        Public Overrides Function GetPreferredSize(proposedSize As System.Drawing.Size) As System.Drawing.Size
            Dim size As Size = MyBase.GetPreferredSize(proposedSize), s As Size
            If AutoSize AndAlso (Dock = DockStyle.Left OrElse Dock = DockStyle.Right) Then
                For Each c As Control In Controls
                    If c.AutoSize AndAlso (c.Dock = DockStyle.Bottom OrElse c.Dock = DockStyle.Top) Then
                        s = c.GetPreferredSize(proposedSize)
                        If size.Width < s.Width Then size.Width = s.Width
                    End If
                Next
            End If
            Return size
        End Function
    End Class

    Now if i used the new PanelEx control instead of normal panel every thing will work fine and i need just to change  the buttons dock to get the effect....

    Thanks guys for your big big help.....

    • Marked as answer by eiadxp Tuesday, December 23, 2014 3:21 PM
    Tuesday, December 23, 2014 3:20 PM