Picturebox rightclick event clears image
-
mardi 31 juillet 2012 02:59
Hello everyone,
I am completely new to C#, and i have a question about it.
I am working on a mapeditor with an friend.
But we are abit confused..Our question is, how can we clear the image of a pictureBox, with one piece of code.
We now have this;
private void pictureBox144_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Right) { pictureBox144.Image = null; pictureBox144.Invalidate(); } }
But on every single pictureBox (192), we need to put it in, but we want, if we rightclick a pictureBox, no matter which one, it will clear that image.
Toutes les réponses
-
mardi 31 juillet 2012 04:13
Hi,
connect each pictureBox with the same event-handler. Get the current by casting the "sender" parameter.
public partial class Form1 : Form { //declare two picboxes PictureBox p1 = new PictureBox(); PictureBox p2 = new PictureBox(); public Form1() { InitializeComponent(); //set some Properties and add the handlers for mousedown p1.Location = new Point(0, 0); p2.Location = new Point(100, 0); p1.BorderStyle = p2.BorderStyle = BorderStyle.FixedSingle; //connect the same handler for all pictureBoxes p1.MouseDown +=new MouseEventHandler(p1_MouseDown); p2.MouseDown += new MouseEventHandler(p1_MouseDown); //add then to the controls collection this.Controls.Add(p1); this.Controls.Add(p2); } private void p1_MouseDown(object sender, MouseEventArgs e) { //get the current picturebox PictureBox pb = (PictureBox)sender; //change a Property to test... if (pb.BorderStyle == BorderStyle.FixedSingle) pb.BorderStyle = BorderStyle.Fixed3D; else pb.BorderStyle = BorderStyle.FixedSingle; } }Regards,
Thorsten
-
mardi 31 juillet 2012 22:54
Dear Thorsten,
In the previous post you have posted a code, but can you give a bit more detailed explaination, and how can i implement it?
Sorry, but i am completely new, only knowing the basics of VB.
Maybe you have some usefull tutorials for me?Thanks,
Jordi Prevost
-
mercredi 1 août 2012 11:04
Hi,
what I'm doing is to add some picturebox-controls to the Form. You usually do this with the designer of Visual Studio. I then hook the MouseDown-event of each pictureBox to the same eventhandler. You also maybe would usually do this with the designer. [In the properties window, the events tab, select the mouseDown-event and click the small down arrow and select the method to connect to]
You see, that I connect picutureBox2 to the event-handler "picturebox1_MouseDown" (which was generated automatically by selecting the event and doubleclicking "MouseDown" when picturebox1 was selected in the properties window)
Now when all pictureboxes-mousedown events point to that method ("pictureBox1_MouseDown"), you then can - in that method - get the control that raised the event by examining the "sender" parameter. An event handler usually gets to parameters in its method declaration, an object called "sender" and an "e"- parameter that provides event-specific data (like the MouseCoordinates in Mouse-events). The sender is usually the object that raised the event, so we can now get the instance of the picturebox that raised the event by casting the object to a picturebox and simply work with that instance then.
You can test the type by checking, if its a picturebox (if you connect different control types to that handler)
PictureBox pb = null;
if (sender is pictureBox)
pb = (PictureBox)sender;
//do somethingbut we only did connect pictuerbox-events to that method so I skipped the test and directly casted the object sender to type pictuerbox and assigned it to the variable pb. Now we cna work with that picturebox and for instance can get the Name-Property, or - in your case - get the Image-Property and clear it or whatever...
private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { //get the picturebox that raised the event PictureBox pb = (PictureBox)sender; //grab the currently assigned image for later cleanup Image img = pb.Image; //set the Image (here - to null) pb.Image = null; //cleanup the old image to prevent Memory leaks if (img != null) img.Dispose(); }Regards,
Thorsten
- Modifié Thorsten GuderaMicrosoft Community Contributor mercredi 1 août 2012 12:46
- Proposé comme réponse Jason Dot WangMicrosoft Contingent Staff, Moderator jeudi 2 août 2012 06:48
- Marqué comme réponse Jason Dot WangMicrosoft Contingent Staff, Moderator mardi 7 août 2012 09:35

