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
Related
I have created a form that shows data and the filter that has checkbox in it
combobox
I have used a Button and a Panel to do this and found that the panel is only shown in the parent's panel not float like the combobox
or panel can't do like this?
Regards
You want something that cannot be done with parented components (like panels)
I see some options:
(1) owner draw a combobox to include checkboxes, see Codeprojects https://www.codeproject.com/Articles/31105/A-ComboBox-with-a-CheckedListBox-as-a-Dropdown
or
(2) Create a small floating popup form without system menu and a single pixel border. Inside the form, you could place a CheckListBox docked Fill to allow for filter checkboxes for each item.. you may have a look at this topic Is there a simple way to implement a Checked Combobox in WinForms
(3) Both solutions have drawbacks. Actually it may be better to avoid it, find another way to specify your filter options, redesign your UI.
I want to set a certain property (Anchor) of all the controls in my main form at once.
There are around 100 controls and I really don't want to change this property for each single control manually.
I know I can select all available controls at once by typing Ctrl + A. The problem then occuring is that the desired property I want to change is not visible in the Properties window. And normally it should be visible because all the controls are a type of Control, shouldn't it?
I also know that I could do it like this:
foreach(Control ctrl in myForm.Controls)
{
ctrl.Anchor = AnchorStyle.Bottom;
}
But I want to know if thers's a way of doing this using the Designer. Is there any?
Usually if controls derive from the same base, you can select them all at once (using the mouse click and drag or holding down ctrl or shift while selecting them one by one), and then you can set any property that they all share from the base class.
You can multi-select all controls on the form and see the Anchor property in the Property Grid. When you edit that with multiple controls selected, each selected control will be set to the Anchor value you specify.
But be careful with Ctrl-A--it will select visual controls as well as non-visual components. So if you have any components on your form that don't render in the client area of the form (such as the Timer or FolderBrowserDialog form components), the Ctrl-A will continue to show common properties--but because these components don't have an Anchor property, the Anchor property won't appear. The only properties to appear when multiple controls on a form are selected are those share by all selected controls.
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.
I'm trying to create a DataGridView cell class which hosts a control (all the time, not just while editing). So far, my approach has been to add the control to the grid and try to synchronise its position with that of the cell.
Would it be possible instead to keep the cell offscreen, route mouse and keyboard events to it, and paint it onto the cell?
UPDATE: By 'offscreen', I don't mean that it should be added to another control such that it isn't displayed; I mean that it should never be added to another control at all.
You can create a control without adding it to the form, then using it as the Cell editor whenever you need to. Usually with grids, when you click on a cell to edit it, it's going to either create a new control and put it in the right place, or it's going to use an existing control. You can make this process a lot easier by creating your own custom cell / column types. See this MSDN page: http://msdn.microsoft.com/en-us/library/7fb61s43.aspx.
Most grids (including DataGridView and 3rd Party Grids) have a facility for adding custom cells.