How would I find out to which control a given control is docked against...

  • Thread starter Thread starter Yasutaka Ito
  • Start date Start date
Y

Yasutaka Ito

Hi,

Is there a way to find to which control the current control is docked
against?

For example, let's say I have panel1 and panel2 docked to left within a
form. The panel1 is the first one on the left and the panel2 is the second
one docked against panel1. I want to be able to find out to which
control/form the specified panel.

I wish, Microsoft had provided a feature similar to the following.

System.ComponentModel.IContainer DockedAgainst for all the control.

Would appreciate any input.

thanks!
-Yasutaka
 
Hi Yasutaka Ito

You mean this one?

Myparent = MyControl.Parent.Name.ToString

I hope this was what you where looking for?

Cor
 
Oops, seems like I wasn't explaining it properly.
By saying 'dock', I meant, setting the control's Dock property.

So, according to the example I gave in my previous post, I'm setting the
Dock property of panel1 and panel2. The panel1 is docked against the left of
its container (the form), and the panel2 is docked against the panel1.

The results I'm expecting from the above example is.... when I'm given
panel2, I should be able to find out as a result that it is docked against
panel1

thanks!
-Yasutaka
 
Hi Yasutako,
So, according to the example I gave in my previous post, I'm setting the
Dock property of panel1 and panel2.
The panel1 is docked against the left of
its container (the form), and the panel2 is docked against the panel1.

So the parent from panel1 is the form
and the parent from panel2 is panel1 I asume or maybe also the form
The results I'm expecting from the above example is.... when I'm given
panel2, I should be able to find out as a result that it is docked against
panel1

So you want to know

if (panel2.docked != Dockstyle.None)

What do I see wrong?

Cor
 
wow ! I thought Yasutako was pretty clear, but lets try it like this:

given a form that contains a number of panel objects ( all panels have the
form as parent ) and all panels have docked == Dockstyle.Left - how does any
given panel determine which other panel is directly to its left ?
 
I'm not sure, but I think the dock is refered always to the "container".
When an control is docked left means that the distance between the container
left border and the control left border is always constant
If you want to know which control is docked left of an other control, I
think you have to calculate all the distance between this control and all
the other controls inside the container.
 
Hi Yasutaka,

There is no method in the frame work to do that.
However you can use the way the control layouts its children and found it
yourself.
Controls starts layouting the contols from the last in the Controls
collection moving backwords to the index 0. So if you can get the index of
the control in question and move form this index to the end of collection.
The control you are looking for is the first sibling with the same value for
the Dock property.

If no control is found that means the control is docked against the edge of
its parent.
The check doesn't make sense if the Dock is set to Fill or None because in
the first case it is docked against the all surrounding siblings; the second
case is obvious.

this is an example of how to do it:

public Control DockAgainst(Control ctrl)
{
if(ctrl.Dock == DockStyle.None || ctrl.Dock == DockStyle.Fill)
{
return null;
}
Control parent = ctrl.Parent;
int index = parent.Controls.GetChildIndex(ctrl);

for(int i = index + 1; i < parent.Controls.Count; i++)
{
Control nextCtrl = parent.Controls;
if(ctrl.Dock == nextCtrl.Dock)
{
return nextCtrl;
}
}

return null;
}
 
This is it! This is it! Cool! Thanks!!

Thanks a lot all of you for the big hands. Feel strong to have such fellow
developers.
Have a great day.

thanks!
-Yasutaka
 
I don't know if there's an easier way, but here's one way. Use the
Control.GetChildAtPoint() method on the form or container. This returns the
name of a control docked above textBox1, which is docked to the top.

One problem with it, though, is that if the controls are on a panel, it just
returns the panel. If they're on the form, it returns the control you want.
There may be a way around the panel issue, but I haven't tried to find it.

Hope this helps,

Dale

private void button2_Click(object sender, System.EventArgs e)
{
MessageBox.Show(this.GetChildAtPoint(new Point(textBox1.Left,
textBox1.Top-1)).Name);
}
 
Back
Top