I have a WinForms panel that holds two items, eg. labels.
I need to align them horizontally differently, let's say Label1 needs to be aligned to center of panel and Label2 needs to be aligned to the right side of the panel. The alignment have to preserved on resizing.
What panel should I use? Panel? FlowLayoutPanel? Anything else?
How should I set properties of panel/labels to achieve the above arrangement?
Use TabelLayoutPanel with two columns. Set the left Label Anchor to Top and Bottom and label Text align center.
Label 2 anchor Right, Text Right Center.
Make TabelLayoutPanel either Dock to fill (if its the only control on the form) OR set the anchor to the sides that you would like to expand towards.
Further alignment of label can be controlled with its anchor positions within parent container and text alignment.
Related
In the WinForm I am working on, I have two TextBox widgets and one button between the text boxes. When the form starts, and the winform is at its minimum size, the TextBoxes look as expected. No gaps between the items where they come close to the button there in the center. This is as seen in the top graphic.
However, when the user maximizes the winform, the TextBoxes move to the correct position but do not expand to keep close to the button in the center. Textbox A is anchored to the left and Textbox B is anchored to the right. So the alignment is correct. This is as seen in the bottom graphic.
Now, the question is how to make text box A grow to the right so that it comes close to the button in the middle and text box B grow to the left so that it also comes close to the button?
Thanks! Eric
Use TablelayoutPanel control. From MSDN
Create TableLayoutPanel with one row and three columns
First column: SizeType: percent, 50%
Second column: SizeType: absolute, 75(button width)
Third column: SizeType: percent, 50%
Then put textboxes and button in the columns.
Set for textboxes and button property .Dock = Fill
Size of the textboxes will be automatically changed with size of the columns in the TableLayoutPanel
Set Dock property of the TableLayouPanel to Fill or Bottom or Top. Then TableLayoutPanel will change width when width of the form is changed.
With simple anchors you cannot achieve this.
But you can use a TableLayoutPanel:
Place a TableLayoutPanel on the form and set Dock to Fill (or Top/Bottom)
Make 3 columns.
Column 1: Percent, 50%
Column 2: Absolute, set the desired size of the Button (eg. 100px)
Column 3: Percent, 50%
Drop TextBoxA into Column1, and set Anchor Left and Right
Drop the Button into Column2, and clear the Anchors
Drop TextBoxB into Column3, and set Anchor Left and Right
Without a TableLayoutPanel, you can make the following:
TextBoxA: Anchored Left and Right
Button and TextBoxB: Anchored Right
So only TextBoxA will grow.
you can use the Anchor Property for Button and textbox -
Button A - Anchor - left and Right
Button B - Anchor - left and Right
you can also, set textbox anchor property as per your needs and control the location of all these three controls.
I am trying to establish the following behavior: When hiding the bottom (blue) panel I want the top (red) panel to stretch to replace it vertically and vice versa (hiding red panel should expand blue).
Using a table layout panel with auto sized rows I was able to get the blue to stretch upwards but I can not get the red panel to grow downwards.
It's difficult to see from the far right picture but the bottom table row did not shrink with the panel. Table layout panel is not a requirement for me but I was hoping it would offer a solution.
Try using a SplitContainer control instead.
You can control each panel's visibility with the Collapsed properties:
splitContainer1.Panel1Collapsed = true;
or
splitContainer1.Panel2Collapsed = true;
I'm a newbie. Designing a form that can be resized, and I want my textboxes, labels and buttons to resize with the form, can someone tell me how to do this?
It depends on the type of layout you need. The "basic tools" you have to do that are following properties: Anchor and Dock.
Anchor
With the Anchor property you "attach" a side of an element to a side of its container. For example if you place a button in the bottom-right corner of a window and you set "Bottom, Right" as Anchor then when you'll resize the form the button will keep its relative position to that corner.
Now imagine you place a multiline text-box in the form, resize as needed (for example 4 px from top, left and right border and 128 px height) and set the Anchor property to "Left, Top, Right". When you'll resize the form that control will keep its height but it'll resize to keep its margins (so if you'll make the form wider its width will be increased).
Dock
Dock is different. With docking you "say" to the Layout Manager to use all available space in one direction. For example if you set to Left then your control will keep its width but it'll use all the available height and its location will be most left as possible.
You may have more than one control docked in a container, imagine you have 5 textbox with Top docking inside a form. They'll be stacked to the top of the form using all the width (and resizing). Another example: a Top docked control (as a banner) and a "Fill" docked control (as main content). Remember that with docking the order of controls matters (if you first place the "Fill" control it'll use ALL the available space and the "Top" dock control will overlap).
Even more
Moreover you have some layout controls too (tables and stacks). They're really easy to use and a 30 minutes of "experiments" will clarify much better than a long text.
I'm having some problems with the layout of buttons on a screen. There are a number of buttons with the visibility set to collapsed depending on certain criteria.
Anyway, with the buttons sitting in a stackpanel, the contents determine the width of the buttons. Because these should share the same size, I put them in a grid where the columns are using the SharedSizeGroup.
Having done this, buttons now all share the same width, the smallest width needed to display the button with the largest content. What it does mean though is that buttons don't display at the required position.
That is - assume five buttons in columns 0-4 where the first button is collapsed, buttons should ideally all shift to the left so that they always occupy space left to right. Is there any way I can accomplish this or, if not, how can I get buttons in a stackpanel to share the same width?
Any help is much appreciated.
You can wrap your buttons with individual grids and use that to set a shared size, the scope should be the stackpanel. Then control the visibility of the individual grids instead of the buttons, this is a bit messy though.
Edit: This is probably what you need:
<UniformGrid Rows="1" HorizontalAlignment="Left">
...
I want to be able to display a tooltip centered above my control.
I know how to customize the XAML used to display the tooltip using the TooltipService, however, the placement options the TooltipService makes available only allow you to specify top, bottom, left, right, etc. They don't let you specify an alignment once on the given side.
By simply using the TooltipService, selecting a placement of "Top" puts the tooltip above the control and has it aligned to the left side of the control. If there is not enough room for it to be aligned left, it moves it to be aligned on the right side of the control.
I want the tooltip to be centered on the top of the control. I don't want it to anchor to either the left or the right while on top.
Is this possible? How?
you should check out the TooltipService. MSDN documentation is here. and here is a (WPF) sample from CodeProject.
A quick shot: you could create your own TooltipService class to extend the possibilities of the existing one to manipulate a Tooltip instance.
Here you can find a nice class to help position a popup by specifying a list of preferred positions, like the TooltipService does for tooltip. You can specify a Top placement with Center HorizontalAlignment. I know this isn't a tooltip but I thought you could check it out and use it to create a BetterTooltipService.