PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2008 How to compare the picturebox1.image is equal to the image what I want from imagelist-VBForums
Results 1 to 5 of 5

Thread: How to compare the picturebox1.image is equal to the image what I want from imagelist

  1. #1

    Thread Starter
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    How to compare the picturebox1.image is equal to the image what I want from imagelist

    im doing a puzzlegame by using imagelist now,i stay this step since a long time . i will be grateful if someone can answer me,thanks


    What I have tried:

    if picturebox1.image = imagelist.images(1) ` not work...

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,588

    Re: How to compare the picturebox1.image is equal to the image what I want from image

    Image is a reference type so you can't use '='. You have to use 'Is' to determine whether two references refer to the same instance. I would have thought that the error message you got when you wrote that code would have told you that but maybe not.

    Of course, they actually do need to refer to the same instance for that to be useful. Two Image objects that were created from the same data are still two different objects so comparing them with 'Is' will result in False. If you want to determine whether two Image objects contain the same image data then that is something else.

    Also, I'd probably suggest not using an ImageList with a PictureBox. Some controls require an ImageList, e.g. ListView and TreeView, but I'd suggest not using them otherwise. That's because they may change the images they contain, e.g. reduce the quality. Maybe you want that but, if not, I'd suggest adding the images you want to your resources and then just load them into an array or List(Of Image) at run-time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Registered User
    Join Date
    Dec 2017
    Posts
    2

    Re: How to compare the picturebox1.image is equal to the image what I want from image

    thank you for reply,but i still want to use imagelist to do my puzzle game,first,I have created a shuffle function,i think it is easy to store the random number in each of pic by using imagelist.Also,this is the last step of the game what is detect the user whether he has completed the puzzle game.(it means compare the pic between picturebox and imagelist).just a little bit.

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

    Re: How to compare the picturebox1.image is equal to the image what I want from image

    Shuffle would be at least as easy, if not easier, with a List(of Image). I seem to remember that JMC created a one line shuffle using LINQ/Lambdas, or something like that. He'll likely share it. It is equally easy to store the necessary number in either one, because the necessary number can be as simple as an integer.

    Comparing the equality of the pictures could be quite painful. As JMC noted, if they were just both loaded from the same resource, they won't be the same image, so reference equality won't work. However, there IS something that WILL work. All controls have a .Tag property, which is type Object. The .Tag is there so you can attach whatever you want to the control. The .Tag property is essentially a storage property that can store ANYTHING. In this case, I think it would make the most sense for the PB.Tag to hold the index of the image from the ImageList or List(of Image). Then you wouldn't need to shuffle the images themselves and could use whatever you wanted. All you'd have to do is randomize the integers in the .Tag property of the PictureBox, and that would shuffle the images plenty. The integers would be the index into the collection of images, so whatever order those integers were found in would be the order of the images shown in the PictureBox.
    My usual boring signature: Nothing

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,588

    Re: How to compare the picturebox1.image is equal to the image what I want from image

    Quote Originally Posted by louis2417 View Post
    first,I have created a shuffle function,i think it is easy to store the random number in each of pic by using imagelist.
    That's a complete non-sequitur I'm afraid. Using an ImageList has exactly zero effect. Either way, you're going to be using those random numbers as an index. Whether it's an index into the Images collection of an ImageList, a Image array or a List(Of Image) makes no difference.
    Quote Originally Posted by louis2417 View Post
    Also,this is the last step of the game what is detect the user whether he has completed the puzzle game.(it means compare the pic between picturebox and imagelist).just a little bit.
    Again, I don't see what relevance that has to using an ImageList. Why is comparing to an Image in an ImageList easier than comparing to an Image in an array or a List(Of Image)? It's simply not.

    Also, you've made no reference to my solution to your issue, which applies whether you're using an ImageList or not. Did you actually read what I said and try to apply it?
    Quote Originally Posted by Shaggy Hiker View Post
    Shuffle would be at least as easy, if not easier, with a List(of Image). I seem to remember that JMC created a one line shuffle using LINQ/Lambdas, or something like that. He'll likely share it. It is equally easy to store the necessary number in either one, because the necessary number can be as simple as an integer.
    Let's say that the Images are stored as resources. You might load them like so:
    vb.net Code:
    1. Private images As Image()
    2. Private rng As New Random
    3.  
    4. Private Sub Form1_Load(...) Handles MyBase.Load
    5.     images = {My.Resources.Image1, My.Resources.Image2, My.Resources.Image3}
    6. End Sub
    You can randomise those Images any time like so:
    vb.net Code:
    1. Dim randomisedImages = images.OrderBy(Function(i) rng.NextDouble()).ToArray()
    That variable will then refer to an array of Images in random order. No need for any numbers because you can load those Images into PictureBoxes like this:
    vb.net Code:
    1. PictureBox1.Image = randomisedImages(0)
    2. PictureBox2.Image = randomisedImages(1)
    3. PictureBox3.Image = randomisedImages(2)
    or like this:
    vb.net Code:
    1. Dim pictureBoxes = {PictureBox1, PictureBox2, PictureBox3}
    2.  
    3. For i = 0 To pictureBoxes.GetUpperBound(0)
    4.     pictureBoxes(i) = randomisedImages(i)
    5. Next
    Even if you do stick with random numbers, you still access an element of the 'images' array in exactly the same way as you would an item in the Images collection of an ImageList. The only advantage an ImageList offers is that you can populate it with Images at design time rather than writing code, which is barely an advantage at all and is at least offset by any downside.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width