954,525 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Call upon general event trigger objects

This is my very first thread on the net period, I'll do my best to describe my c# problem. I was wondering if there is any way of calling upon general event trigger objects (referring to buttons, picture box etc.) on a whole, for example is there a keyword that i can use to do this.
I want multiple picture boxes to be handled by one event and I want to be able to make each picture box invisible when I move the mouse over it, without having to name the picture boxes individually. Is this possible?

pickleleon
Newbie Poster
6 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

>I want multiple picture boxes to be handled by one event ..

You want to write single event handler for multiple picture boxes.

private void Form1_Load(object sender, EventArgs e)
        {
            picture1.MouseEnter += new EventHandler(MyHandler);
            picture2.MouseEnter+=new EventHandler(MyHandler);
           
        }

        void MyHandler(object sender, EventArgs e)
        {
            PictureBox currentPic = sender as PictureBox;
            ....
        }
__avd
Posting Genius (adatapost)
Moderator
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
 

adatapost's answer is right.

You can attach the events of all the picture boxes in one go.

After coding your event handler for one of the picture boxes select all the picture boxes.
On the properties window select the events button at the top and scroll to find the MouseEnter event.
Click on the drop-down arrow and select the newly code MouseEnter event handler.
Now all the picture boxes are attached to the event handler in the forms InitializeComponent() function.

nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

Thanks for my first replies :), i've established how to link multiple events before and have used it in another section of my code. But what I want to do is set up the event handler to make any object that I assign to it (eg. a picture box) go invisible. At the moment i have an event for each picture box which looks like this...

private void picturebox1_MouseMove(object sender, MouseEventArgs e)
{picturebox1.visibility = false;
}
pickleleon
Newbie Poster
6 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

For events the sender object is the object triggering the event.
Try this.

private void picturebox1_MouseMove(object sender, MouseEventArgs e)
{
   PictureBox pb = sender as PictureBox;
   if(pb !=null)
   {
      pb.visibility = false;
   }
}
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
 

It's working, :)thanks a lot. Minimized my code dramatically.

pickleleon
Newbie Poster
6 posts since May 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: