views:

41

answers:

1

How can "solid" margins be made around System.Windows.Forms.Button control?

        var button = new System.Windows.Forms.Button();
        button.Dock = DockStyle.Fill;
        button.Margin = new Padding(20);

        var panel = new System.Windows.Forms.Panel();
        panel.Controls.Add(button);

In the example above the button won't have any margins within the container panel.

Is it somehow possible to implement so the button would have 20px space around it but still behave like an ordinary Button (e.g. inherit from System.Windows.Forms.Button class and do some custom draw)?

Edit: Let me explain what I am trying to do. I would like to have such button control which would have ".Dock = DockStyle.Right" property set. Also it would have padding on the left. Thus, having few such buttons on the panel would stack them to the right of the panel. Why for? E.g. I have 3 such buttons stacked to the right. In some cases I would like to hide middle one. I would set it's property ".Visibile = false" so it is hidden. In this case the rightmost button would stack to the leftmost having the same space between them.

A: 

Place the Button in the Panel like you tried but leave the Dock property at default. Instead, size the Button within the Panel as you want and set the Button's Anchor property to Top, Right, Left and Bottom. Keeping the default Dock property value and setting the Anchor property as I desribed will cause the button to resize within the margin as you resize the Panel.

Jay Riggs