Reputation: 61
I was working on a simple menu animation in Visual Studio using C# (Windows Form) and wanted to keep track of the controls that are over a panel, in my case - panel5
, in live time. This is the code sample:
void check()
{
if (panel5.Contains(panel3))
MessageBox.Show("3");
else
if (panel5.Contains(panel2))
MessageBox.Show("2");
else
if (panel5.Contains(panel4))
MessageBox.Show("4");
}
I don't know why, but it will always pop up a message box with the message '2'. So the function check()
will always detect that the panel2
is over the panel5
. I use this method every time this 2 events are triggered :
private void PanuIntrareStanga_MouseEnter(object sender, EventArgs e)
{
posibilitate = 1;
backForth++;
timer2.Start();
check();
}
and this one:
private void PanouIntrareDreapta_MouseEnter(object sender, EventArgs e)
{
posibilitate = 2;
backForth++;
timer2.Start();
check();
}
Is there a method to fix this and keep track of the controls that are over panel5
?
Upvotes: 0
Views: 120
Reputation: 5986
Condition alone is not enough here because you want to map everything - you need to loop on the parent controls (panel5) and find all the child controls of type panel he contains, also you can check each child and each grandchild control and so on... try to understand this example:
private void DiscoverPanels()
{
foreach (Control ctrl in panel5.Controls)
{
if (ctrl is Panel)
{
MessageBox.Show(ctrl.Name + " " + "is a child of panel5");
foreach (Control grandchild in ctrl.Controls)
{
if (grandchild is Panel)
{
MessageBox.Show(grandchild.Name + " " + "is a child of " + ctrl.Name);
}
}
}
}
}
Upvotes: 1