6 Replies - 390 Views - Last Post: 21 July 2014 - 04:16 AM Rate Topic: -----

#1 martynball  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 21-July 14

Controls inside Panel stop the Panel's hover code from running!

Posted 21 July 2014 - 03:09 AM

Hey, I have got a Panel, which has got controls inside it. These controls remove the hover state from the Panel. I have tried to give these Controls the same hover function as the Panel, and then tried to detect it's type and get it's parent if it isn't a Panel.

I couldn't get that to work however, can't really find much on Google which matches my case. I want the code to be "dynamic", so, no hard coded Panel names. There will be multiple panels which all do the same thing when moused over, and I also want to be able to put multiple layers of controls in the panel.

Here is the code I have so far, but it only accounts for 1 level of controls. So for example:

1 Level
1. Panel
  2. PictureBox
  2. Label



2 Level
1. Panel
  2. PictureBox
  2. Label
  2. TableLayoutPanel
    3. PictureBox (in table)



C# Code (currently assigned to the panel and controls click event for testing)
private void panel1_MouseHover(object sender, EventArgs e)
        {
            var nsender = sender.GetType().ToString();
            String[] split = nsender.Split(".".ToCharArray());
            if (split[split.Length-1] != "Panel")
            {
                MessageBox.Show("Not Panel, go to Parent!");
            }
            else
            {
                MessageBox.Show("Is the Panel!");
            }

            //var panel = (Panel)sender;
            //panel.BackColor = Color.FromArgb(35, 35, 35);
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Controls inside Panel stop the Panel's hover code from running!

#2 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: Controls inside Panel stop the Panel's hover code from running!

Posted 21 July 2014 - 03:25 AM

hi you can simply get the type of control using methods/functions available on dot net.
try using something like this:
if(sender is Panel)
{
    //Do something
}
else
    //Do something else


This post has been edited by jimzcoder: 21 July 2014 - 03:26 AM

Was This Post Helpful? 1
  • +
  • -

#3 martynball  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 21-July 14

Re: Controls inside Panel stop the Panel's hover code from running!

Posted 21 July 2014 - 03:29 AM

Didn't think of that, thanks. How would I do the opposite?

if(sender is not Panel)
{
    //Do something
}
else
    //Do something else



So then I can use a while loop to loop through the objects

This post has been edited by andrewsw: 21 July 2014 - 03:57 AM
Reason for edit:: Removed previous quote

Was This Post Helpful? 0
  • +
  • -

#4 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: Controls inside Panel stop the Panel's hover code from running!

Posted 21 July 2014 - 03:38 AM

just enclose with parenthesis.
it would look something like this.
if(!(sender is Panel))


Hope it keeps you going.
Good luck.
Was This Post Helpful? 1
  • +
  • -

#5 martynball  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 128
  • Joined: 21-July 14

Re: Controls inside Panel stop the Panel's hover code from running!

Posted 21 July 2014 - 03:45 AM

View Postjimzcoder, on 21 July 2014 - 03:38 AM, said:

just enclose with parenthesis.
it would look something like this.
if(!(sender is Panel))


Hope it keeps you going.
Good luck.


Ah thanks, i'm used to PHP so still getting used to the C# equivalents.

Now, shouldn't this work?

private void panel1_MouseHover(object sender, EventArgs e)
        {
            while (!(sender is Panel))
            {
                sender = sender.Parent;
            }


            //var panel = (Panel)sender;
            //panel.BackColor = Color.FromArgb(35, 35, 35);
        }



I'm getting an error from .Parent saying that the object doesn't have a definition for Parent.
Was This Post Helpful? 0
  • +
  • -

#6 jimzcoder  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • Posts: 335
  • Joined: 14-November 12

Re: Controls inside Panel stop the Panel's hover code from running!

Posted 21 July 2014 - 03:58 AM

yes it should throw an exception because you are directly calling a control function from an object.
you should try it this way
//try converting it into a control first then get its parent
while (!(sender is Panel))
            {
               Control cntrl = (Control)sender;
               //get the parent by using
               MessageBox.Show(cntrl.Parent.ToString());
            }



Was This Post Helpful? 1
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3818
  • View blog
  • Posts: 13,533
  • Joined: 12-December 12

Re: Controls inside Panel stop the Panel's hover code from running!

Posted 21 July 2014 - 04:16 AM

This is a slight variation:
sender = ((Control)sender).Parent;

But WinForms do not support event-bubbling so the while loop will not be entered. When you hover over any control within the panel it will not be firing the panel's hover event.

You will have to attach the same event-code to all of the child elements in the panel.

An alternative EventBroadcastProvider is discussed here, although I haven't tried it myself.

..or switch to WPF.

This post has been edited by andrewsw: 21 July 2014 - 04:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1