How do I add a form to the panel properly - c#

I designed a side panel and when I press the buttons, the form inside the panel on the right changes. However, the components in the forms I designed appear in the wrong places. Some are not visible because they are left outside.
I upload some photos. Design Screen and
at Runtime
Where am i doing wrong ? Panel Size and form size are same.
UPDATE:
I created usercontrol instead of form. However, nothing has changed. For example, when I put two labels side by side with 5 px between them in the design, it seems to have much more spacing if I run it. Or the labels I placed at the far right or at the bottom do not appear at runtime.
TelemetriUI telemetriUI = new TelemetriUI();
telemetriUI.TopLevel = false;
telemetriUI.FormBorderStyle = FormBorderStyle.None;
telemetriUI.Dock = DockStyle.Fill;
this.mainPanel.Controls.Add(telemetriUI);
this.mainPanel.Tag = telemetriUI;
telemetriUI.BringToFront();
telemetriUI.Show();

Related

How to create a scroll section that I can add buttons to on runtime?

I am new to the winforms and want to create an application that will have a scroll section which I need to make for the buttons which I will add on runtime.
The user will press an add button to add a button containing data which will then be used for the title, description. I need the button to be added to the scroll section and to be automatically placed under the last button in the section so it is organized and so I can scroll up and down through the section.
Like the image has with the scroll bar and the buttons:
How do I solve the problem?
You can use a FlowLayoutPanel with the following properties set:
AutoScroll = true;
FlowDirection = FlowDirection.TopDown;
WrapContents = false;
Once you do that, you can add buttons (or any control) to the Controls collection and it should flow from top to bottom and cause vertical scrollbars to appear once you fill the space.
Note, that it is quite difficult (or virtually impossible) to make a WinForms app look as nice as the one you provided an example of. You would probably be better off using WPF. But my answer should at least solve your particular problem with WinForms.

Specify the location of a maximized form?

I have an MDI app, and I have recently added a new control to the MDI container, which covers the client area (the area where the children appear and function). The trouble is, when they try to maximise their forms, it appears behind the sidebar:
Original form for comparison (to see the overlap)
I know that I can restrict the total size of each form that gets maximised, using the Form.MaximumSize property, however I'm not sure how to set the x/y location of the form to sit next to the sidebar.
I could use the side panel as a marker, i.e. x = sidepanel.Width because that will never change, but I don't know how to apply that to a maximised form.
Alternatively, is it possible to give the sidebar the same behaviour as the menu bar? That is - the menu bar is not considered to be in the client area; so when a form is maximised it will not overlap the child form?
Ok, so after some fiddling around, I found the main problem was that since I was adding the control dynamically, control of it was a bit difficult. So instead I did this:
Added a panel (using the forms designer) to the MDI Parent
Set the Dock property to "left"
Added the custom control dynamically to the panel:
SidePanel = new Menu_SidePanel();
SidePanel.Location = new System.Drawing.Point(0, 0);
SidePanel.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left);
panel1.Controls.Add(SidePanel);
Now, because the panel has been added to, and "docked" on the Parent form, the rest of the child forms, when maximised, use the Side Panel as another boundary.

Resize Splitter (for final user)

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();

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.

Combo box in a scrollable panel causing problems

I have a panel with AutoScroll set to true. In it, I am programmatically adding ComboBox controls. If I add enough controls to exceed the viewable size of the panel a scroll bar appears (so far so good). However, if I open one of the combo boxes near the bottom of the viewable area the combo list isn't properly displayed and the scrollable area seems to be expanded. This results in all of the controls being "pulled" to the new bottom of the panel with some new blank space at the top. If I continue to tap on the drop down at the bottom of the panel the scrollable area will continue to expand indefinitely. I'm anchoring the controls to the left, right and top so I don't think anchoring is involved. Is there something obvious that could be causing this?
Update: It looks like the problem lies with anchoring the controls to the right. If I don't anchor to the right then I don't get the strange behavior. However, without right anchoring the control gets cut off by the scroll bar.
Here's a simplified test case I built that shows the issue:
public Form1()
{
InitializeComponent();
Panel panel = new Panel();
panel.Size = new Size(80, 200);
panel.AutoScroll = true;
for (int i = 0; i < 10; ++i)
{
ComboBox cb = new ComboBox();
cb.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
cb.Items.Add("Option 1");
cb.Items.Add("Option 2");
cb.Items.Add("Option 3");
cb.Items.Add("Option 4");
cb.Location = new Point(0, i * 24);
panel.Controls.Add(cb);
}
Controls.Add(panel);
}
If you scroll the bottom of the panel and tap on the combo boxes near the bottom you'll notice the strange behavior.
This seems to be a problem specific to the devices you are using. I can't reproduce this behavior at all. Have you tried setting the Dock property of the Panel? How about the other properties that affect scrolling behavior (AutoScrollMargin, AutoScrollMinSize, AutoScrollPosition)? I'd also play with the size of the panel, and maybe use the System.Windows.Forms.Screen class to determine it automatically based on the device.
Try setting autoscroll to false and try using scroll bars normally.
Alternatively, if you are unable to find another reasonable solution, it would be possible to handle the scroll/resize events and manually resize/move your controls (Exactly what anchor does) While this would be quite annoying to do, it would get rid of any problems you may be having.
I too was able to test against a device of Windows CE, under Visual Studio 2005, .Net CF 2.0 as we too have stuff using older equipment too. I have no problems. Might it be any issue of the Windows CE version on the device that is causing the issue? What device is being used (ie: Manufacturer?). I even created a brand new project of the single form, so no other things going on to alter / influence your code.
Does the behavior work while debugging, run-time, or both?

Categories