How can I arrange controls in my parent form by code?
So far I call the user controls with this code;
Me.ParentForm.Controls.Remove(Me)
controlMain()
I want the user controls arrange itself whenever the user resize the parent form or maximize the form. Currently, I set the controls by,
Public Sub controlMain()
Dim usrctl As New _ctlMain
_Main.Controls.Add(usrctl)
usrctl.Location = New Point(_Main.Width / 2 - usrctl.Width / 2, _Main.Height / 2 -usrctl.Height / 2)
End Sub
which is on a module. _Main is my parent form while _ctlMain is the control being called. I do not intend to put the user control on the parent form during design because I have other user controls to call after a specific function in an active control is called.
I have tried the autosize property of usercontrol but I guess it doesn't work on my application. Usercontrol doesn't have the dock and anchor properties.
Use one of the automatic layout controls, like a FlowLayoutPanel or a TableLayoutPanel.
Instead of adding your user controls to the form itself, add them to either a FlowLayoutPanel or TableLayoutPanel control that has been placed on top of the form using DockStyle.Fill.
It sounds to me like a FlowLayoutPanel is what you want. With that, the layout of the controls is handled entirely automatically, and they are positioned either in left-to-right or top-to-bottom order, depending on the value of the FlowDirection property.
The only reason to choose a TableLayoutPanel is if you need to have more precise control over the exact positioning of the controls. It works just like an HTML table, with each control getting its own "cell".
You can also set the Dock and/or Fill properties of the individual user controls if you'd like to ensure that their sizes are automatically adjusted. For example, you can set each control to fill the entire cell in which it is placed in a TableLayoutPanel.
Related
I have a DataRepeater in which I have a Label (LabelID) and a GroupBox which contains RadioButtons. I am binding the Labels to a column from a DataTable and what I wish to do is to give the user the chance to select one of the Radio Buttons within each cell of DataRepeater and then upon progress I wish to read the user's selection for each cell. The problem is that when the user starts selecting the radio buttons and scrolls down to select radio buttons for other cells within the DataRepeater, the previous selections change or even the ones that user has not selected yet get selected. I have no idea why this is happening.
Here is the code for what I have done:
LabelID.DataBindings.Clear()
LabelID.DataBindings.Add(New Binding("Text", SomeDataTable, "SomeID"))
myDataRepeater.DataSource = SomeDataTable
I added the GroupBox which contains the RadioButtons in the Visual Studio drag and drop framework.
I tried the following binding as something that I thought might solve the problem, but it did not.
GroupBoxSelection.DataBindings.Clear()
GroupBoxSelection.DataBindings.Add(New Binding("Tag", SomeDataTable, "SomeID"))
I know it has something to do with scrolling the DataRepeater up and down. Because I increased the size of the DataRepeater to get rid of the ScrollBar and this strange behavior won't happen anymore. I cannot keep the DataRepeater that big so I would like to find another solution.
Any help will be appreciated?
I don't know how the DataRepeater works exactly but I wouldn't be surprised if it reused controls to increase performance. If so then your controls may be retaining their values as they are moved. What you might try is creating a user control to contain the RadioButtons and expose a single property that you can then bind. If an instance gets reused then the binding should update correctly as it does for the other controls.
In the layout I'm writing, there's an area where a different control is shown, depending on various conditions.
Can I place all of these controls at design-time, or I must "re-attach" and "remove" each control at runtime?
You can host a panel inside a TableLayoutPanel cell. Or any other control container. In that you can put as many controls as you want.
For example, you could use a nested FlowLayoutPanel. So you may not need to manually perform layout management.
I am trying to populate my windows form with new controls and data based on what is read from my database. The left side of the form is a static panel which will not need re sizing but I need to create multiple labels on the right side which requires more space. I added the vscroll control but am having trouble increasing the size of the right side of the form.
To use a scroll-bar will require a semi-low-level implementation where you need to always update the view by repositioning the elements, calculating your scroll-bar in relation to total view, what elements would be visible and so forth.
A better solution in this case will probably be to add a Panel control on the right which is docked (f.ex. Fill) and then set the AutoScroll property to True.
This way you leave all the "low level" stuff to the Panel control and you can add and position the elements you need to the Panel's Controls collection instead.
My main form has two panels, left docked and right docked. The right side panel has two child panels with top dock and bottom dock settings. The usercontrol is added to the right side top panel.
My usercontrol has a panel and a label. The panel is anchored on all 4 sides, the label is anchored on all except the bottom. At runtime I create this usercontrol and set it to dockstyle=fill and then I add it to my top right panel.
With everything set to "fill" I expect that when I add my usercontrol to the panel it will take on the appropriate width and height and pass that info to the child controls (labels) inside of my usercontrol.
My problem is that this stretching of the size does not happen when I create my objects during the Load event on my usercontrol. Even though initializecomponent has ran for the usercontrol the panel inside of it (4 corners anchored) has not taken the x/y values of the available space. As a result my usercontrol shows up about 50% of the width I want.
Lets say that instead of creating objects during usercontrol load that I instead start a timer and have the timer call my create routine when it raises the tick event. When I do things like this my objects are created with the full width/height that I expect. The only issue here is that this causes a delay in my interface.
Can someone help explain this behavior? My mainform is calling a "load gui" routine which is instantiating usercontrols, setting panel sizes, and then adding usercontrols to those panels. This particular user control is the last to load into the panels from that load gui routine so it does not make sense that the parent panel width/height would not be known yet. This is one of my first apps where I am purposely trying to use dockstyle=fill to keep things consistent across different main form sizes without writing all the extra size_changed code handlers. I'm sure this one is easy to work around once I know where the problem lies.
Thanks for any help provided!
this turned out to be a padding issue on the parent usercontrol. I also had to allow a bit of wiggle room to make sure that the controls didn't overflow the panel so I did a parent.width - 15 and that along with the padding made everything work much better.
From the post "Growing user control not updating"...
Using C#, .Net 2.0 in a Windows environment.
UserControl1 - draws cells to a bitmap buffer dependent upon NumberOfCells property
UserControl2 - panel contains UserControl1 which displays vertical scroll when necessary; also contains NumberOfCells which sets UserControl1's NumberOfCells.
Formf1 - contains NumericUpDown controls (just increments) which updates the UserControl2 - suppose to!
When I increment the control on the form by say 20, UserControl1 adds the necessary cells, UserControl2 displays the vertical scroll bar accordingly, BUT the form does not 'redraw' to the updated/correct image!! Meaning, after I increment by 20, cells are added, vertical scrool bar added... but the image shown is just everything else expanding.
I reset the control to scoll to the very TOP and the scrolling works, but the image is still staic... UNTIL I resize my form, more specifically, when I change it from maximize to window or vice versa!!!
What can I do to 'reset/redraw' the correct image???? Thank you in advance.
Lawrence
If this is C# and Windows Forms I would try calling Update() or Refresh() on the control to make it redraw itself.
Try calling Refresh in the Scroll events and the NumberOfCells Property.