[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Creating user object, derived from button

Posted on 2007-09-28
5
Medium Priority
?
156 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)
0
Comment
Question by:Dhaest
  • 4
5 Comments
 
LVL 10

Expert Comment

by:dkloeck
ID: 19977531
>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
 
LVL 10

Expert Comment

by:dkloeck
ID: 19977552
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
 
LVL 10

Expert Comment

by:dkloeck
ID: 19977591
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
 
LVL 53

Author Comment

by:Dhaest
ID: 19977668
>> 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
 
LVL 10

Accepted Solution

by:
dkloeck earned 1500 total points
ID: 19977815
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

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses
Course of the Month18 days, 6 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question