2

Initially i was having a picture box which can be moved on the form by user from one place to another. I have handled the events for the picture box and it was moving perfectly.

But now user wants to display a text below the picture. So I thought to create a custom control dynamically and add that picture box and a label control inside the user control.

I also set the dock properties of controls to TOP and Bottom. Now my user control is completely covered with the sub controls.

After that i want to handle the mouse events for the user control. But unfortunately that is not working for me.

As per my understanding, now i cannot access the user control instead i am having access to sub controls in user control, so the mouse events for user control are not working.

Correct me if am wrong, and provide any solution.

Lokesh
  • 301
  • 3
  • 19

1 Answers1

1

well, the mouse event like MouseDown and MouseUp occurs only when the mouse is doing something on the specific control. the best offer i can give you is to catch each mouse event in the controls and call a method on the userControl

  public UserControl1()
  {
     InitializeComponent();
     this.MouseDown += new MouseEventHandler(this.UserControl1_MouseDown);
     this.comboBox1.MouseDown += new MouseEventHandler(this.comboBox1_MouseDown);
  }

  private void UserControl1_MouseClick(object sender, MouseEventArgs e)
  {
      UCMouseDown();
  }

  private void UserControl1_MouseDown(object sender, MouseEventArgs e)
  {
      UCMouseDown();
  }

  private void comboBox1_MouseDown(object sender, MouseEventArgs e)
  {
      UCMouseDown();
  }

  private void UCMouseDown()
  {
      // Your code
  }
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70
  • 1
    I have to recalculate the coordinates and position the User control accordingly. But if i handle the events for sub controls i will be getting the coordinates relative to User Control and not relative to Form. In that case coordinates will not be calculated properly. – Lokesh Sep 10 '13 at 07:16
  • 1
    Hey that worked. I should have tried it myself. But for this solution i have to handle the events for both my sub controls. What if i have more than 10 controls? Will look forward to get a global approach for this. – Lokesh Sep 10 '13 at 07:30
  • @Lokesh there is a global approach, but you aren't going to like it: http://stackoverflow.com/questions/547172/pass-through-mouse-events-to-parent-control – No Idea For Name Sep 10 '13 at 07:36