Lokesh
Lokesh

Reputation: 301

Handling UserControl events dynamically

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.

Upvotes: 2

Views: 133

Answers (1)

No Idea For Name
No Idea For Name

Reputation: 11597

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
  }

Upvotes: 1

Related Questions