|
I need to create a User Control containing DataGrid with customers and below it two fields with labels to enter search criteria for these customers.
I would like this User control to be resizable in the parent application (preferably in Designer mode) in this way: the search criteria TextBoxes are always at the bottom of the control and DataGridView panel is streched to fill the rest of the User Control space. I can't find a way to achieve this.
Split container (text boxes in bottom and grid in top) is wrong because when the control is streched in the parent form during design or during runtime, both panels stretch and the space between grid and text boxes grows.
Docked panel at bottom with textboxes in it and Dock=Bottom would be OK, but there is no way to tell the datagrid to dock to the rest of space. When datagrid is docked to Top, its height is fixed, does not grow when the control size grows in the parent form. On the other hand, when the dock is Fill, the datagrid overlaps with bottom panel. Same when instead of bottom panel I use toolstrip.
So, is it possible to do at all ??
I can ask the User Control to calculate the size of all its elements during runtime, but it would have to be a separate public function in the User Control. It cannot be calculated in the control constructor because in the constructor the final size is not yet known - it is determined in the constructor of the parent form. Besides of the overhead of calculating everything, it is not elegant, because of the additional call necessary to draw the control.
|
| monetia Friday, February 01, 2008 2:11 PM |
Ah hah! I believe I discovered your problem. (note that I said First drop the panel)
Open the Document Outline. You should see your panel1 and dataGridView1 under the userControl1 root. My guess is that yous lists the panel1 first. Select it in the Document Outline and press the down arrow button at the top of the Document Outline window. This will move it so that it follows the dataGridView.
|
| Tergiver Friday, February 01, 2008 8:26 PM |
First drop a Panel onto the control and set it to dock at the bottom.
Next drop the DataGridView above the panel and set it to dock Fill.
Now you can set the Panel's height to whatever you want and stick the text boxes (and whatever else) in the Panel. |
| Tergiver Friday, February 01, 2008 2:42 PM |
I know I can set the Panel's height as needed, but the problem is that the bottom of the grid is hidden behind the bottom panel.
Both during runtime and during design.
It looks like setting Dock=Fill fills the whole control and not control minus bottom panel (which has Dock=bottom)
You can check it in designer: after docking both panel and grid, the height of the grid = height of the whole control (and I expect it should = height of control minus height of bottom docked panel)
|
| monetia Friday, February 01, 2008 7:21 PM |
I did check it. It worked just fine for me. I went back and did it again and added the following handler to the dataGridView1's Paint event:
Code Snippet private void dataGridView1_Paint(object sender, PaintEventArgs e) { base.OnPaint(e); Rectangle rect = new Rectangle(0, 0, dataGridView1.Width, dataGridView1.Height); e.Graphics.DrawRectangle(new Pen(Color.Aqua, 6), rect); }
The bright blue rectangle shows up exactly where I expect it to: around the dataGridView which does not overlap the panel.
* Edit - That handler is in the UserControl. The user control is docked on a form with Dock = Fill so that I could verify that the user control's contents behaved as expected when resized (by resizing the form). They do. |
| Tergiver Friday, February 01, 2008 8:11 PM |
Ah hah! I believe I discovered your problem. (note that I said First drop the panel)
Open the Document Outline. You should see your panel1 and dataGridView1 under the userControl1 root. My guess is that yous lists the panel1 first. Select it in the Document Outline and press the down arrow button at the top of the Document Outline window. This will move it so that it follows the dataGridView.
|
| Tergiver Friday, February 01, 2008 8:26 PM |
Thanks a lot!! I somehow forgot to drop panel first, but did as you suggested in the Document Outline and now all is OK.
|
| monetia Friday, February 01, 2008 8:35 PM |
BTW, the explaination can be found here: http://msdn2.microsoft.com/en-us/library/system.windows.forms.control.dock.aspx
The relevant bit is:
"Controls are docked in their Z-order, which is the visual layering of controls on a form along the form's Z-axis (depth)."
So you have to have non-Fill controls specified first (last in the document outline which is weird, but what can you do). Then the last control says, "Fill", which means, "fill up the remaining space". |
| Tergiver Friday, February 01, 2008 9:07 PM |