Sending click events from a custom control back to the form the control is on

mickn66

Regular
Joined
Nov 2, 2002
Messages
53
I have this user control I made called a Blob. The blob is a small control that has a panel on it, covering the entire control, and a label on the top half of the panel. And I have a Form with a number of Blobs on them. I’ve managed to get the Blob, each of them, to respond to a click on the panel (the part that isn’t covered by the label) by popping up a message box that identifies the Blob – by putting a message box in the Panel’s Mouse_Down handler (the message box displays a property of the Blob called ‘longname’ – each Blob has a different one). However, I don’t know how to pass the longname (or some other id) back to the form that the Blob is on. I don't even know how to get the form to realize that a Blob has been clicked on, in addition to which blob it was. Putting something in the MouseDown handler of the form itself only reacted when I clicked on a part of the form that was not covered by a Blob. Thanks in advance.
 
This may not be the best solution but you could pass the form class by ref in the constructor of blob and assign it to a global variable. This way whatever you do to the global variable will happen to the form.

PB
 
attach a method of your form to all of your blobs click/mouse down event.

when the method is fired the blob will be the sender argument
 
Thanks for your help. How do I go about attaching a method of the form to all the blobs? I mean considering that the Blobs are created at run time. The code I have that adds new Blobs when necessary looks like this:

Dim newblob As Blob = New Blob
...several properties of newblob are set..

Me.Controls.Add(newblob)

Do I need to add something to attatch the method on the form to the newbob? Thanks again in advance :)


Joe Mamma said:
attach a method of your form to all of your blobs click/mouse down event.

when the method is fired the blob will be the sender argument
 
How about something like

Code:
public class Form1 : System.Windows.Forms.Form
{
  private System.Windows.Forms.MainMenu mainMenu1;
  private System.Windows.Forms.MenuItem menuItem1;

  etc etc...

  public void DoSomething (string something) 
  { 
    ..... 
  }

  public void CreateBlobs ()
  {
    BLOB b = new BLOB(ref this);
  }
}

public class BLOB
{
  private Form1 useform;
  public BLOB (ref Form1 form)
  {
     useform = form;
  }

  public void DosomethingtoForm ()
  {
     useform.DoSomthing("Hello from BLOB");
  }
}
 
Back
Top