Creating user object, derived from button

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)
LVL 53
DhaestAsked:
Who is Participating?
 
dkloeckCommented:
yes, because otherwise you will have no access to the location of the other buttons.

However, there is an ugly way to do it from the object..
if you only want to check for the object of the same class, and you will create your own, you can create a static List to keep the information of all created objects in that class

something like:
class customButton : Button
{
    private static List<customButton> createdCustomButtons = new List<customButton>() ;

   public customButton()
   {
       createdCustomButtons.Add(this);
   }
}

createdCustomButtons will then contain all created customButtons, and you can use that list to check if they overlap (but i suggest you do it from the parent form)
0
 
dkloeckCommented:
>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
{

}
0
 
dkloeckCommented:
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
    }
}
0
 
dkloeckCommented:
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);
}
0
 
DhaestAuthor 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 ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.