Get really usable area of a Groupbox - c#

In Windows Forms, when I position a label control at Y=0 inside a groupbox, then the label intersects with the title text on top of the groupbox.
How can I get the usable area within the Groupbox, i.e. the are that's not obstructed by the title text of the box?
Setting control.Y = groupBox.Padding.Top doesn't work. And Groupbox.ClientRectangle doesn't take the text into account either.
Edit: There is an easy hack to get that inner rectangle: Simply position one Labelin the GroupBox, and set it's Dock property to Fill. Then you can get the relevant information (Top/Bottom/Left/Right) from the Panel, or simply use the panel directly to add your child controls.
However, I'd still like to know how to get those coordinates without such hacks.

Try using the DisplayRectangle property:
The DisplayRectangle property returns the client rectangle of the display area of the control. For the base control class, this is equal to the client rectangle. However, inheriting controls might want to change this if their client area differs from their display area. The display rectangle is the smallest Rectangle that encloses a control and is used to lay out controls.
Example:
label1.Location = groupBox1.DisplayRectangle.Location;

Quite old thread, but here is what I use for my controls :
label1.Location = new Point(0,(int)(groupBox1.Font.Size)*2);
And this is how it looks with different text sizes.

Related

How to change position of inherited items in an Inherited user control

I have used a user control as a base class (let's call it BaseUC) with 3 labels (in 3 lines) on it (they are set as protected).
And there is another user control that inherits from it (InheritedUC). I have added two more labels in InheritedUC, which are positioned between the base's labels (so there are 5 lines).
Everything is fine is Visiual Studio's design UI view. But when I run the application, labels on BaseUC overlap with the ones in InheritedUC and I can't see the ones on the inherited control.
Any ideas to fix this? Thank you very much
From MSDN: Control.Anchor Property
Use the Anchor property to define how a control is automatically
resized as its parent control is resized. Anchoring a control to its
parent control ensures that the anchored edges remain in the same
position relative to the edges of the parent control when the parent
control is resized.
You can anchor a control to one or more edges of its container. For
example, if you have a Form with a Button whose Anchor property value
is set to Top and Bottom, the Button is stretched to maintain the
anchored distance to the top and bottom edges of the Form as the
Height of the Form is increased.
Set the Anchor property on all labels:
For example:
label1.Anchor = AnchorStyles.Top | AnchorStyles.Left;
If you put your controls in a FlowLayoutPanel and set the following options:
AutoScroll = True
FlowDirection = TopDown
WrapContents = False
Then you should get panel that will grow and shrink as your controls are added or removed.
Source

Dynamically creating textbox and setting its location by pixel

In a WPF application, I'd like to create a textbox dynamically which will show in front of the application and be able to freely set its location by pixel. (The textbox is going to follow the mouse cursor).
This was easily done in Winforms on the fly but WPF makes things.. a little bit weird when it comes to setting a control's location by pixel since I have to add the control as a child of a container. I'm aware this is certainly doable on Canvas, but what I actually have is a dockpanel with a richtextbox to the left and a datagrid to the right.
So what are my options here? Do I have to use canvas? Can I get away with using dockpanel (or grid) to implement what I want here?
You can use a Canvas or a Grid. If you use a Canvas, set the Canvas.Left property and the Canvas.Top property. If you use a Grid, you'll need to set a size for your TextBox, set the HorizontalAlignment to Left, and VerticalAlignment to Top. To change the location of the TextBox, assign it values for MarginLeft and MarginTop.

Resizing contents inside DevExpress Docking Panels

I am attempting to add three panels to a window using a Devexpress Docking manager and dockable panels. Here are the current results:
The three panels are placed and sized how I would like them however their contents will not correctly resize as I resize the window. This first image indicates this by the Picturebox that fails to fill the window. My current attempt to regulate this is: (Panel3 refers to a panel that contains pictureBox1. which in turn is contained by dp3.)
void dp3_SizeChanged(object sender, EventArgs e)
{
panel3.Size = panel3.Parent.Size;
pictureBox1.Width = dp3.Width;
pictureBox1.Height = dp3.Height;
}
The Same is true for the Controls Window. I have controls that do not appear unless the window is grossly oversized.
The controls are contained in 4 seperate panels that are themselves contained in the dockable window.
How do I make things appear the correct size and location whendocking and resizing?
Go throught this DevX article - Designing Resizable Windows Forms in
Visual Studio .NET-2,
that i like most for understanding about layout in Winforms.
You should set the Anchor and Dock properties on the controls in the forms.
The Anchor property controls which edges of a control are "bound" or "tied" to the corresponding edges of its form.
For example, if you set Anchor to Bottom, the distance between the control's bottom edge and the bottom of its parent will not change, so the control will move down as you resize the form.
If you set Anchor to Top | Bottom, the control will resize vertically as you resize the form.
To make a control resize with the form, set the Anchor to all four sides, or set Dock to Fill.
You can set the control's Dock property to Fill. This will cause the control to fill it's parent container.
You may still need to write some code to handle laying out the child controls. You can either do this by handling the Resize event, or by using a container that supports resizing for you (such as FlowLayoutPanel or TableLayoutPanel).
Use your Control's Anchor property. You'll probably need to set it to all sides, Top, Bottom, Left, Right, if you want it to resize according to parent control in all four directions
If you want to Maintain the controls Aspect Ratio on Resize, You'll need to store off the aspect ratio somehow, whether it's something known to you at design time or if you just want to calculate it in the constructor of the form after InitializeComponent(). In your form's Resize event,

How do I make multiple controls in a Windows Form automatically resize with the window?

I'm new to Windows Forms in Visual Studio, and I am wondering how to automaticly resize controls to the window size.
Say, I have 2 controls in a panel, a List Box and a Button. I want the button to dock to the bottom, and I want the List Box to fit the rest of the space. when the window resizes, the button should be at the bottom (as expected with docking), and the list box should stretch down to the button.
Is there a way to do this without any code?
Thanks.
Dock is pretty easy to use, but I recommend using the Anchor properties instead. Resize your form to a reasonable size in the Designer. Then, place your controls to look the way you want. Then, decide which controls should resize with the form and set the Anchor property as follows:
If you want the control to resize with the form in width, set the Right anchor.
If you want to resize height, set the Bottom anchor.
If you want the control to stay right when the form resizes, unset the Left anchor.
If you want the control to stay bottom when the form resizes, unset the Top anchor.
The problem I have with Docks is that they sometimes act funny when controls are not declared in a specific order, and to get the effect you want, sometimes you have to create extraneous panels just to hold controls.
It really gets messy when you want to maintain the aspect ratio of each control. One way, which is not really up to the mark if you want to get into fixing the details, is to use TableLayoutPanel and use Dock and Anchor wisely to achieve what you want.
Use the dock and fill options on the controls. Look under properties for each object, and containers if they are in any.
You can use SplitContainer
Google for examples. Here is one
Try setting your ListBox's Dock property to Fill.
You'll need to watch for one thing though: by default the ListBox will size itself to display whole list items. If you resize the control so that it displays a partial item it will adjust itself so it will display a complete item. This can make the control appear to lose its 'Dock'ing behavior. The solution for this is to set the ListBox's IntegralHeight property to false, which specifies that the control not resize itself to fit complete items.

Best way to make Windows Forms forms resizable

I am working on a largish C# project with a lot of Windows Forms forms that, even though you can resize the form, the elements in the form don't scale.
How can I make the form elements (such as the datagridview, text area's, etc.) scale when the user changes the size of the form?
Nearly all the forms subclass from one specific form, so if there's something I can do in the base class, that'd be great.
You should set the Anchor and Dock properties on the controls in the forms.
The Anchor property controls which edges of a control are "bound" or "tied" to the corresponding edges of its form.
For example, if you set Anchor to Bottom, the distance between the control's bottom edge and the bottom of its parent will not change, so the control will move down as you resize the form.
If you set Anchor to Top | Bottom, the control will resize vertically as you resize the form.
To make a control resize with the form, set the Anchor to all four sides, or set Dock to Fill.
Use the Anchor and Dock properties.
Anchor allows you to pin specific sides of the control to the sides of the parent control.
Dock will bind the whole control to a side of the parent control or it can be set to fill the contents of the parent control.
You usually just need to set the Anchor to the bottom and right of the parent control but gets more difficult when you have controls side by side, then you need to manually resize the controls on the forms OnResize event to get them to scale naturally together.

Categories