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)
Related
I am designing a windows form in C# and there is a checkbox called "electronic delivery". Under it is a field for "email address". Now I would like to add the option that the email field is only visible if checkbox is checked.
If checkbox is not checked, I would like to have a different field there.
I know how to handle this on the code side, to make things visible/hidden, but how do I place the fields in the form? Should I place one on top of another? Then I won't be able to access the one below to change it's properties.
Or should I keep only one filed and change it's name inside the code?
You could place your controls in FlowLayoutPanel control. The FlowLayoutPanel control arranges its contents dynamically in a horizontal or vertical flow direction.
You could just place them one on top of the other as you suggested. You said you did not want to do this because it would make selecting the properties of the controls difficult.
You can select a control from the combobox drop down menu on the properties section of Visual Studio.
You should create two Different Panels and Add Objects based on the Requirement. After Put visible and Hide Code in the Check Box Checked Event.
Try Panels it Will Work.
You can Simple Move Panel Along with all you objects which make it super Easy.
Is there a simple way to change the order in which controls are displayed within a panel? I do not mean Z-Index. I mean explicitly setting the 'flow order'. Thank you.
The StackPanel and the Grid use a Children property of type UIElementCollection
By using Remove(At) and Insert you could change the order of the elements in the collection.
After that it depends on the Panel (Grid, Stack, Dock, ...) whether or not, and if how, it uses this order.
Some Panels, such as the Grid, also use attached properties (Grid.Column, Dock.Top) to position elements so there is no single way of re-ordering for all panels.
If the elements are added dynamically through data binding, re-ordering the data items in their collection might cause the Panel to display the controls in a different order.
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
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.
In our project, SharpWired, we're trying to create a download component similar to the download windows in Firefox or Safari. That is, one single top down list of downloads which are custom controls containing progress bars, buttons and what not.
The requirements are that there should be one single list, with one element on each row. Each element must be a custom control. The whole list should be dynamically re-sizable, so that when you make it longer / shorter the list adds a scroll bar when needed and when you make it thinner / wider the custom controls should resize to the width of the list.
We've tried using a FlowLayoutPanel but haven't gotten resizing to work the way we want to. Preferably we should only have to set anchoring of the custom controls to Left & Right. We've also thought about using a TableLayoutPanel but found adding rows dynamically to be a too big overhead so far.
This must be quite a common use case, and it seems a bit weird to me that the FlowLayoutPanel has no intuitive way of doing this. Has anyone done something similar or have tips or tricks to get us under way?
Cheers!
/Adam
If you don't want to use databinding (via the DataRepeater control, as mentioned above), you could use a regular Panel control and set its AutoScroll property to true (to enable scrollbars).
Then, you could manually add your custom controls, and set the Dock property of each one to Top.
.NET 3.5 SP1 introduced a DataRepeater Windows Forms control which sounds like it'd do what you want. Bind it to the list of "downloads" (or whatever your list represents) and customize each item panel to include the controls you need.