How to set Z-index in C# (winform apps) - c#

I am working on desktop application in which i'm facing a problem on Z-Index. I generate some elements like panel, picture box and labels dynamically. My problem is in positioning element w.r.t z-Index, I tried Bringtofront() and setChildIndex() functions but they don't work as I wanted/expected.

I suggest you to generate panel first and then generate child controls in it and for location of child control use relative positioning of panel.
above suggestion is just a normal way but still if your problem is different then please explain your problem in detail.

Create panel dynamically then add controls to it:
Panel p = new Panel();
p.Controls.Add(controlobject);

Related

Winform control becomes child of other control, as opposed to being added to the form.

So I have three panels all stacked on top of each other in the same location in a windows form application.
Each time I add another panel on top it becomes a child of the previous panel. This means each panel cannot be shown independently, ie, to show panel 2 I must first show panel1 (the parent). I'm fairly sure this is what is happening. However I need to be able to call panel2.show(), whilst panel 1 is hidden.
Is there a way to work around this?
This is a WinForms designer issue. You can either try moving the panels around and set the position in the properties or enter code behind and set the parent-child relationships manually.
basically don't just drag and drop panels on top of each other. Try to programmatically code their locations. Look for this kind of code in your Form.Designer.cs this.panel2.Controls.Add(this.panel3); and delete them.
Add all your panels in the form part which is supposed to be at the bottom.
this.Controls.Add(this.panel1);
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel3);
This should be able to put all panels as form children.

Panels in same area in winform

I currently have a windows form that I am using as a "holder" for many panels. All of these panels are being used like pages, so say I have a default panel, a details panel etc. They all cover the exact same area and i am using a top menu to have buttons to show/hide the correct panel.
The problem I have with this is that I cannot select one panel and edit it or simply bring it to the front to edit it in "design" mode, I currently have to keep sending the top panel to the back until I get the one I need. This is okay now, but eventually my program will have over 20 panel type pages, and this will get tedious.
Is there a better way to manage this, or is there an easier way than using panels?
All these panels are used like pages
Above line in your question suggests that you should be using TabPage instead of Panels.
Therefore, You can use a TabControl, and add TabPage to it both at design time as well as at runtime easily, and add controls of your choice to the different TabPage(s).
According to wikipedia:-
Tab is one that allows multiple documents to be contained within a single window, using tabs as a navigational widget for switching between sets of documents. which is essentially what you want to do.
As shown in the picture
You can then use Button to switch b/w different TabPage or show/hide TabPage.
It will not only solve your problem of designing at design time, it will also provide a "clean" user interface. Hope that helps!
Check this:-
http://en.wikipedia.org/wiki/Tab_(GUI)
Use a TabControl instead. It will allow you to switch an active panel also in design time.
If you want to keep the panel method there is a way of working around the problem of one panel becoming an element of another. Drag the panel outside the boundary of the other panel (so that it is no longer an element of the other panel) and then re-position it using the arrow keys on your keyboard (i.e. not by dragging it into position over the other panel using the mouse). It seems like the panel will only become an element of another panel if it is dragged into position over the top of another panel using the mouse.

How to make controls stay in a distance of each other when adding them programmatically?

I have a panel, which I add controls programmatically to it. I want each control stay in a far from other controls and not stay on top of them.
for this purpose I can calculate a position for each control based on Panel's size, but it seems a bit odd.
Is there a way to make controls be added in a line and when it ended they be added in another line?
You can use a FlowLayoutPanel to achieve what you're describing. It's under Containers in the ToolBox. Set the direction to horizontal and it will flow from left to right, and wrap when it needs to.
I believe the WrapPanel class does what you're describing in WPF. Or the FlowLayoutPanel in WinForms.
You have a few options. You can use one of the containers such as FlowLayoutPanel or TableLayoutPanel. You can also nest them in each other. And you have to set the Margin property for each control you add to the containers.
Sadly the Windows Forms technology lacks on this part a little, while WPF has a very rich layout system. Even somethings like Margin doesn't always work as expected.

Layout manager in C#

I am trying to design a UI in C# . I come from Java background and am familiar with the different layout managers in Java.
So what I am trying to do is:
I have a Pane. To this pane I wish to add controls one below the other. In Java I would have used a BoxLayout (Y-Axis), and then just added the controls. Also the individual controls/containers could have been further customized by using different layouts for each individual container.
How do I do this in C#?
What I need to do is:
1)Add controls one below the other. Each individual control can be a collection of smaller controls.
So what I can have is something like:
Control 1 here
Control 2 here
Control 3 here
Each control can have its own layout, say BoxLayout on X axis and so on. How do I do all this?
PS: I am using WINFORMS.
I will take a stab that you are building WinForms, and suggest you look at the following controls, which are similar to the Java layout managers;
Panel
FlowLayoutPanel
TableLayoutPanel
A Panel will let you place controls arbitrarily within it, and you use the Dock, Anchor and Location properties to position each child control.
A FlowLayoutPanel will do what you are asking if you set the flow to 'TopDown', but everything will be left-aligned and that can't be changed I believe.
A TableLayoutPanel is going to be the most useful to you I expect. Just create the panel with 1 column, and add each control to a row. The rows can be set to AutoSize to their contents to give you the closest match to a BoxLayout I think.

Resizing the datagrid proportional with the form size

hii
I am a fresher in the c # so i want to know how to resize the datagrid(any other control)aith respect to the form size.
I just added one datagrid in the form then what i have to do?please help me...from the very basic please
For a dynamic layout that scales with your form size you have various options (depending on the complexity of your layout).
The first ones are
Anchoring
Docking.
But you can also work with advanced container controls like
TableLayoutPanel
FlowLayoutPanel
SplitContainer.
Some more informations i already post in an older question.
If you have more concrete problems about how to solve a specific layout problem you should post a new question with your exact problem.
But at a first tip i can say that it is never necessary to use the Resize event and do some size changes on yourself. There is always a solution that can be solved with the above elements.
You have to ways of dong it:
Using a Dock. It works fine and is very easy to use but its limit is that you can stick it to a one side of parent container. So if you want to streach control only in width you will fail.
Using Anchor. It require more configuration but you can specify all four(top, right, down, bottom option separably.
Regards
Szymon
Go to the properties window and scroll down to "Docking" and choose to dock the control in the parent container. This will give you various options about how you want the control to dock. You should put some containers in there of some sort, maybe, if you've got buttons or things you'd like to have show up above/below/next to the grid. Also, if your grid goes behind your other controls, select the control and bring it to the front.
edit:
You need some containers in which to put your buttons / drop-downs. You could use a flow-layout panel (which wouldn't resize its child controls), or you could use a plain panel or the table-layout panel. The table one will let you dock your child controls within each cell of the table, and you can set your columns & rows to auto-size to a percentage of the entire table width. That way everything will autosize accordingly.

Categories

Resources