Resize Splitter (for final user) - c#

Main question:
I have a Form that is MDI container and placed a Splitter.
I need that the final user can customize the size of this Splitter. How? By default the mouse cursor changes to VSplit but the Splitter is always locked.
Optional question:
This structure/visual layout of this program is identical to the Simatic 7. Example:
I'm guessing the Splitter is the better option to enable the two panels, the one on the left and the one on the bottom. Right? Or how to get the similar layout of the example?

I recommend a splitContainer over a splitter. SplitContainer was to replace splitter, but they left it for compatibility. I find it quite a bit easier to use since it creates the panels for you and gives you better access to the properties you will likely want.
It's quite a bit easier to get to do what you intend without extra work.
You should be able to get that layout with 2 splitcontainers. Setting one's Orientation property to Horizontal, then placing the other splitcontainer in the top panel.
Simplified Code Sample to display a form:
Form1 frm = new Form1();
frm.TopLevel = false;
SplitContainer2.Panel2.Controls.Add(frm);
frm.Dock = DockStyle.Fill;
frm.Show();

Related

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!

Best Way to make a Windows Forms scalable?

What would be the best way to make a WinForms application fully scalable, for example when the Form resizes?
In WPF i would use something like a Viewbox and/or a UniformGrid, but something like this doesn't exists in WinForms.
Is there an easier (and maybe faster) way to rescale controls on a from after resizing it, instead of resizing them all by calculating their new Size/Location etc.?
Thanks in advance
In Windows Forms, you use the Anchor and Dock properties for each control.
Here's an article about using them: http://www.techrepublic.com/article/manage-winform-controls-using-the-anchor-and-dock-properties/
You should also look at FlowLayoutPanel and TableLayoutPanel
you can use anchor and dock, depending on your need:
Anchor - the edges of the container to which a control is bound and determines how a control is resized with its parent.
Dock - which control borders are docked to its parent control and determines how a control is resized with its parent.
for further read: Dock and Anchor
Have a look at the Anchor property found on pretty much any control. This allows you to lock a control to any (or all) of the four borders of a window.
Once one distance is anchored (e.g. Top or Right), the control will always try to keep that distance, no matter how you resize your window.
For example, you'd set Anchor to Bottom and Right for a button that is supposed to always stay in the bottom right corner of a window. A text box, that should always fill the window from left to right would use Left and Right.
Similar things can be achieved utilizing Dock, but a docked control will always try to fill as much space as possible (there are different strategies available, like "fill everything from here and upwards) based on its container. Depending on your use case, this can however be a lot harder to control (and I usually only use it if I want a single control to fill a full window, e.g. a TextBox).
If you need more complex alignment, like widths scaled on some kind of ratio (e.g. 30%), then there are several different containers available.

Prevent Child Components when Overlapping Panel

I have a WinForms application which has two panels which individually contain a usercontrol component (one in each panel). How ever, because both panels are the same size and in the same location, the top most panel becomes a child of the other panel. I want to be able to hide one panel and then show the other panel:
this.panel1.visibile = false;
this.panel2.visibile = true;
But when I hide panel1, the second panel is hidden as well.
How can I make panel2 a non-child of panel1?
I like to keep things simple because I'm new to C# Programming.
This is a common accident in the designer. Dropping the second panel on top of the first one will make it a child of the first panel. Two basic strategies to fix this:
View + Other Windows + Document Outline. Click the second panel in the list and drag it to the form. You'll need to fix up the Location property by hand by typing its value in the Property window.
Keep the upper left corner of the second panel just a bit to the left or top of the first panel. Put it in the right place in the form's constructor by assigning its Location property after the InitializeComponent() call.
Check this answer for a control that works well at design time and lets you easily flip panels at runtime.
The designer will do this automatically because it assumes that when you drag one control over another, you want to make it a child of that control.
What I usually do to get around this is to create the control in a different place on the form, and then use the properties to manually match the positions of sizes of the two controls.

Resizing a panel c# win forms

If you imagine a win form with a line drawn vertically down the middle. On the left i have a graph, and when you click the graph certain forms open on the right in an mdi type panel.
I am trying to figure out how to logically get this to look like a proper application should but am failing!
The whole form loads in a maximised view. I first set the panel width to 0 then when i add a form i check if the panels width is less than the forms, if it is then change the panels width to that of the forms.
This doesn't look great tho tbh, resizing makes strange things happen and i see a lot of grey. Does anyone have any ideas?
Use a SplitContainer on your main form. Ensure it's Dock property is set to Fill
Put your graph stuff on the left panel, and your other stuff on the right side.

How to have form resizeable C#

I currently have all the usual components inside a panel in a form, but how can I have it so when I resize the form it will move the components with it?
I already tried
this.panel1.size = new System.Drawing.Size(this.form.size.width, this.form.size.height);
but it didn't work
try setting Anchor or Dock of the panel.The nachor property has 4 possible states Top, Right,Left, Bottom. These states can be combined. IF you set all 4 to panel it wiil resize with the from. Dock is more or less like Anchor jsut experiment with different combinations to see if you can get waht you want. IF what you whant is very complex and can not be done with simple Anchor or Dock you can attach to evnet of the from SizeChanget and calculate the new size of the panel.

Categories