I am creating an Interactive map which displays the facilities when the user clicks on the Checkbox, such as toilets. It will display all the toilet locations via PictureBoxes. I want to do this without having to write all this code?

Private Sub chkCash_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCash.CheckedChanged

If chkToilet.Checked And RadioButton1.Checked Then
PictureBox1.Visible = True
PictureBox2.Visible = True
PictureBox3.Visible = True
PictureBox4.Visible = True
Else
PictureBox1.Visible = False
PictureBox2.Visible = False
PictureBox3.Visible = False
PictureBox4.Visible = False
End If

If chkToilet.Checked And RadioButton2.Checked Then
PictureBox5.Visible = True
PictureBox6.Visible = True
PictureBox7.Visible = True
PictureBox8.Visible = True
PictureBox9.Visible = True
Else
PictureBox5.Visible = False
PictureBox6.Visible = False
PictureBox7.Visible = False
PictureBox8.Visible = False
PictureBox9.Visible = False
End If
End Sub

Have you added the picture box to panel or groupbox?

No I have not

You need to add Picturebox to panel or groupbox, it will be easy to manage.

Once the controls are grouped, you can loop through all the content controls and enable or disable at a single go.

would adding the panel over the picturebox hide my map?

would adding the panel over the picturebox hide my map?

Try it and learn

No it doesn't work. The panel would just hid my map. Is their a way of doing it like this?

Dim firstArray = {Me.PictureBox1, Me.PictureBox2, ...}
Dim secondArray = {Me.PictureBox5, Me.PictureBox6, ...}
 
