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

Access Button / Form from Inherited Control
klika84
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
rurounip
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
Whitehorseq
Pollocks
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
Sylvain Trépanier
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...