How do I code a button to select an image?

Ayesha Mirza 21 Reputation points
2021-04-13T00:20:07.167+00:00

I want the user to select an image by clicking the button and clicking select. I have the images in a separate folder. So what I am trying to do is create an if-else statement, where: if the user clicks a certain button (for example button1) it would assign a variable called cardbackimage to retain the image associated with button1.

I wanted to know: -how to assign an image to a button ( so when you click it, it means that the user wants that image) -how to assign the image that was selected to a variable

  • and how to end the form once the image is selected, I have made the select button (as shown in the image) the accept button.

the image is an image of my form that I have created

87123-capture.jpg

Public Class Form1
    Private Sub VScrollBar1_Scroll(sender As Object, e As ScrollEventArgs)

    End Sub

    

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub CancelBtn_Click(sender As Object, e As EventArgs) Handles CancelBtn.Click

    End Sub

    Private Sub ButtonSelect_Click(sender As Object, e As EventArgs) Handles ButtonSelect.Click

    End Sub

    Private Sub Label1Title_Click(sender As Object, e As EventArgs) Handles Label1.Click

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click

    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click

    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

    End Sub

    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click

    End Sub

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click

    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click

    End Sub
End Class
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,922 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,779 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,551 Reputation points
    2021-04-13T01:52:58.207+00:00

    Hello,

    Simple example where you can access the image via the PictureBox image property of SelectedPictureBox property

    Use the following custom button

    Imports System.ComponentModel  
      
    Public Class ButtonPictureBox  
        Inherits Button  
        <Description("The image associated with the control"), Category("Behavior")>  
        Public Property PictureBox() As PictureBox  
    End Class  
      
    

    Assign a Picturebox to PictureBox property in code or designer mode.

    In design mode

    87204-figure2.png

    Form code

    Public Class ExampleForm  
        Private Property SelectedPictureBox() As PictureBox  
        Private Sub ExampleForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
      
            Controls.OfType(Of ButtonPictureBox).ToList().ForEach(  
                Sub(box)  
                    AddHandler box.Click, AddressOf AllButton_Click  
                End Sub)  
        End Sub  
      
        Private Sub AllButton_Click(sender As Object, e As EventArgs)  
            SelectedPictureBox = CType(sender, ButtonPictureBox).PictureBox  
        End Sub  
      
        Private Sub GetSelectedButton_Click(sender As Object, e As EventArgs) Handles GetSelectedButton.Click  
            If SelectedPictureBox IsNot Nothing Then  
                MessageBox.Show(SelectedPictureBox.Name)  
            Else  
                MessageBox.Show("Please select an image")  
            End If  
        End Sub  
    End Class  
    

    87185-figure1.png

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.