Results 1 to 14 of 14

Thread: [RESOLVED] Image tag not found.

  1. #1

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Resolved [RESOLVED] Image tag not found.

    Hi,

    I can't find a reason why in function 'Finder' the pic'box isn't recognised, but function 'Primer' it's found just fine.
    vb.NET Code:
    1. Public Function Primer(sender As Object, e As System.EventArgs) As Boolean
    2.         Dim one, two As Integer, box As PictureBox
    3.  
    4.         one = CInt(DirectCast(sender, PictureBox).Tag)
    5.         box = CType(Me.Controls("PictureBox" & one.ToString), PictureBox)
    6.         two = CInt(box.Image.Tag)

    vb.NET Code:
    1. Public Function Finder() As Integer
    2.   Dim box As PictureBox, two As Integer
    3.  
    4.         For one = 1 To 10
    5.             box = CType(Me.Controls("PictureBox" & one.ToString), PictureBox)
    6.             two = CInt(box.Image.Tag)
    7.         Next
    In function 'Finder', when execution reaches line 6, I get the error message: 'Object reference not set to an instance of an object.', 'System.Windows.Forms.PictureBox.Image.get returned Nothing'. and in the 'Local' window, box.name = PictureBox1. (ok)
    In both functions the variable 'one' is just a valid integer. And in both cases the pic'box contains an image, all images have a numeric Tag set. (0 to 52). All ten pic'boxes have a numeric Tag set. (1 to 10).


    Poppa
    Last edited by Poppa Mintin; Sep 20th, 2021 at 05:54 AM. Reason: Thread title changed
    Along with the sunshine there has to be a little rain sometime.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Image tag not found.

    Quote Originally Posted by Poppa Mintin View Post
    in both cases the pic'box contains an image
    Except that's obviously not true, or you wouldn't be getting that error message. Set a breakpoint and step through the code and you will see that, at least once, the Image property of the PictureBox you get is Nothing.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Image tag not found.

    If I were you, then I would setup some conditional statements to check for easy errors:
    Code:
    Public Function Finder() As Integer
        Dim foundControl As Control
        Dim box As PictureBox
        Dim two As Integer
         
        For one = 1 To 10
            foundControl = Controls.Find($"PictureBox{one}")
            If (foundControl IsNot Nothing) Then
                box = TryCast(foundControl, PictureBox)
                If (box IsNot Nothing) Then
                    If (box.Tag IsNot Nothing AndAlso Integer.TryParse(box.Tag.ToString(), two))
                        ' do something with two
                    Else
                        MessageBox.Show($"The control PictureBox{one} does not have a valid tag. The tag value is: {If(box.Tag Is Nothing, "null", box.Tag)}", "Invalid Control", MessageBoxButtons.Ok)
                    End If
                Else
                    MessageBox.Show($"The control PictureBox{one} cannot be converted to a PictureBox.", "Invalid Control", MessageBoxButtons.Ok)
                End If
            Else
                MessageBox.Show($"Unable to find control PictureBox{one} on the Form.", "Invalid Control", MessageBoxButtons.Ok)
            End If
            two = CInt(box.Image.Tag)
        Next
        ' ...
    End Function
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Image tag not found.

    Quote Originally Posted by jmcilhinney View Post
    Except that's obviously not true, or you wouldn't be getting that error message. Set a breakpoint and step through the code and you will see that, at least once, the Image property of the PictureBox you get is Nothing.
    That's what I thought.

    However, I put a breakpoint at 'Finder' line 5 and run the project. When it breaks at line 5 I single step to line 6 and the Local window tells me that 'box' contains PictureBox1. Fine, that's correct.

    I single step again and get the error message.
    Pausing the cursor over the icon in the taskbar(?) I and see a thumbnail of the form and I can see that picturebox1 does contain an image, and it's a valid image.

    So I figure that I must be using the wrong code somewhere.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Image tag not found.

    two = CInt(box.Image.Tag)

    Box has an Image property and a Tag property
    Image does not have a Tag property

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Image tag not found.

    Quote Originally Posted by .paul. View Post
    two = CInt(box.Image.Tag)

    Box has an Image property and a Tag property
    Image does not have a Tag property
    Actually, the Image class does have a Tag property. If it didn't then that code wouldn't compile with Option Strict On or throw a MissingMemberException with Option Strict Off. What may well be the case is that it is actually the Tag of the PictureBox that is wanted in that scenario, rather than the Tag of the Image. That said, the error message still says that the Image property getter returned Nothing so it still seems unlikely that it actually doesn't. Regardless, if the code is changed to get the Tag of the PictureBox then there will be no call to the getter of the Image property, so it will be a moot point, at that stage at least.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Image tag not found.

    Quote Originally Posted by Poppa Mintin View Post
    However, I put a breakpoint at 'Finder' line 5 and run the project. When it breaks at line 5 I single step to line 6 and the Local window tells me that 'box' contains PictureBox1. Fine, that's correct.

    I single step again and get the error message.
    Pausing the cursor over the icon in the taskbar(?) I and see a thumbnail of the form and I can see that picturebox1 does contain an image, and it's a valid image.
    What a crazy way to debug. Why didn't you just look at box.Image in the Locals window? That's what's throwing the exception so that is what you should have been looking at in the debugger. If you're told exactly where an exception is being thrown, the logical thing to do is to look in that place.

  8. #8

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Image tag not found.

    Quote Originally Posted by jmcilhinney View Post
    What a crazy way to debug. Why didn't you just look at box.Image in the Locals window?
    Ok, I did that, .Image still says Nothing.
    I may have somewhere else to look now though... I changed 'For one = 1 To 10' to 'For one = 2 To 10' and tried again. PictureBox2 showed it's image tag correctly, so...

    Now I have to see if I've moved the PictureBox1 image, but the form hasn't refreshed yet. That's a distinct possibility.

    When SWMBO gives me the opportunity, I'll check that out...


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Image tag not found.

    Did you take my advice and setup the conditional statements?
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  10. #10

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Image tag not found.

    Quote Originally Posted by dday9 View Post
    Did you take my advice and setup the conditional statements?
    Not yet, but advice noted.

    Pop
    Along with the sunshine there has to be a little rain sometime.

  11. #11

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: Image tag not found.

    Quote Originally Posted by Poppa Mintin View Post
    Now I have to see if I've moved the PictureBox1 image, but the form hasn't refreshed yet. That's a distinct possibility.
    I added a 'Me.Refresh()' before the 'For one = 1 To 10', ran the project again, checked again and lo n' behold !
    Yes, I've already moved the image.

    Oh bother !

    Back to the drawing-board.

    Problem solved, me again... might've known.

    Thanks for the help guys.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  12. #12
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: Image tag not found.

    Quote Originally Posted by Poppa Mintin View Post
    Not yet, but advice noted.

    Pop
    What I was going to suggest if you had taken my advice was that if no MessageBox appeared, then that confirms the image exists. My next piece of advice would have been to update the UI manually, which is what you ultimately did.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  13. #13

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: [RESOLVED] Image tag not found.

    Quote Originally Posted by .paul. View Post
    two = CInt(box.Image.Tag)

    Box has an Image property and a Tag property
    Image does not have a Tag property
    Bit late responding to this, we've just moved house.

    As John says, yes, images do have a tag. If we just look at (say) PictureBox1.Image, all it tells us that it's a bitmap, nowhere does it give the name of the image.

    In my case the image could be any of the 53 images in recourses (a full pack of playing cards and the back of the card). All 'boxes can have any one of 'em picked at random. I need to know which 'card' is in the clicked 'box.
    So when I've set up the image array variable from 'card(0)' (the back of the card) to card(52) (King of spades) in the alphabetical order Clubs, Diamonds, Hearts and Spades with which you may be familiar if you've played Bridge.

    But how to know which card is in which 'box?
    When I'm setting up the array 'card(52)' I assign each element's tag with it's index number, in that way I can deduce it's face value by:-
    Code:
            Suit = 0
            CardValue = ImgTag(box_Number)
            While CardValue > 13
                 CardValue -= 13
                 Suit += 1
            End While
    Code:
        Public Function ImgTag(ByVal box As Integer) As Integer
            Dim px As PictureBox = CType(Me.Controls("PictureBox" & box.ToString), PictureBox)
            If px.Image Is Nothing Then Return 0
            Return CInt(px.Image.Tag)
        End Function
    (Return 0 can't be the back of the card because no card back is 'clickable').
    Suit = 0... Clubs
    Suit = 1... Diamonds
    Suit = 2... Hearts
    Suit = 3... Spades
    CardValue 1 = Ace, 2 = Deuce... 13 = King.


    Poppa
    Along with the sunshine there has to be a little rain sometime.

  14. #14

    Thread Starter
    PowerPoster Poppa Mintin's Avatar
    Join Date
    Mar 2009
    Location
    Bottesford, North Lincolnshire, England.
    Posts
    2,423

    Re: [RESOLVED] Image tag not found.

    Hi,

    This project is complete for now, I may add a 'Hall Of Fame' feature at some later date.

    For anyone interested enough there is a taste of the project as it stands at this OneDrive location.

    Poppa
    Along with the sunshine there has to be a little rain sometime.

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