My own tableheader? - c#

I want to make a table header:
The steps I have taken
Made a user control
Placed a TableLayoutPanel on it(this is basically a grid layout?)
Added and removed the number of columns/rows I wanted
Placed a label in each cell
For each label set its dock to fill and borderstyle to fixedsingle
However the borders are not lined up against each other but instead there is a gab, that makes the whole thing look very ugly.
The border size is also very small, how can I make it bigger?

You can remove the gaps by changing the lable margin property to 0,0,0,0 but the borders will double up in thickness where they meet (Leaving the outer border thinner)
You can disable the borders on the individual labels and use the TableLayoutPanel CellBorderStyle property to get a consistent look with several options for line styles but this will affect all cells, not just the header.
If the TabelLayoutPanel is a fixed size perhaps a background image?
I have no idea how to make the borders thicker in winforms, This is where WPF rules but beats you over the head for using it.
Mike

Related

How can I link a component's Size to the Window size?

I am making a simple WinForms program.
I would like to link a component's size to the size of the Window.
Let's say the user enlarges or shrinks the Window by dragging it's borders: I would like that a component gets bigger when the Window does and vice versa.
Let's pretend that we have two Buttons, in the center of the Window, side by side: I want to make them the same size filling the entire width of the Window.
How can I do that?
Sample procedure, using a TableLayoutPanel and 2 Buttons:
Add a TableLayoutPanel to the Form
Edit the Columns and Rows collections so that you have 2 Columns, both sized at 50% and one Row, set to Autosize.
Set the Location.X of the TableLayoutPanel to 0 and adjust its width to the width of the Form.
Set the TLP Anchor property to Left and Right
Adjust-drag the Row height to be ~twice the size of the Button it will host
Add one Button to the Form
Adjust the appearance of the Button as required.
CTRL-Drag the Button to create an exact duplicate
Add the two Buttons to the two cells of the TableLayoutPanel
SHIFT-Select both Buttons and set the Dock property to DockStyle.Fill
You can now adjust the Margin property of the Buttons (still both selected, so the same settings will apply to both) to modify the space between the controls
Re-adjust the TableLayoutPanel's only Row height as needed.
Extra: if you have only these controls on the Form, you may want to fix the MinimumSize of the Form, to avoid that, when the Form is resized, your controls are shrinked beyond recognition, ruining the overall layout: resize the Form in the Designer to a point where the hosted controls layout is compromised, find a suitable minimum size and use this measure as the Form's MinimumSize property. The MinimumSize can be set using only the Width or the Height measure (e.g., (100, 0)). This limits the Form's Width but not the Height. Or the opposite, of course.
If, when dragging the Buttons inside the TableLayoutPanel, the Buttons
are not automatically inserted at the Top-Left position of the cell
and instead they appear positioned in a random place, then the
TableLayoutPanel has gone rogue and needs to be put down. Delete it
and drop another on the Form. Rinse and repeat.
This may happen if you tamper with the layout a bit. Better start over than trying to correct the problem.
TableLayoutPanel Control Overview

Make DataGridView stretch to middle of WinForms form?

I have a WinForm form that has two DataGridView controls paced on it such that they are stacked, one above the other against the right hand side of the form.
I would like a way of setting them so that when I expand the form, they expand height-wise with it, as well as width-wise. I managed width-wise by anchoring them to the left and right sides and anchoring the top one to the top and the bottom one to the bottom. However, from here I'm not sure how to get them to use up the space in the middle that appears when the form maximizes...Maybe an image will make my meaning clearer:
Normal Size:
Maxmized; I'd like the grids to expand to take up the full height of the form between the two of them as the red arrows show:
If this question is blindingly obvious I apologise and can only say I didn't really know how to phrase it properly and so found searching for it on Google unhelpful!
You have two options:
TableLayoutPanel or
SplitContainer
The former lets you create a table of many columns and/or rows with various sizing options from absolute and percent to autosize. This is very powerful for layout; but in other respects TLPs are somewhat restricted as the 'cells' are only virtual..
A SplitContainer offers only two panes but lets you treat each with all the things you can do to a container: add one or more controls, anchor or dock them, give each pane a BackColor and make use of its event model.
So if you need just two controls of equal size that adapt to the form size like you showed in the question, a SplitContainer is maybe the better option.
Set the splitter to fixed and make it smaller, anchor the SplitContainer to all sides and drop the DGVs into their panes and Dock them to Fill.
You could also make the splitter moveable to allow the user to resize the panes; if you do that do make the splitter width larger..
Also make sure that the FixedPanel is set to None so that height changes are shared.
Hint: If you want a few more panes to share the space you can nest several SplitContainers.. But for larger numbers do consider switching to TLP!

C# positioning buttons

I have 3 buttons in my form. What I need to do is when I make the actual form bigger or smaller, the buttons should change their position and size so they look good, so they wouldn't remain the same size and position. I tried to use the anchors, but that does not work very well. What can I use to solve my problem?
You can check dock and anchor properties
http://www.youtube.com/watch?v=afsx1IJULLI
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dock(v=vs.110).aspx
You should set both left and right, or top and bottom anchors to resize control. If you'll set only one anchor from these pairs, then control will be positioned instead of resizing.
Docking will resize control, because it is equivalent of setting three or more anchors.
Try using TableLayoutPanel, put your buttons inside the columns of the table
Look good is different all the time. I like placing buttons in StackPanel and setting AutoSize property to true. This fixes two issues:
If user has 150% font in Windows settings - your UI does not break;
if you resize window to be very small - your buttons do not enforce minimal width/height and adapt to ratio user has chosen

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.

Make UserControl resize when Dock = Fill

I have a problem with UserControl that I'm crafting. It consists of TableLayoutPanel with another TableLayoutPanel in it with ListView inside. I want to make it resizable so that it will fit in left panel of my app and behave somewhat like Toolbox in Visual Studio. The problem is that my control doesn't scale when I resize panel.
UserControl is embedded in panel with Dock = Fill and Anchor = Tob, Left, Bottom, Right. Also all controls in it are made that way. How can I fix this?
EDIT: It's WinForms, not XAML.
Use a split panel and put your UserControl in the left panel and Dock.Fill it.
You're probably looking for the AutoSize properties on the TableLayoutPanel and the AutoSize ColumnType of that panel.
You can achieve something like a Dock = Fill by simply auto-sizing the table layout panel (GrowAndShrink) so that it will always fit your inner control.
Please post your designer code to see how you embedded the controls in which other control.
I suspect your resize problem come from your resizing strategy of control inside the TableLayoutPanel.
The table layout panel is tricky. Regarding the resize strategy you want to follow inside a cell of the table panel, the control in the cell have either to be Dock.Fill or Anchor = Top, Left, Bottom, Right.
Basically:
Il you want the grid cell to adapt to the size of the control, then have the control in the cell Anchor = Top, Left, Bottom, Right and set the row/column to autosize.
If you want the control in the cell to adapt to the cell size, use Dock.Fill on it and use a percentage or a absolute value to size your cell.
The behavior of the TableLayoutPanel is best described in the MSDN documentation.

Categories