-
Sep 20th, 2021, 05:40 AM
#1
Thread Starter
PowerPoster
[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:
Public Function Primer(sender As Object, e As System.EventArgs) As Boolean
Dim one, two As Integer, box As PictureBox
one = CInt(DirectCast(sender, PictureBox).Tag)
box = CType(Me.Controls("PictureBox" & one.ToString), PictureBox)
two = CInt(box.Image.Tag)
vb.NET Code:
Public Function Finder() As Integer
Dim box As PictureBox, two As Integer
For one = 1 To 10
box = CType(Me.Controls("PictureBox" & one.ToString), PictureBox)
two = CInt(box.Image.Tag)
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.
-
Sep 20th, 2021, 06:42 AM
#2
Re: Image tag not found.
Originally Posted by Poppa Mintin
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.
-
Sep 20th, 2021, 08:35 AM
#3
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
-
Sep 20th, 2021, 09:41 AM
#4
Thread Starter
PowerPoster
Re: Image tag not found.
Originally Posted by jmcilhinney
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.
-
Sep 20th, 2021, 09:56 AM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 20th, 2021, 10:18 AM
#6
Re: Image tag not found.
Originally Posted by .paul.
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.
-
Sep 20th, 2021, 10:20 AM
#7
Re: Image tag not found.
Originally Posted by Poppa Mintin
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.
-
Sep 20th, 2021, 11:19 AM
#8
Thread Starter
PowerPoster
Re: Image tag not found.
Originally Posted by jmcilhinney
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.
-
Sep 20th, 2021, 11:20 AM
#9
Re: Image tag not found.
Did you take my advice and setup the conditional statements?
-
Sep 20th, 2021, 11:22 AM
#10
Thread Starter
PowerPoster
Re: Image tag not found.
Originally Posted by dday9
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.
-
Sep 20th, 2021, 11:27 AM
#11
Thread Starter
PowerPoster
Re: Image tag not found.
Originally Posted by Poppa Mintin
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.
-
Sep 20th, 2021, 11:38 AM
#12
Re: Image tag not found.
Originally Posted by Poppa Mintin
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.
-
Sep 22nd, 2021, 05:08 PM
#13
Thread Starter
PowerPoster
Re: [RESOLVED] Image tag not found.
Originally Posted by .paul.
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.
-
Sep 24th, 2021, 07:18 AM
#14
Thread Starter
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|