question

AyeshaMirza-5854 avatar image
0 Votes"
AyeshaMirza-5854 asked karenpayneoregon commented

How do I code a button to select an image?

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




dotnet-visual-basicwindows-forms
capture.jpg (138.7 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello,

Did you have a chance to try out my recommendation yet?

0 Votes 0 ·

1 Answer

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

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





figure2.png (12.9 KiB)
figure1.png (256.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.