We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

changing images for each wrong guess in hangman

sethRnda
sethRnda asked
on
Medium Priority
265 Views
Last Modified: 2011-09-20
here's the code I have to far......I can only change the image once: (


  Dim m As Image

        For Each m In ImageList1.Images



            If found = False Then


                btn.Visible = False
                Label2.Image = ImageList1.Images(1)

         


            End If





        Next m

shouldn't this code cycle to the next image is another incorrect guess is entered?
Comment
Watch Question

Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
what programming language are you using? looks like vb?

without a bit more of code I doubt we can tell you anything, but I guess (wildly) that you run this code when a choice has been made, but this will only set 1 image, always the same one.
you will have to control which of the images you have already displayed...

also, you do                
Label2.Image = ImageList1.Images(1)
and it should rather be like this:
Label2.Image = m

or probably better:
have a variable (n) that counts the number of wrong guesses, and simply get that image, without any loop:

Label2.Image = ImageList1.Images(n)




Author

Commented:
I've tried this

dim guess as integer

for img = 0 to val(strWord.length +2)

if found = false then

btn.visible = false
label2.image = imagelist1.images(img)

next img

but it doesn't cycle throught the imagelist

and yes I'm using vb.net
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
why do you cycle through the list?
you "know" exactly which image you want to display...

simply do this line:
label2.image = imagelist1.images(val(strWord.length) +2)

Author

Commented:
 Dim m As Integer
        m = 1





        If found = False Then


            btn.Visible = False
            Label2.Image = ImageList1.Images(m)


            m = m + 1


        End If
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
yes, that looks good...

Author

Commented:
it's not working though : (
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
the variable m needs to be a member of the form, not the function you are calling to change the image...

Author

Commented:
so i should declare it in the form load sub?
Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009
Commented:
no, not in the form_sub, but on the form class level:

Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim m As Integer = 0

    Private Sub Form1_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
        ....
    End Sub

    ...

End Class

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
thanks, it works, but could you explain why it needs to be declared there?
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
>thanks, it works, but could you explain why it needs to be declared there?
if you put it into the function, it will be "erased" from memory when the function is completed.
you can verify that from adding a watch on the variable, and see that it cannot be watched when you go out of the function.
also, when you get back into the function, you will notice that the variable gets recreated and reinitialized to null value ...

putting it at form level will prevent the variable to get out of scope, so keep the value between the function calls..

Author

Commented:
that makes sense...thankyou very much angel: )

Author

Commented:
hey angel...have you ever seen a function that would completely refresh a form....?
Guy Hengel [angelIII / a3]Billing Engineer
CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2009

Commented:
well, the function might, believe it or not, be named Refresh() ...
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.