We help IT Professionals succeed at work.

Creating user object, derived from button

179 Views
Last Modified: 2010-04-15
I have the following. I experimented with dynamic buttons on my screen. I added them and i had 3 events (mouseDown, mouseUp & mouseMove) for dynamically moving the buttons on the screen.
In the MouseUp, I make sure that the buttons can't overlap each other.

How can I create an userobject from this button (object will be named table), derived from the button-object ?
How can I check with the MouseUp event that this object doesn't overlap other objects on the form where this object is placed on ?

(I haven't created an userobject wiht events before)
Comment
Watch Question

Commented:
>How can I create an userobject from this button (object will be named table), derived from the >button-object ?

If you mean an instance.. you just write Button userInstance = new Button();

If you mean a derived class.. you do something like:

class customButton : Button
{

}

Commented:
As for the overlapping.. in the parent form..

first subscribe to the events of the dynamic buttons

foreach(Button btn in ButtonCollection)
{
btn.MouseUp += new EventHandler(OnMouseUp);//use right delegate..im not sure if this is the one
}

public void OnMouseUp(object sender, MouseEventArgs e)
{
      bool hasOverlapingButton = false;
     //code to check if there exists a button which overlap
     foreach(Button btn in ButtonCollection)//you also could get all your Buttons out of the Controls variable
    {
         if(btn.Bounds.IntersectsWith(((Button)sender).Bounds)
                    hasOverlapingButton =true;
    }
    if(hasOverlapingButton )
    {
          //do something
    }
}

Commented:
To get the buttons out of the parents controls do:

List<Button> ButtonCollection=new List<Button>();
foreach(Control ctrl in Controls)
{
   if(ctrl is Button)
         ButtonCollection.Add(ctrl as Button);
}
Dirk HaestProject manager
CERTIFIED EXPERT

Author

Commented:
>> As for the overlapping.. in the parent form..

Do you mean that the events still needs to be captured in the main form and not in the object ?
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.