Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Access Button / Form from Inherited Control
 

Access Button / Form from Inherited Control

I'm trying to create a custom control which inherits from the Windows button control. Part of the functionality that I require is to make it possible to move the button around with the mouse. For this I need to determine the position of the button relative to the form, and also the form's position relative to the screen. If I didn't use a custom control, I could use code like this:

public void myButton_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
blnMoving = true;
MouseDownX = e.X;
MouseDownY = e.Y;
MovingRect = new Rectangle(button1.Location, button1.Size);
}
...

The question I have is when I'm creating the event handlers for the mouse events, how do I refer to "button1", because before I have created button1 I cannot refer to it (refer to the control itself). Also, how would I refer to the form in my custom event handlers, since the custom control can be put on any form for which I won't know the name beforehand. Must I use "this." before the methods?
MigrationUser 1  Sunday, May 11, 2003 6:13 PM
I figured it out. Basically I have to replace all instances in my generic code of "button1." with "this.", and all instances of that refer to the form (currently ".this") to "ButtonApp.Form1.ActiveForm.". I would be nice to use a shortcut to get to the form name, but for now this will do. Later I will do a routine to deduce which form the button is on, for this I presume I have to use something like a forms collection.
MigrationUser 1  Monday, May 12, 2003 1:02 AM
Since you are deriving your own control, I would override the corresponding OnXXXX Protected methods which raise the events you are handling.  Your snippet, for example, would reside in the class declaration for your button and look like this:

protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e) 

base.OnMouseDown(e);
if (e.Button == MouseButtons.Left) 

blnMoving = true; 
MouseDownX = e.X; 
MouseDownY = e.Y; 
MovingRect = new Rectangle(this.Location, this.Size); 



My C# is not strong, but I think I have the syntax right...
MigrationUser 1  Monday, May 12, 2003 11:02 AM
Hi all, 
  I asked the more of less the same question in a different forum, and Jacob pointed me over to this thread (thanks for the tip and THANKS for the code, btw). I also want a generic version of these methods, but I did it slightly different:

(1) Instead of "button1" I used the sender parameter, casted to a Control. This way is does not matter which Controls I assign this event to. For example, I can always do:

Panel p = new Panel();
if(WeNeedANewButton)
{
    Button b = new Button();
    b.MouseUp += new System.Windows.Forms.MouseEventHandler(this.DynControl_MouseUp);
    p.Controls.Add(b);
}

And that should work for any Control. (I think...)

(2) Instead of using 'this' (or 'Me' in VB?) I use the Parent property of the sender parameter since I only want to move my controls around the panel or tabpage where I placed them. In your case maybe you want the 'TopLevelControl' property since this should always spit out 'Form1' more or less.

For example, my version of Jacob's MouseUp looks like this so far:

private void DynControl_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
    Control c = (Control)sender;

    if(e.Button == MouseButtons.Left && !(blnClick))
    {
        c.Location = c.Parent.PointToClient(new Point(MovingRect.X, MovingRect.Y));
        c.Parent.Refresh();
        oldRect = Rectangle.Empty;
        blnMoving = false;
    }
    else
    {
        blnClick = false;
        blnMoving = false;
        c.Parent.Refresh();
        oldRect = Rectangle.Empty;
    }
}

This all *seems* to work so far - I am still trying to understand Jacob's VB code - if anyone wants me to, I will post the C# version when I am done - and then we can figure out how to do it better together.

Steve
MigrationUser 1  Wednesday, May 21, 2003 5:49 AM

Hi Steve,

I'm still working on the same problem and I would like all the help I can get. Please could you post your C# version when you're done, I'm sure I'll learn something from it.

Eugene
MigrationUser 1  Thursday, June 12, 2003 12:34 PM
Hi Eugene, 
 Sorry it took so long to reply - I was on vacation and studiously avoided my computer the entire time ;-) I can either post the code or mail it to you if you prefer. Let me know which option is better for you.

 I am trying to add a drag resizing operation to the code, which is making the event handlers bigger and bigger, but I hope to figure it out in the next couple of days. There were some other posts about this (look for Jacob's recent posts). 

 Look forward to hearing from you.

Steve
MigrationUser 1  Tuesday, June 17, 2003 7:23 AM

You can use google to search for other answers

Custom Search

More Threads

• How to get domain role names collection
• Painting Problem , That's is Wierd !!!!!!!!
• Set Property Browsable to make false at runtime
• Localizing DisplayMember
• Displaying combobox in sub header or category part of propertygrid...?
• Controls Gone!!
• Overriding the default Windows Forms IRootDesigner
• Autoscroll problem
• UserControl as Container
• Component become invisible in designtime