Question how do I code a button to select an image?

ayesha120597

New member
Joined
Apr 12, 2021
Messages
3
Programming Experience
1-3
I want the user to select an image by clicking the button and then clicking select that will close the form. I have the images in a separate folder that I have added as a picturebox and they are added above their respected button. 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

Form code:
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 Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click

    End Sub

    Private Sub Label1_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
 
Last edited:
This is what my form looks like
 

Attachments

  • Capture.JPG
    Capture.JPG
    138.7 KB · Views: 20
There are numerous specific ways this could be done but you already have a Click event handler for each Button so, in each of those, simply get the Image of the appropriate PictureBox and assign it to a public property of type Image. You can then set the DialogResult of the form to OK to close the dialogue. In the calling form, you can then get the selected Image from that same public property.
 
There are numerous specific ways this could be done but you already have a Click event handler for each Button so, in each of those, simply get the Image of the appropriate PictureBox and assign it to a public property of type Image. You can then set the DialogResult of the form to OK to close the dialogue. In the calling form, you can then get the selected Image from that same public property.
so to an assign an image to a button I wrote this:
private void ButtonGeode_Click(object sender, EventArgs e)
{
ButtonGeode.Image = Image.FromFile("C:\\Users\\Ayesha Mirza\\Desktop\\OOP III\\Customize Menu Back\\CardBack\\blue geode.png")
}

does this make sense?
 
No it doesn't make sense. Apart from anything else, that's C# code and you have posted in a VB.NET forum. Maybe decide what language you're using and stick to it, and post in the appropriate place. If you're actually using C# then there's a link to our sister site at the top of this page.

You already have the Image object in a PictureBox so why are you loading it from a file again? Why are you displaying it on a Button when you already have to displayed in a PictureBox? How does displaying it on a Button help you get it out of the dialogue in the calling form? Read what I wrote and do that:
VB.NET:
Public Property SelectedImage As Image

Private Sub ButtonGeode_Click(sender As Object, e As EventArgs)
{
    SelectedImage = PictureBoxGeode.Image
}
If what you actually want is the path of the image file, rather than the Image object, then that is something different and you need to actually state that.
 
Back
Top