You are viewing our Forum Archives. To view or take place in current topics click here.
I Need Visual Basic Help + Rep
Posted:

I Need Visual Basic Help + RepPosted:

Gavin_Garner
  • TTG Senior
Status: Offline
Joined: Jun 10, 201014Year Member
Posts: 1,000
Reputation Power: 46
Status: Offline
Joined: Jun 10, 201014Year Member
Posts: 1,000
Reputation Power: 46
Ok So I Have A Project That Has Over 1,000 Picture Boxes. I Need To Write Some Code So That When A Picture Box Is Clicked It Changes The Image. Like This
    Private Sub PictureBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        PictureBox1.Image = My.Resources.None
    End Sub


But Of Course I Don't Want To Do That Over 1,000 Times. So I Need Some Code That Monitors All Of The Picture Box Clicks.

If You Can Help Me With This I Will +Rep You
#2. Posted:
Imp
  • Blind Luck
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
Status: Offline
Joined: Jan 01, 201113Year Member
Posts: 1,957
Reputation Power: 401
You have to create a Control Array, then Add an event handler to deal with the Click of the picture box...

Here is a link to the msdn help for creating a control array, hope this helps. PM me if you need any assistance

[ Register or Signin to view external links. ]
#3. Posted:
Experiment5X
  • TTG Senior
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
I used a converter, to go from C# to VB, it should be fine though.

Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms

Namespace WindowsFormsApplication7
   Public Partial Class Form1
      Inherits Form
      Public Sub New()
         InitializeComponent()
         Dim cList As New List(Of Control)()
         For Each myControl As Control In Me.Controls
            If TypeOf myControl Is PictureBox Then
               Dim myPicBox As PictureBox = DirectCast(myControl, PictureBox)
               AddHandler myPicBox.Click, New EventHandler(AddressOf myPicBox_Click)
            End If
         Next
      End Sub

      Private Sub myPicBox_Click(sender As Object, e As EventArgs)
         Dim p As PictureBox = DirectCast(sender, PictureBox)
         p.Image = p.ErrorImage
      End Sub
   End Class
End Namespace


Last edited by Experiment5X ; edited 1 time in total
#4. Posted:
VallHalli
  • New Member
Status: Offline
Joined: Jun 06, 201113Year Member
Posts: 18
Reputation Power: 0
Status: Offline
Joined: Jun 06, 201113Year Member
Posts: 18
Reputation Power: 0
Experiment5X wrote

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            List<Control> cList = new List<Control>();
            foreach (Control myControl in this.Controls)
                if (myControl is PictureBox)
                {
                    PictureBox myPicBox = (PictureBox)myControl;
                    myPicBox.Click += new EventHandler(myPicBox_Click);
                }
        }

        void myPicBox_Click(object sender, EventArgs e)
        {
            PictureBox p = (PictureBox)sender;
            p.Image = p.ErrorImage;
        }
    }
}

that c# lol
i converted it for u
(sorry dont no how to add code)

Imports System.Collections.Generic
Imports System.Drawing
Imports System.Windows.Forms

Namespace WindowsFormsApplication7
Public Partial Class Form1
Inherits Form
Public Sub New()
InitializeComponent()
Dim cList As New List(Of Control)()
For Each myControl As Control In Me.Controls
If TypeOf myControl Is PictureBox Then
Dim myPicBox As PictureBox = DirectCast(myControl, PictureBox)
AddHandler myPicBox.Click, New EventHandler(AddressOf myPicBox_Click)
End If
Next
End Sub

Private Sub myPicBox_Click(sender As Object, e As EventArgs)
Dim p As PictureBox = DirectCast(sender, PictureBox)
p.Image = p.ErrorImage
End Sub
End Class
End Namespace
#5. Posted:
Experiment5X
  • TTG Senior
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
Status: Offline
Joined: Aug 14, 200915Year Member
Posts: 1,291
Reputation Power: 65
VallHalli wrote
that c# lol
i converted it for u
(sorry dont no how to add code)


You're quick lol, I fixed almost right after I posted.
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.