Array.ForEach(firstArray, Sub(pb) pb.Visible = True
Array.ForEach(secondArray, Sub(pb) pb.Visible = True
Member Avatar for Unhnd_Exception

If I were displaying icons on a map with picture boxes like you are doing I would probably do something like:

Create a list of pictureboxes to group the them. When you need to change the visibilty then loop through the list.

Private Toilets as New List(of Toilet)

Private Sub chkCash_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCash.CheckedChanged 
    
        For each Toilet as PictureBox in Toilets
         Toilet.Visible = (chkToilet.Checked AndAlso RadioButton1.Checked) 
     next

End Sub

You could add the pictureboxes to the list at load time or where ever.

Member Avatar for Unhnd_Exception

Heres an example of how I would display icons on a map.

Public Class Form1

    'I would create a list to store all icons
    Private MapIcons As List(Of MapIcon)

    'A class to store all icon information
    Private Class MapIcon
        Public Description As String
        Public Icon As Bitmap
        Public IconType As MapIconTypes
        Public ID As Integer
        Public PixelLocation As Point
    End Class

    'An Enumeration of all icon types
    Private Enum MapIconTypes
        None = 0
        Toilet = 1
        Sink = 2
    End Enum

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        MapIcons = New List(Of MapIcon)

        'Sample Map
        PictureBox1.Size = New Size(200, 200)
        PictureBox1.Image = New Bitmap(200, 200, Imaging.PixelFormat.Format24bppRgb)

    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'You would get this from a file, resource, or database
        'Creating a sample toilet image.
        Dim BitmapToilet As New Bitmap(32, 32, Imaging.PixelFormat.Format24bppRgb)
        Dim BitmapToiletGraphics As Graphics = Graphics.FromImage(BitmapToilet)
        BitmapToiletGraphics.Clear(Color.Brown)
        BitmapToiletGraphics.Dispose()

        'You would also get your icon information from somewhere else
        'Creating a sample toilet map icon.
        Dim MapIconToilet As New MapIcon
        MapIconToilet.Description = "Shitter's Clogged"
        MapIconToilet.Icon = BitmapToilet
        MapIconToilet.IconType = MapIconTypes.Toilet
        MapIconToilet.ID = 1
        MapIconToilet.PixelLocation = New Point(25, 75)

        MapIcons.Add(MapIconToilet)

    End Sub

    Private Sub PictureBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDoubleClick

        If CheckBoxToilet.Checked Then
            'Could use linq like the PictureBox_Paint sub
            'to get all toiles

            For Each MapIcon As MapIcon In MapIcons
                If Not MapIcon.IconType = MapIconTypes.Toilet Then Continue For

                'If the current mouse position is inside the map icon
                'then the user just double clicked on it.
                With MapIcon.PixelLocation
                    If .X >= e.X - 16 AndAlso .X < e.X + 16 AndAlso _
                       .Y >= e.Y - 16 AndAlso .Y < e.Y + 16 Then
                        MsgBox(MapIcon.Description)
                    End If
                End With
            Next
        End If

    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint

        'Paint all icons that are checked for display
        If CheckBoxToilet.Checked Then
            'Linq is a convienent way to get all items
            'of a list with a certain condition
            Dim ToiletCenter As Point
            Dim AllToilets = From ToiletIcon In MapIcons _
                                 Where ToiletIcon.IconType = MapIconTypes.Toilet
            'All toilets will be in AllToilets
            For Each ToiletIcon In AllToilets
                ToiletCenter.X = ToiletIcon.PixelLocation.X - 16
                ToiletCenter.Y = ToiletIcon.PixelLocation.Y - 16
                e.Graphics.DrawImage(ToiletIcon.Icon, ToiletCenter)
            Next
        End If

    End Sub

    Private Sub CheckBoxToilet_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxToilet.CheckedChanged
        'You could handle every check box that displays icons with
        'this sub.  Repaints the picturebox so the checked icons 
        'will be displayed
        PictureBox1.Refresh()
    End Sub

End Class

Thanks for your posts. I am new to vb and found your second post a bit complex to understand. On the first post do I write down all the pictureboxes into a notepad?

Member Avatar for Unhnd_Exception

In the load event you could add all the picture boxes you are displaying for toilets in the ToiletsList.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     Toilets.Add(PictureBox1)
     Toilets.Add(PictureBox2)
     Toilets.Add(PictureBox3)
     '....
End Sub

After the form is loaded you would be able to loop through them and change the visibilty.


Since your wanting to use pictureboxes. Another thing you could do is:

If you have all the pictureboxes added to the main form, "You drug and dropped them onto the form from the toolbox". You could set the tag of each picture box to what it represents. ie Toilet. then instead of using the Toilets List and adding picture boxes to it you could do something like:

Private Sub chkCash_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCash.CheckedChanged 

    For each Control in Me.Controls         
      If not typeof control is picturebox then continue for
      if Control.tag is nothing then continue for
      
      if Control.tag.tostring = "Toilet" then

         Control.Visible = (chkToilet.Checked AndAlso RadioButton1.Checked) 
       endif
    next

With that example you need no list. All you need to do is click on the picture box on the designer and fill in Toilet for the tag. And it will only work if the picture boxes are on the main form. Not in a panel or any other container. If they are then you would need to check Panel1.Controls,or whatever container, instead of Me.controls

Hope some of that helps.

I have 63 pictureboxes. I don't want to write all this code in my program.

Toilets.Add(PictureBox1)
Toilets.Add(PictureBox2)
Toilets.Add(PictureBox3)
....
Toilets.Add(PictureBox49)

I have 5 RadioButtons, each button has it's own floor such as Radiobutton1 is Floor 1, Radiobutton2 is Floor 2. Floor 1 has Pictureboxes 1...8 displaying toilets, floor 2 has Pictureboxes 9..13 displaying toilets and so on.

Can anyone help?

Dude dont call useless to Experts here... Show ur efforts.
We cant sit and write code for you... behave properly.

Member Avatar for Unhnd_Exception

Here you go <Edit>But shouldn't have</Edit>.

Add a text file to your project.

Go to project/Add new item And choose textfile. Name it MapIcons. When you see the text file in the solution explorer, click on MapIcons.txt and set the Copy to Output = copy if newer.

Add this to the text file
Toilet;1;45;78
Toilet;1;100;15
Toilet;2;200;45

Don't add this
File format
Item name; Floor number; x postion; y postion


Go to project / Properties/ Resources. Where it says add resource Choose add existing file. Open an image. In the paint event change my.resources.forum32 to what ever you added.


paste this code into a new form and add a picturebox1 CheckboxToilet RadiobuttonFloor1 and radiobuttonFloor 2.

Option Strict On
Option Compare Text

Public Class Form1

    'I would create a list to store all icons
    Private MapIcons As List(Of MapIcon)

    'A class to store all icon information
    Private Class MapIcon
        Public IconType As MapIconTypes
        Public Floor As Integer
        Public PixelLocation As Point
    End Class

    'An Enumeration of all icon types
    Private Enum MapIconTypes
        None = 0
        Toilet = 1
        Sink = 2
    End Enum

    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        MapIcons = New List(Of MapIcon)

        PictureBox1.Size = New Size(300, 300)

        LoadMapIcons()

    End Sub

    Private Sub LoadMapIcons()
        'load all icons from a text file 
        'TextFile in the format IconType,Floor,x,y
        'Everything in here should be validated.
        'This is for example.
        Dim StringReader As New IO.StringReader(My.Computer.FileSystem.ReadAllText("MapIcons.txt"))
        Dim Line() As String
        Dim MapIcon As MapIcon

        Do While StringReader.Peek <> -1
            Line = StringReader.ReadLine.Split(New Char() {";"c})
            MapIcon = New MapIcon
            Select Case Line(0)
                Case "Toilet"
                    MapIcon.IconType = MapIconTypes.Toilet
            End Select

            MapIcon.Floor = CInt(Line(1))
            MapIcon.PixelLocation = New Point(CInt(Line(2)), CInt(Line(3)))
            MapIcons.Add(MapIcon)
        Loop
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Dim Floor As Integer
        'Get the floor number
        If RadioButtonFloor1.Checked Then
            Floor = 1
        ElseIf RadioButtonFloor2.Checked Then
            Floor = 2
        End If

        'Paint all icons that are checked for display
        If CheckBoxToilet.Checked Then
            'Linq is a convienent way to get all items
            'of a list with a certain condition
            Dim AllToilets = From ToiletIcon In MapIcons _
                                 Where ToiletIcon.IconType = MapIconTypes.Toilet And ToiletIcon.Floor = Floor
            'All toilets will be in AllToilets
            For Each ToiletIcon In AllToilets
                'You need to add your own resource image.
                e.Graphics.DrawImage(My.Resources.Forum32, ToiletIcon.PixelLocation)
            Next
        End If

    End Sub

    Private Sub CheckBoxToilet_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBoxToilet.CheckedChanged, RadioButtonFloor1.CheckedChanged, RadioButtonFloor2.CheckedChanged
        'You could handle every check box that displays icons with
        'this sub.  Repaints the picturebox so the checked icons 
        'will be displayed
        PictureBox1.Refresh()
    End Sub

End Class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.