Answered by:
On double click event ,clear multiple picture box image at the same time

Question
-
Hello,
I have created one windows form with 100 picture boxes on it. Now i want to clear 100 picture boxes images at the same time on double click event. Please tell me the solution.
Do needful.
Thanks & Regards
Rushali
Monday, September 14, 2015 5:51 AM
Answers
-
It's not a good idea to use a Click and Double Click event for the same control since Double Clicks can be confused as a Click if the timing is not accurate for the Double Click which is set in Control Panel for a Mouse somewhere.
Either recursively search for all PictureBox's on the Form and clear them or when loading them add them all to a List(Of PictureBox) and then use a loop to set all of the PictureBox's images or backgroundimages to Nothing in the List(Of PictureBox).
For Each Item As PictureBox In Me.Controls.OfType(Of PictureBox)() Item.Image = Nothing Next
La vida loca
- Edited by Mr. Monkeyboy Monday, September 14, 2015 6:17 AM
- Proposed as answer by IronRazerz Monday, September 14, 2015 10:24 PM
- Marked as answer by Youjun Tang Thursday, September 24, 2015 3:29 AM
Monday, September 14, 2015 6:10 AM
All replies
-
I'm not sure how you was able to fit the 100 picture box in one form!.
How did you add them to the form? If you added them dynamically via the code, you can place them in a Panel control and then when you want to clear call the Clear method on the panel to remove all the control it contains.
Fouad Roumieh
- Edited by Fouad Roumieh Monday, September 14, 2015 5:59 AM
Monday, September 14, 2015 5:58 AM -
It's not a good idea to use a Click and Double Click event for the same control since Double Clicks can be confused as a Click if the timing is not accurate for the Double Click which is set in Control Panel for a Mouse somewhere.
Either recursively search for all PictureBox's on the Form and clear them or when loading them add them all to a List(Of PictureBox) and then use a loop to set all of the PictureBox's images or backgroundimages to Nothing in the List(Of PictureBox).
For Each Item As PictureBox In Me.Controls.OfType(Of PictureBox)() Item.Image = Nothing Next
La vida loca
- Edited by Mr. Monkeyboy Monday, September 14, 2015 6:17 AM
- Proposed as answer by IronRazerz Monday, September 14, 2015 10:24 PM
- Marked as answer by Youjun Tang Thursday, September 24, 2015 3:29 AM
Monday, September 14, 2015 6:10 AM -
Hi Fouad,
i want to clear multiple picture box images on their each picture box double click event separately. I have added picture boxes from toolbox directly. we can clear image of picture box with the help of following line of code-
Picturebox.image = Nothing
but i want to clear 100 picture box images for that i have to write the code for each picture box double click event. so i want to reduce the code. Please tell me the solution.
Thanks & Regards
Rushali
Monday, September 14, 2015 6:20 AM -
Hi Fouad,
i want to clear multiple picture box images on their each picture box double click event separately. I have added picture boxes from toolbox directly. we can clear image of picture box with the help of following line of code-
Picturebox.image = Nothing
but i want to clear 100 picture box images for that i have to write the code for each picture box double click event. so i want to reduce the code. Please tell me the solution.
Thanks & Regards
Rushali
Create one event and then in the DoubleClick event in the events window select that event for the 100 control DoubleClick event, thats if we said the same implementation of the event handler will be applied to all. This can be done via code also.
Then your event will be something like the below:
Private Sub PBoxDoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.DoubleClick, PictureBox2.DoubleClick Dim pBox As PictureBox = CType(sender, PictureBox) pBox.Image = Nothing End Sub
However, I suggest that you describe the functional scenario that trying to achieve so maybe you can get a better idea of how to do this work, writing small amount of code is not the sign that you doing it right.
Fouad Roumieh
Monday, September 14, 2015 7:32 AM -
Hello,
It is working. Thanks a lot.
Thanks & Regards
Rushali
Monday, September 14, 2015 8:42 AM -
Hello,
It is working. Thanks a lot.
Thanks & Regards
Rushali
Well then you should propose the post that answered the question as the answer so this thread will be ended and so people looking at this thread will know what answered the question.La vida loca
Monday, September 14, 2015 3:34 PM -
Hello,
Here you need the code in form load, all picture boxes are assumed to be on the form canvas
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Me.Controls.OfType(Of PictureBox).Select( Function(pb) pb).ToList.ForEach( Sub(pb) AddHandler pb.DoubleClick, Sub(sender1 As Object, e1 As EventArgs) Me.SuspendLayout() Try Me.Controls.OfType(Of PictureBox).Select( Function(pb1) pb1).ToList.ForEach( Sub(item) item.Image = Nothing) Finally Me.ResumeLayout() End Try End Sub End Sub) End Sub End Class
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.
Microsoft Developer tools
Developer’s Guide to Windows 10 video series- Proposed as answer by Fouad Roumieh Monday, September 14, 2015 5:47 PM
Monday, September 14, 2015 4:27 PM -
I suggest Kevinistructor solution, I mentioned previously that the event attachment can be done via code and this is the way to do it. This is more clean, since you looking for little code :).
I'm confused if you want to clear 1 picture box on double click or all at the same time, in case you want to clear only one you can merge both codes as below (with the apology to Kevininstructor for replicating his code in my post):
Me.Controls.OfType(Of PictureBox).Select( Function(pb) pb).ToList.ForEach( Sub(pb) AddHandler pb.DoubleClick, Sub(sender1 As Object, e1 As EventArgs) Me.SuspendLayout() Try CType(sender1, PictureBox).Image = Nothing Finally Me.ResumeLayout() End Try End Sub End Sub)
Fouad Roumieh
Monday, September 14, 2015 5:55 PM -
In the future no apologies are not needed for any code I present as it is fair game :-)
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.
Microsoft Developer tools
Developer’s Guide to Windows 10 video seriesMonday, September 14, 2015 7:00 PM -
This works for attaching event handler also.
For Each Item As PictureBox In Me.Controls.OfType(Of PictureBox)() AddHandler, Item.DoubleClick, AddressOf PBoxDoubleClick Next
La vida loca
- Edited by Mr. Monkeyboy Monday, September 14, 2015 9:35 PM
- Proposed as answer by IronRazerz Monday, September 14, 2015 10:23 PM
Monday, September 14, 2015 9:34 PM