Anchor listbox problem

R

Rotsey

Hi,

I have a Listbox in a Panel of SplitContainer.

I want to anchor it so the listbox grows/shrinks vertically with the
resizing the splitcontainer.

This is my code
What happens is it sizes the listbox horizontally about nearly the same size
as the split container width.


_TableList = new MdsListBox();

_TableList.Name = "TableList";
_TableList.Left = 10;
_TableList.Top = 40;
_TableList.Width = 150;
_TableList.Height = 100;
_TableList.Anchor = (AnchorStyles.Left | AnchorStyles.Right |
AnchorStyles.Bottom | AnchorStyles.Top);

I want the size of the listbox set here retained.

rotsey
 
M

Marc Gravell

I want to anchor it so the listbox grows/shrinks vertically with the
resizing the splitcontainer.
What happens is it sizes the listbox horizontally about nearly the same size
as the split container width.

If you only want it to grow vertically, then anchor it left or right,
but not both.

Marc
 
M

Marc Gravell

By anchoring it left | right, you are telling it to keep the left edge a
fixed distance from the container's left edge, and the right edge a
fixed distance from the container's right edge. Ihis means that it will
grow and shrink horizontally along with the container. If you don't want
it to track (grow and shirnk) the right hand edge, then don't anchor it
to the right hand edge.

Marc
 
R

Rotsey

but now the listbox is anchored but part is not visiible under the second
panel

the listbox is the top panel

why can't it just stay where i set it to.....is this rocket science MS?
 
M

Marc Gravell

I don't understand what you mean "part is not visible under the second
panel"... it is starting at the height you give it: so give it the right
height! Or alternatively, set Dock to Left (much easier than managing
all the details manually).

A quick (C# 3) demo:

using System;
using System.Drawing;
using System.Windows.Forms;

static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.Run(new Form
{
Text = "Split Demo",
Controls =
{
new SplitContainer() {
Dock = DockStyle.Fill,
Orientation = Orientation.Horizontal,
Panel1 = {
BackColor = Color.Green,
Controls = {
new ListBox {
Dock = DockStyle.Left,
IntegralHeight = false,
BackColor = Color.Red,
Items = {
"Foo", "Bar", "Blip", "Blap", "Bloop"
}
}
}
}, Panel2 = {
BackColor = Color.Blue
}
}
}
});
}
}
 
R

Rotsey

That does not compile

But it is not what I want

I have more just that control in the top panel of the splitcontainer.

So I have set the poistion and size of where I want the lb to start
and set the Anchor.Left , top bottom as you say.

But the bottom of the lb is not visible it is under the bottom panel

any more info would be grateful Marc.
 
M

Marc Gravell

That does not compile

Yes it does - with a C# 3 compiler (I did state it was C# 3...).
I have more just that control in the top panel of the splitcontainer.
...
But the bottom of the lb is not visible it is under the bottom panel

OK; how are you setting up the form? Are you using the Visual Studio
designer? If so, you should be able to simply resize (drag) the ListBox
as needed, and then set the Anchor...

Are you doing something different?

Marc
 
R

Rotsey

I have a farily busy form

A splitContainer horizontal
In the right panel a tab control
then in a tap page I have my split container vertical
The in the top panel a another panel
It is in this panel of that I have my lb and other controls

Everything from the tabcontrol is added dymanically.

One issue I did have was setting the splitterdistance of the vertical
control
did not seem right I had to set it to 10. The panel hieght with its control
is actaully 145

I changed lb height to 5 and it is still actually about 100 and the bottom
is not visible.

Any ideas?
 
M

Marc Gravell

Up to you... but the trick is to initialise the child controls with
sizes relative to their container.

Something like:

using System;
using System.Drawing;
using System.Windows.Forms;

static class Program
{

static void AddChild(Control parent, Control child, AnchorStyles
anchor) {
child.Left = parent.Padding.Left + child.Margin.Left;
child.Top = parent.Padding.Top + child.Margin.Top;
child.Height = parent.ClientSize.Height - child.Top
- parent.Padding.Bottom - child.Margin.Bottom;
child.Width = parent.ClientSize.Width - child.Left
- parent.Padding.Right - child.Margin.Right;
child.Anchor = anchor;
parent.Controls.Add(child);
}
[STAThread]
static void Main()
{

const AnchorStyles AnchorAll = AnchorStyles.Top |
AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right,
AnchorTopBottomLeft = AnchorStyles.Top |
AnchorStyles.Bottom | AnchorStyles.Left;

Application.EnableVisualStyles();

Form form = new Form();
SplitContainer vSplit = new SplitContainer();
vSplit.Orientation = Orientation.Vertical;
vSplit.Panel1.BackColor = Color.Tomato;
vSplit.Panel2.BackColor = Color.PowderBlue;
vSplit.SplitterWidth = 5;
AddChild(form, vSplit, AnchorAll);

TabControl tabs = new TabControl();
AddChild(vSplit.Panel2, tabs, AnchorAll);

TabPage page = new TabPage("Test");
tabs.TabPages.Add(page);

SplitContainer hSplit = new SplitContainer();
hSplit.Orientation = Orientation.Horizontal;
hSplit.SplitterWidth = 5;
hSplit.Panel1.BackColor = Color.PaleGreen;
hSplit.Panel2.BackColor = Color.PaleTurquoise;
AddChild(page, hSplit, AnchorAll);

ListBox lb = new ListBox();
lb.Items.AddRange(new object[] { "Foo", "Bar", "Blip", "Blop",
"Bleep" });
lb.IntegralHeight = false;
AddChild(hSplit.Panel1, lb, AnchorTopBottomLeft);

Application.Run(form);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top