How i can place multiple controls in a particullar cell of TableLayoutPanel - c#

I'm using a TableLayoutPanel control and in my scenario I have to place two controls inside of one particular cell, is that possible? If so, please elaborate.

You should use a Panel or any other content control inside that cell and then you'll be able to add many controls inside it.

Put a tablelayout panel in the cell and give it two columns and one row. There's your two "cells".

Put a Panel inside the cell and in that panel control you can place as many controls as you want

Try something like this:
1) Create user control and place all control you want on it and than add new control in the cell
2) Put controls in the panel then put panel in the cell

Related

Possible to sort Labels in FlowlayoutPanel

I have created a FlowLayoutPanel where labels are added dynamically, the label data is taken from a datagridview. Is it possible to sort those labels using a Date column from the datagridview?
A flow layout panel will keep it's child controls in the order that they are added to it.
This means that you can't sort the controls on it.
Your only options are either to remove all the labels and add then in a different order, or to use a regular panel and then sort the existing label by changing the location property of all the labels.
The first option is, of course, much easier.
It is possible to order items of any container using the SetChildIndex method.
Here is an example of how to move an item up one place:
myControl.Parent.Controls.SetChildIndex(
myControl, myControl.Parent.Controls.GetChildIndex(myControl) - 1)

Having a certain layout when adding labels to a Panel

I have a WinForm with a Panel and I keep adding Labels to it. Is it possible to set the Panel so that everytime I add a label it will have a certain layout ? The layout I am looking for is having a single column of Labels, so everytime I add a new Label it will be added to the next row. I haven't found any property for the Panel to do that. Is this possible ?
Use FlowLayoutPanel instead of Panel. And set the FlowDirection property to TopDown
You can create a ListBox in the panel, and then add the Labels to it, rather than to panel directly.
Use a TableLayoutPanel and set its ColumnCount to 1.

C# DragDrop; How to retrieve the source's parent?

I have a WinForm with 3 different groupboxes. Furthermore my WinForm contains a tablelayoutpanel with another panel in each cell that can contain objects.
When dragging these objects into a groupbox I should trigger an action based on the cell of the tablelayout the object came from.
The problem is that I cannot find a way to determine the parent panel of the dragged object.
How can I best do this?
You can do something like below to access a cell in particular the cell panel. And then save this panel instance to a public variable and acsess while you perform drag over,drag enter or wherever you need it.
var currentlySelected = layoutPanel.Cell(x,y).Controls[0] as Panel

How do I keep my winforms controls from overlapping when I expand the application?

I have a pretty simple winforms application. It contains a rich edit box, embedded browser, progress bar, a few buttons etc.
I have the anchors on the controls set to expand in all directions.
When expanding up and down, however, the controls will overlap one another. How do I prevent this from happening?
Thanks
Your best bet is to add a TableLayoutPanel to your form which contains the "layout grid" this should be docked to the form, then you can add your controls into the cells in the table (they can cover multiple rows and columns so you can get the layout you want).
You must set the property Autosize=true on every control, especially on the main form.
Note that some controls like TabControl have this property, but you can't see it with intellisense (Attribute Browseable=false).

Adding to a Scrollable Panel with Location

I am using a Panel to hold a list of controls (user-defined). The way that I add the panels, I am setting the location of the control based on the Panel.Controls.Count before I add it to the panel.
comRec.Location = new Point(comRec.Location.X, panel1.Controls.Count * 25);
panel1.Controls.Add(comRec);
Now, this works nicely and looks exactly the way that I want it to. However, once we reach the limit on the window, the AutoScroll enables (which I do want). Now, if the user were to scroll to the bottom of the Panel, this ultimately changes the location of every control in the panel. Instead of my first comRec.Location being (0,0), it is something like (0,-219). So now, when the user adds another comRec object, it creates a HUGE gap between the objects.
My question is this, what is the best way to account for the changes of the location with the scrollbar and still using my adding system. I am assuming that will have to do something with checking the value of the scrollbar and using it to determine the location.
Also, is there a BETTER way to display a list of controls? Should I be using a Panel?
Look at the FlowLayoutPanel control, it's exactly what you what.
You could add an additional panel into the hierarchy:
Outer panel (scrollable)
Inner panel (not scrollable, resize it whenever you add a control)
User Defined Control 1
User Defined Control 2
User Defined Control 3
User Defined Control 4
...
This way, your additional controls' locations would be relative to their direct parent, the non-scrolling panel.
If you add several controls, try to suspend the layout of the panel while adding the controls:
panel1.SuspendLayout();
// Add controls ...
panel1.ResumeLayout();
This helped me in a similar situation where the user could change dynamically the visibility of existing controls.

Categories

Resources