need help creating events
in specific, i would like to have a mouseenter and mouseexit event for a picturebox to change the background color.
I have been programming exclusively in xna up until now, and this is my first foray into creating events in c#. So far i have successfully created
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
for a forms closing event. However i can't seem to find the mouse ones. Googling isn't helping much due to the implementation the results are taking.
Thanks.
rep++...
This project is going to be a (alpha) 10 ft interface program launcher for my media center. I tried a free one and the hot key polling was happening so often that the program was actually bogging the system down to the point it was losing audio sync on videos.
Future plans include a drag-drop to add more programs, but currently it's simply going to serve my immediate needs of keyboard or mouse input therefore i need a way of highlighting icons upon mouseover.
Re: need help creating events
Are you just having trouble wiring events?
Code:
public partial class Form1 : Form
{
public Form1()
{
this.InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.WireEvents();
}
private void WireEvents()
{
//wiring up events
this.pictureBox1.MouseEnter += new EventHandler(this.pictureBox1_MouseEnter);
this.pictureBox1.MouseLeave += new EventHandler(this.pictureBox1_MouseLeave);
}
private void UnWireEvents()
{
//unwiring events
this.pictureBox1.MouseEnter -= new EventHandler(this.pictureBox1_MouseEnter);
this.pictureBox1.MouseLeave -= new EventHandler(this.pictureBox1_MouseLeave);
}
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
this.pictureBox1.BackColor = Color.AliceBlue;
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
this.pictureBox1.BackColor = SystemColors.Control;
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//assuming you had wired up this event
//you could do the unwiring here
this.UnWireEvents();
}
}
Re: need help creating events
If the form is being closed, you don't need to unwire internal event handlers.
Re: need help creating events
thanks. I'll try that later tonight. Is it necessary to do the wire/unwire, or can i just add these lines to form load:
this.pictureBox1.MouseEnter += new EventHandler(this.pictureBox1_MouseEnter);
this.pictureBox1.MouseLeave += new EventHandler(this.pictureBox1_MouseLeave);
Also: is it possible to have that event handler be the same for each box but somehow send a unique argument value so a single function can handle them all, no matter how many i have? I will eventually be creating them at run time.
Re: need help creating events
The event handlers have a sender parameter that contains the object (in this case, PictureBox control instance) that raised the event.
I'm presuming you'd want to parameterise the colour that the background gets set to? I would probably do that by setting up a Dictionary<object, Color> and populating it with the picturebox and the desired colour. By using object rather than PictureBox as the key, you don't need to do any casting of the sender argument, just see if the dictionary contains an entry for that object.
You also have the event args parameter in the handler. For the mouse related events, you can more strongly type this as MouseEventArgs rather than just EventArgs, which gives you some more state information relating to the Mouse, although I'm not sure that will necessarily help you much in this instance.
Re: need help creating events
Quote:
Originally Posted by
Lord Orwell
thanks. I'll try that later tonight. Is it necessary to do the wire/unwire, or can i just add these lines to form load:
this.pictureBox1.MouseEnter += new EventHandler(this.pictureBox1_MouseEnter);
this.pictureBox1.MouseLeave += new EventHandler(this.pictureBox1_MouseLeave);
You can do just that. You can also wire the events in the Designer, this then goes in the designer's code file so you don't have to look at the plumbing code each time you edit the form. (This is both a positive and a negative - positive because most of the time you don't care about that stuff, and so you're removing noise. Negative because sometimes (very rarely) you do care about that stuff and digging through the designer or the designer's code is a PITA. YMMV)
[Edit: Oh, you'll be creating them at runtime. Ignore this then. You obviously can't use the designer to do that.]
Re: need help creating events
i tried to do it in the designer and couldn't figure out how. thus the post.
Re: need help creating events
To do it in the designer, click the PictureBox, go to the properties window. Click the lightning bolt, this will show all the events of the selected object. Navigate to the MouseEnter event. Either double-click the event or select it and press Enter. This will automatically wire the events for you.
Re: need help creating events
it must be creating them under the hood, but that's fine with me. I was trying to create them with the combo drop-down. I figured out the lightning bolt after i posted. Currently i have every picturebox triggering the same event. All i need to do is adjust it to create at runtime, and that's future plans.