Results 1 to 9 of 9

Thread: [Solved CODING BEGINNER] How can I show a images from listbox ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Thumbs up [Solved CODING BEGINNER] How can I show a images from listbox ?

    Hello , i have a emergency!
    i look for a way to execute a image from a listbox taken randomly.

    i tried this:

    textbox1.test = random_number ' i managed to generate a random number
    picture1.image = listbox1.item(textbox1.text) but it don't work
    also
    picture1.image = image.location(listbox1.item(textbox1.text)) ' notice important i add all path my pictures are in my.resources.[name of pictures] !
    NO result! crash of applications.

    somebody could be my savior! help me please! thank you so much for your answer in advance!
    Last edited by danzey; Jun 26th, 2021 at 03:58 PM.

  2. #2
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,138

    Re: [HELP CODING BEGINNER] How can I execute images from listbox ?

    Since "executing an image" is a nonsensical goal, step 1 is to explain exactly what you are trying to accomplish.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [HELP CODING BEGINNER] How can I execute images from listbox ?

    Quote Originally Posted by OptionBase1 View Post
    Since "executing an image" is a nonsensical goal, step 1 is to explain exactly what you are trying to accomplish.
    indeed,i would like show a image simply fom a list in a listbox.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [HELP CODING BEGINNER] How can I show a images from listbox ?

    My first advice is for you to turn on Option Strict, which can be done in Project|Properties on the Compile tab, or more permanently under the Options.

    Option Strict will disallow implicit conversions, which are indirectly at the root of the problem. Of course, if implicit conversions are disallowed, it will mean that you will need to explicitly convert things, which means thinking more about types. Explicit conversions are faster, but you won't see that, so it won't really matter. Still, it will help. For example, anything in a textbox is text. It may look like an integer, but it is a string. Using a string to index a listbox wouldn't work, except that Option Strict Off is allowing an implicit conversion of the string into an integer, but what are you getting back? You're obviously hoping that it will be a picture...eventually, but it probably isn't. So, what IS in that listbox? What are you getting back from it? I'm guessing that what you are getting back is a filename complete with path, but that's just a guess. That would be a string, though, and most likely what you are getting back is an Object that is being implicitly converted to something.

    Tell us what is in the listbox, and what error message you are getting. Then, stop using implicit conversions as a crutch, because it isn't helping you any.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Exclamation Re: [HELP CODING BEGINNER] How can I show a images from listbox ?

    [thank you for your help!

    first , i try a thing like this: PictureBox1.Image = My.Resources.ResourceManager.GetObject(ListBox1.Items(TextBox6.Text))


    Quote Originally Posted by Shaggy Hiker View Post
    My first advice is for you to turn on Option Strict, which can be done in Project|Properties on the Compile tab, or more permanently under the Options.

    Option Strict will disallow implicit conversions, which are indirectly at the root of the problem. Of course, if implicit conversions are disallowed, it will mean that you will need to explicitly convert things, which means thinking more about types. Explicit conversions are faster, but you won't see that, so it won't really matter. Still, it will help. For example, anything in a textbox is text. It may look like an integer, but it is a string. Using a string to index a listbox wouldn't work, except that Option Strict Off is allowing an implicit conversion of the string into an integer, but what are you getting back? You're obviously hoping that it will be a picture...eventually, but it probably isn't. So, what IS in that listbox? What are you getting back from it? I'm guessing that what you are getting back is a filename complete with path, but that's just a guess. That would be a string, though, and most likely what you are getting back is an Object that is being implicitly converted to something.

    Tell us what is in the listbox, and what error message you are getting. Then, stop using implicit conversions as a crutch, because it isn't helping you any.
    i added the names of my pictures. they are in my.resources included with my exe(application).

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [HELP CODING BEGINNER] How can I show a images from listbox ?

    Just the names of your pictures?

    So, what happened when you tried a thing like that? Personally, I've never done anything like what you are doing, so off the top of my head, I don't know how to do this. You are implicitly converting the string from the textbox into an integer. That will either work or crash, depending on what is in the textbox. You are then using that to get something back from a Listbox, which is probably being implicitly converted to something from an Object. That will probably work, because the Object is probably a string, and a string is the proper argument for GetObject. That implicit conversion will probably give you the string you are expecting, and isn't likely to fail. However, if it isn't working, then something is wrong with it. If you are getting an error message, you'd have to tell us what it was. If you aren't getting an error message, then you might put a breakpoint on that line and see what

    ListBox1.Items(TextBox6.Text)

    returns. Is it what you expect? If so, then what does

    My.Resources.ResourceManager.GetObject(ListBox1.Items(TextBox6.Text))

    return?
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [HELP CODING BEGINNER] How can I show a images from listbox ?

    Quote Originally Posted by Shaggy Hiker View Post
    Just the names of your pictures?

    So, what happened when you tried a thing like that? Personally, I've never done anything like what you are doing, so off the top of my head, I don't know how to do this. You are implicitly converting the string from the textbox into an integer. That will either work or crash, depending on what is in the textbox. You are then using that to get something back from a Listbox, which is probably being implicitly converted to something from an Object. That will probably work, because the Object is probably a string, and a string is the proper argument for GetObject. That implicit conversion will probably give you the string you are expecting, and isn't likely to fail. However, if it isn't working, then something is wrong with it. If you are getting an error message, you'd have to tell us what it was. If you aren't getting an error message, then you might put a breakpoint on that line and see what

    ListBox1.Items(TextBox6.Text)

    returns. Is it what you expect? If so, then what does

    My.Resources.ResourceManager.GetObject(ListBox1.Items(TextBox6.Text))

    return?
    if i have no result ,the picture don't show but i have no crash!
    i am going to try with My.Resources.ResourceManager.GetObject(ListBox1.Items(TextBox6.Text).ToString)
    wait ... wait... it seems to work... i will comfirm with My.Resources.ResourceManager.GetObject(ListBox1.Items(TextBox6.Text))
    Last edited by danzey; Jun 26th, 2021 at 03:19 PM.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [HELP CODING BEGINNER] How can I show a images from listbox ?

    Well, if it works it works, but there are a couple things you really need to change:

    1) Turn Option Strict ON and fix the errors that causes. Implicit conversions aren't just slow, they also can hide some really interesting bugs.
    2) Use better names for controls. Leaving those default names will end up biting you badly at some point.
    3) If you don't know about debugging, at least as far as breakpoints and examining the code at a breakpoint, it would be the most useful (and one of the simplest) tools you can learn.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Lively Member
    Join Date
    May 2017
    Posts
    96

    Re: [HELP CODING BEGINNER] How can I show a images from listbox ?

    Quote Originally Posted by Shaggy Hiker View Post
    Well, if it works it works, but there are a couple things you really need to change:

    1) Turn Option Strict ON and fix the errors that causes. Implicit conversions aren't just slow, they also can hide some really interesting bugs.
    2) Use better names for controls. Leaving those default names will end up biting you badly at some point.
    3) If you don't know about debugging, at least as far as breakpoints and examining the code at a breakpoint, it would be the most useful (and one of the simplest) tools you can learn.
    thank you for all! thank you for your advice i will do my best!
    i confirm it works! solved now!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width