Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. Incapable of listening for Mouse Events on UserControl

Incapable of listening for Mouse Events on UserControl

Scheduled Pinned Locked Moved Windows Forms
helpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Draekz
    wrote on last edited by
    #1

    I've searched this forum and just simply can't find an answer already written out. I am creating a user control using the designer. The control only paints stuff like lines and colors within its bounds using OnPaint and OnPaintBackground. Now, I need to respond to mouse clicks, yet I cannot get the control to respond to any mouse clicks. if I do Capture = true, then it will listen to 1 single event, and then Capture is lost and i cannot click again. When I do capture, the mouse cursor turns into an hourglass anyway, which I dont want. How does one listen to events of a UserControl??? It seems this should work simply... but it does not. I am adding this control into a Table control which is in a form. There really isn't much more to it than that. I have a deadline and I'm just stumped on something that should be so ridiculously simple... yet its not. Here is the basic class I'm writing, its just a UserControl created from the visual designer. I've got breakpoints on all those mouse events and NOT ONE fires. PLEASE HELP!

    public partial class HomeHeader : UserControl
    {
    public HomeHeader()
    {
    InitializeComponent();
    }

    protected override void OnClick(EventArgs e)
    {
    base.OnClick(e);
    }
    
    protected override void OnMouseDoubleClick(MouseEventArgs e)
    {
    base.OnMouseDoubleClick(e);
    }
    
    protected override void OnMouseDown(MouseEventArgs e)
    {
    base.OnMouseDown(e);
    }
    
    protected override void OnMouseMove(MouseEventArgs e)
    {
    base.OnMouseMove(e);
    }
    
    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
    }
    

    }

    L 1 Reply Last reply
    0
    • D Draekz

      I've searched this forum and just simply can't find an answer already written out. I am creating a user control using the designer. The control only paints stuff like lines and colors within its bounds using OnPaint and OnPaintBackground. Now, I need to respond to mouse clicks, yet I cannot get the control to respond to any mouse clicks. if I do Capture = true, then it will listen to 1 single event, and then Capture is lost and i cannot click again. When I do capture, the mouse cursor turns into an hourglass anyway, which I dont want. How does one listen to events of a UserControl??? It seems this should work simply... but it does not. I am adding this control into a Table control which is in a form. There really isn't much more to it than that. I have a deadline and I'm just stumped on something that should be so ridiculously simple... yet its not. Here is the basic class I'm writing, its just a UserControl created from the visual designer. I've got breakpoints on all those mouse events and NOT ONE fires. PLEASE HELP!

      public partial class HomeHeader : UserControl
      {
      public HomeHeader()
      {
      InitializeComponent();
      }

      protected override void OnClick(EventArgs e)
      {
      base.OnClick(e);
      }
      
      protected override void OnMouseDoubleClick(MouseEventArgs e)
      {
      base.OnMouseDoubleClick(e);
      }
      
      protected override void OnMouseDown(MouseEventArgs e)
      {
      base.OnMouseDown(e);
      }
      
      protected override void OnMouseMove(MouseEventArgs e)
      {
      base.OnMouseMove(e);
      }
      
      protected override void OnMouseUp(MouseEventArgs e)
      {
          base.OnMouseUp(e);
      }
      

      }

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, I have no trouble getting normal mouse events on a form holding two instances of this little UserControl1, which holds a ListBox, a ComboBox and a Label. The Label text gets updated for the one UserControl1 instance my mouse is hovering over, as long as I'm not over one of its internal Controls (that's why I'm having the ListBox and ComboBox).

      public partial class UserControl1 : UserControl {
      	public UserControl1() {
      		InitializeComponent();
      	}
      
      	private void log(string s) {
      		label1.Text=DateTime.Now.ToString("mm:ss.fff ")+s;
      	}
      
      	private void UserControl1\_MouseMove(object sender, MouseEventArgs e) {
      		log("MouseMove");
      	}
      

      or

      public partial class UserControl1 : UserControl {
      	public UserControl1() {
      		InitializeComponent();
      	}
      
      	private void log(string s) {
      		label1.Text=DateTime.Now.ToString("mm:ss.fff ")+s;
      	}
      
      	protected override void OnMouseMove(MouseEventArgs e) {
      		log("MouseMove");
      		base.OnMouseMove(e);
      	}
      }
      

      The main difference with your code is my MouseMove handler contains real code, it does not just call base.OnMouseMove(). Instead it shows a little string in a label. IMO base.OnMouseMove() does nothing by default, an empty UserControl simply has no functionality. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read formatted code with indentation, so please use PRE tags for code snippets.


      I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


      D 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, I have no trouble getting normal mouse events on a form holding two instances of this little UserControl1, which holds a ListBox, a ComboBox and a Label. The Label text gets updated for the one UserControl1 instance my mouse is hovering over, as long as I'm not over one of its internal Controls (that's why I'm having the ListBox and ComboBox).

        public partial class UserControl1 : UserControl {
        	public UserControl1() {
        		InitializeComponent();
        	}
        
        	private void log(string s) {
        		label1.Text=DateTime.Now.ToString("mm:ss.fff ")+s;
        	}
        
        	private void UserControl1\_MouseMove(object sender, MouseEventArgs e) {
        		log("MouseMove");
        	}
        

        or

        public partial class UserControl1 : UserControl {
        	public UserControl1() {
        		InitializeComponent();
        	}
        
        	private void log(string s) {
        		label1.Text=DateTime.Now.ToString("mm:ss.fff ")+s;
        	}
        
        	protected override void OnMouseMove(MouseEventArgs e) {
        		log("MouseMove");
        		base.OnMouseMove(e);
        	}
        }
        

        The main difference with your code is my MouseMove handler contains real code, it does not just call base.OnMouseMove(). Instead it shows a little string in a label. IMO base.OnMouseMove() does nothing by default, an empty UserControl simply has no functionality. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        I only read formatted code with indentation, so please use PRE tags for code snippets.


        I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


        D Offline
        D Offline
        Draekz
        wrote on last edited by
        #3

        Sigh... Everyone ignore this thread... I am embarrassed to say that somehow i managed to not notice a large Image box in the user control designer that wasn't supposed to be there that was transparent and taking all the clicks. everything works as it should now... Sigh...

        L 1 Reply Last reply
        0
        • D Draekz

          Sigh... Everyone ignore this thread... I am embarrassed to say that somehow i managed to not notice a large Image box in the user control designer that wasn't supposed to be there that was transparent and taking all the clicks. everything works as it should now... Sigh...

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          So my ListBox and ComboBox were a hint for you then. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups