Results 1 to 25 of 25

Thread: [RESOLVED] Picture Box Issue

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Resolved [RESOLVED] Picture Box Issue

    I have a picture box on my Windows App Form. I created an Images Folder that has 6 different images in it. The Copy to Output Directory is set to Always for each of those images. Now Im supposed to set the Image location for the picture box to the Images folder but when I put the location of the images folder in there the picture box has a X on it. How do I fix this?

    Thanks in advance!!

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Picture Box Issue

    "Im supposed to set the Image location for the picture box to the Images folder" -- no, what you should be doing is setting the image location to a FILE in the Images folder... not the folder itself.
    Also are you doing this at runtime, or at design time? If at runtime, what does your code look like?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: Picture Box Issue

    Quote Originally Posted by techgnome View Post
    "Im supposed to set the Image location for the picture box to the Images folder" -- no, what you should be doing is setting the image location to a FILE in the Images folder... not the folder itself.
    Also are you doing this at runtime, or at design time? If at runtime, what does your code look like?

    -tg
    Im doing it on design time. But I have 6 different images so how would all of those be one location if its not the folder?

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Picture Box Issue

    Well a picturebox can only show one picture...

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: Picture Box Issue

    Quote Originally Posted by techgnome View Post
    Well a picturebox can only show one picture...

    -tg
    Well my assignment is to create a Hangman Game so I need the image to change as it goes, thats why I have 6 different images.

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

    Re: Picture Box Issue

    Are you required to have the images in a folder like that? If not, then I would suggest that you add them as resources (Project | Properties, on the Resources tab select Add Resource | Add Existing File).

    This will be much easier to work with than drawing the from a folder. Once added as a resource, all the pictures are available as My.Resources.SomeFileNameHere. So, when you needed to switch images, you'd just be setting the image to a new resource.

    If you can't use resources, and have to use files, then with that number of files, you might consider simply loading them. I don't believe there is an automated way to cycle through images in a folder, and even if there was it would be pretty brittle (how would it know the order?). So, you can load all the images into an array or list of images when the program starts, or you could load each one as needed. The former would be a bit easier, and likely a bit more efficient, too, but either way will work fine. In both cases, YOU will be the one saying which image is next. It might be the next image in an array, or a new image from a file name, but either way there isn't any automatic solution.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: Picture Box Issue

    Quote Originally Posted by Shaggy Hiker View Post
    Are you required to have the images in a folder like that? If not, then I would suggest that you add them as resources (Project | Properties, on the Resources tab select Add Resource | Add Existing File).

    This will be much easier to work with than drawing the from a folder. Once added as a resource, all the pictures are available as My.Resources.SomeFileNameHere. So, when you needed to switch images, you'd just be setting the image to a new resource.

    If you can't use resources, and have to use files, then with that number of files, you might consider simply loading them. I don't believe there is an automated way to cycle through images in a folder, and even if there was it would be pretty brittle (how would it know the order?). So, you can load all the images into an array or list of images when the program starts, or you could load each one as needed. The former would be a bit easier, and likely a bit more efficient, too, but either way will work fine. In both cases, YOU will be the one saying which image is next. It might be the next image in an array, or a new image from a file name, but either way there isn't any automatic solution.
    Im not sure the school says do add folder and name it images...but I dont understand how to set the image location so that all of them are used..?? I added them as resources like you said instead but what would the image location be for the picture box now that they are under resources?


    okay now I have another problem..because my instructions also says to set each image to Copy Always under the Copy To Output Directory but I cant do that now that they are considered a resource..so Im going to keep it as a folder like the instructions said instead of making them resource..but that still doesnt solve the image location issue
    Last edited by EmilyM1105; Feb 20th, 2018 at 05:31 PM.

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

    Re: Picture Box Issue

    Quote Originally Posted by EmilyM1105 View Post
    Well my assignment is to create a Hangman Game so I need the image to change as it goes, thats why I have 6 different images.
    The PictureBox can only display one image at a time. If you want the display to change then you need to change the ImageLocation to the path of a different file. Each time the user gets a letter wrong, you change the ImageLocation to the path of the next file. If you wanted a Label to display different text then you'd change the Text property of that Label, right? This is no different. If you want to change the image displayed, change the ImageLocation property.

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Picture Box Issue

    Yeah, it sounds like your instructions are to keep them in a folder, so using them as resources would not be right.

    I assume that these images are different images of a partial stick figure, but is that right? I am thinking that you start with one image that has none of the stick figure, then after each wrong guess you switch to a different image. If that isn't the way this is supposed to work, then how is it supposed to work. I don't want to be suggesting one thing when you actually need something completely different.

    EDIT: I see I was posting as JMC was posting, but he's under the same impression I am that the picture changes after each wrong guess. Is that right?
    My usual boring signature: Nothing

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Picture Box Issue

    {edit: As for the following post, I didn't see the previous three posts at the time I put this together}.

    Quote Originally Posted by EmilyM1105 View Post
    Well my assignment is to create a Hangman Game so I need the image to change as it goes, thats why I have 6 different images.
    As Shaggy Hiker says, you need to have six places to store the image. In one case, they are already stored in the folder on the disk, so you can just load them as needed, but that is somewhat involved.
    The alternative is to preload them somewhere, and doing that at run time can still be a bit involved.
    In your case, you're trying to load them at design time in the IDE, which is the easiest, and the way it should be done is just adding them to your resources as Shaggy suggests.

    But, if you want an easy, but less optimal solution, you could just accept the one image at a time in a picturebox and use multiple pictureboxes to hold the images. As already stated, this is not the best solution, but is fairly simple for a beginner to understand.

    As an example, start a new project and add a picturebox to the form. Set the visible property to False so you won't see the picturebox when you run the program.
    Now, copy that picturebox, and paste a copy of it to the form five more times. Now you should have six pictureboxes, all not visible when you run, and named Picturebox1 to Picturebox6.

    Create a seventh Picturebox from the tool box and size it to the size you need to display your images. For the example, name it HangDisplay. This is where you'll display your images one by one.

    Select the first picturebox (Picturebox1) and select the image propery and click the small button with the "..." on it to bring up the Select resource dialog. Select "Local resource:" and click on the "Import" button. Find the first image you want to use in the file dialog and select Open. On the "Select Resource" dialog select "OK" and you should see the picturebox now holds the image selected. Since you won't see this at run time, you don't have to size the picturebox to see the whole image, its all there.

    Repeat the process for each picturebox (2 to 5), loading each with the next image in the sequence.

    When you start the code, you can add these pictureboxes to an array so that you can access them by number to retrieve the image and assign it to the HangDisplay picturebox.

    If you set it up properly, you can test it with this code. You just need to add a button to step through each image to see it work.
    Code:
    Public Class Form1
      Private HangImgs() As PictureBox
      Private HangState As Integer
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        HangImgs = {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6}
        HangDisplay.Image = HangImgs(HangState).Image
      End Sub
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        HangState += 1
        If HangState > 5 Then
          HangState = 0  'game over, start a new game or perhaps game over when HangState = 5, display final state and offer a new game
        End If
        HangDisplay.Image = HangImgs(HangState).Image
      End Sub
    End Class
    Last edited by passel; Feb 20th, 2018 at 06:18 PM.

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Picture Box Issue

    {Note that I didn't see posts #7, #8 and #9 before writing this:}

    Of course, it is actually easier to do this better using resources, so lets see that in action.
    In this case we don't need to add the six pictureboxes so that is simpler already.
    Open up the Resource tab as Shaggy suggested
    If you select "Images, then "Add Existing File..." under "Add Resource", you get a directory listing.
    Browse to your folder, then holding the ctrl key and clicking each file you want, or click on the first file, then hold the Shift key when you click on the last file to select all the files you want to add.
    Click the "Open" key and all the files selected will be added to the Resource in one fell swoop.
    Now you can modify the previous example to add the resource images to an array of images, and the code will work similar to the previous example.

    {Note in my case, I just added six of the smallest size images that I had in a directory, so the resource identifier names are derived from the files I loaded. Yours would be different, and probably more meaningful}

    Using Resources to hold the images is actually quite a bit easier than the multiple pictureboxes example, and much less resource intensive.
    Code:
    Public Class Form1
      'Private HangImgs() As PictureBox
      Private HangImgs() As Image
      Private HangState As Integer
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ' HangImgs = {PictureBox1, PictureBox2, PictureBox3, PictureBox4, PictureBox5, PictureBox6}
        HangImgs = {My.Resources.CircleCompare,
                    My.Resources.LabelAligned,
                    My.Resources.mSummation,
                    My.Resources.RedBar,
                    My.Resources.testcursor,
                    My.Resources.uestTLu}
        HangDisplay.Image = HangImgs(HangState)
      End Sub
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        HangState += 1
        If HangState > 5 Then
          HangState = 0  'game over, start a new game or perhaps game over when HangState = 5, display final state and offer a new game
        End If
        HangDisplay.Image = HangImgs(HangState)
      End Sub
    End Class
    Last edited by passel; Feb 20th, 2018 at 06:15 PM.

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

    Re: Picture Box Issue

    I certainly agree that there are better ways to do this but, if the assignment tells you that you are supposed to use the ImageLocation property then that's probably what you should do. Some instructors will appreciate your taking initiative while others will not appreciate your not following instructions. In that case, I'd tend to suggest naming the images based on the number of wrong guesses that they represent. You can then store the number of wrong guess in an Integer variable and use that to create the file path, e.g.
    vb.net Code:
    1. myPictureBox.ImageLocation = IO.Path.Combine(folderPath, wrongGuessCount & ".jpg")

  13. #13
    Addicted Member
    Join Date
    Nov 2011
    Posts
    229

    Re: Picture Box Issue

    Just expanding a little on what has already been said.

    The images folder (directory) that you created is part of your application. To access the images you will first need to know the path to the images folder, this will be made up of the application startup path and the location of your folder in the IDE solution explorer, in my case the path was

    Code:
     Private FolderPath As String = IO.Path.Combine(Application.StartupPath, "images")
    FolderPath is now the string that contains the path to my images folder. You can see that I named each image with a number as jmcilhinney suggested. Next I tested the results using a form with one button and one picturebox, the x variable is incremented with each click of the button and resets to a value of one when x reaches four, cycling throught each of the images in turn.

    Code:
      Private FolderPath As String = IO.Path.Combine(Application.StartupPath, "images")
        Private x As Integer
    
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            x += 1
    
            If x = 4 Then
                x = 1
            End If
    
            PictureBox1.ImageLocation = IO.Path.Combine(FolderPath, x.ToString & ".jpg")
    
        End Sub
    Name:  screenshot.jpg
Views: 2704
Size:  40.0 KB

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: Picture Box Issue

    Quote Originally Posted by Shaggy Hiker View Post
    Yeah, it sounds like your instructions are to keep them in a folder, so using them as resources would not be right.

    I assume that these images are different images of a partial stick figure, but is that right? I am thinking that you start with one image that has none of the stick figure, then after each wrong guess you switch to a different image. If that isn't the way this is supposed to work, then how is it supposed to work. I don't want to be suggesting one thing when you actually need something completely different.

    EDIT: I see I was posting as JMC was posting, but he's under the same impression I am that the picture changes after each wrong guess. Is that right?
    Yes thats right.. I have written it out in my code but no matter what I put in the image location it keeps showing a red X.. Here is my Game Form code...

    Code:
    Public Class GameForm
        ' Define variables
        Dim dsQuestions As String           'Object variable
        Dim numWord As Integer              ' To hold index of word
        Dim word As String                  ' Hold actual word
        Dim numWrongGuesses As Integer      ' Hold number of wrong guesses
        Dim numRightGuesses As Integer      ' Hold number of right guesses
        Dim gameStarted As Boolean = False  ' Indicate if game has started or not
    
        Private Sub GameForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' Disable txtGuess textbox.
            txtGuess.Enabled =
                ' Set numWord variable.
            Dim numWord As Integer = 0
    
            ' Call the Load Data method.
            LoadData()
    
        End Sub
    
        Private Sub QuitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles QuitToolStripMenuItem.Click
            ' Close the form
            Me.Close()
        End Sub
    
        Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
            Dim dsQuestions As New QuestionDataSet
            Dim numWord As Integer = 1
            numWord += 1
            Dim word As String
            Dim numWrongGuesses As Integer = 10
            Dim numRightGuesses As Integer = 5
            Dim gameStarted As Boolean = True
    
            ' Initialize controls
            lblUnitDescription.Text = "Choose a game"
            txtGuess.Text = lblGuessALetter.Text
    
            ' Clear text of all labels.
            lblUnitDescription.Text = String.Empty
            txtGuess.Clear()
            lblLetter0.Text = String.Empty
            lblLetter1.Text = String.Empty
            lblLetter2.Text = String.Empty
            lblLetter3.Text = String.Empty
            lblLetter4.Text = String.Empty
            lblLetter5.Text = String.Empty
            lblLetter6.Text = String.Empty
            lblLetter7.Text = String.Empty
            lblLetter8.Text = String.Empty
            lblLetter9.Text = String.Empty
            lblLetter10.Text = String.Empty
            lblLetter11.Text = String.Empty
            ' Enable txtGuess. 
            txtGuess.Enabled = True
            ' Set the focus to txtGuess.
            txtGuess.Focus()
    
            ' Gets word column row
    
            Try
                word = CStr(dsQuestions.Words(numWord)(1))
                ' Gets unit description
                lblUnitDescription.Text = CStr(dsQuestions.Words(numWord).UnitsRow(1))
    
            Catch
    
                ' Set label control for character.
                lblLetter0.Text = "_"
                lblLetter1.Text = "_"
                lblLetter2.Text = "_"
                lblLetter3.Text = "_"
                lblLetter4.Text = "_"
                lblLetter5.Text = "_"
                lblLetter6.Text = "_"
                lblLetter7.Text = "_"
                lblLetter8.Text = "_"
                lblLetter9.Text = "_"
                lblLetter10.Text = "_"
                lblLetter11.Text = "_"
    
    
                Dim ch As Char = CChar(CStr(0))
                Dim ch1 As Char = "_"
                Dim ch2 As Char = "_"
                Dim ch3 As Char = "_"
                Dim ch4 As Char = "_"
                Dim ch5 As Char = "_"
                Dim ch6 As Char = "_"
                Dim ch7 As Char = "_"
                Dim ch8 As Char = "_"
                Dim ch9 As Char = "_"
                Dim ch10 As Char = "_"
                Dim ch11 As Char = "_"
            End Try
        End Sub
    
        Private Sub txtGuess_TextChanged(sender As Object, e As EventArgs) Handles txtGuess.TextChanged
            Dim gameStarted As Boolean = True
            Dim word As String = ""
            Dim numRightGuesses As Integer = 5
            Dim ch As Char
    
            If gameStarted = True Then
                If word.Contains(txtGuess.Text) Then
                    SetRightLetterLabels(word, ch)
                    numRightGuesses += 1
                End If
    
            ElseIf word.Contains(txtGuess.Text) = False Then
                pboxHangman.ImageLocation = "Images \ Hangman-  & numWrongGuesses & .png"
                numWrongGuesses += 1
            End If
            CheckProgress()
    
        End Sub
    
        Private Sub CheckProgress()
            ' Calculates the total number of tries.
            Dim numTries As Integer
            numTries = numRightGuesses + numWrongGuesses
            ' Displays messages
            If numRightGuesses = 5 Then
                MessageBox.Show("You in 5 tries!" & ControlChars.CrLf &
                                "Do you want to play another game?", "You won!",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Information)
            ElseIf numWrongGuesses = 10 Then
                MessageBox.Show("You in 10 tries. The word was ''." & ControlChars.CrLf &
                                "Do you want to play another game?", "You lost",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
            End If
        End Sub
    
        Private Sub WonGame()
        End Sub
    
        Private Sub LostGame()
            Dim numWrongGuesses As Integer
            If numWrongGuesses > 6 Then
                numWrongGuesses = True
            End If
            End
    
            lblLetter0.Text = "_"
            lblLetter1.Text = "_"
            lblLetter2.Text = "_"
            lblLetter3.Text = "_"
            lblLetter4.Text = "_"
            lblLetter5.Text = "_"
            lblLetter6.Text = "_"
            lblLetter7.Text = "_"
            lblLetter8.Text = "_"
            lblLetter9.Text = "_"
            lblLetter10.Text = "_"
            lblLetter11.Text = "_"
        End Sub
    
        Private Sub LoadData()
            Dim dsQuestions As New QuestionDataSet
            Dim taUnits As New QuestionDataSetTableAdapters.UnitsTableAdapter
            Dim taWords As New QuestionDataSetTableAdapters.WordsTableAdapter
            taUnits.Fill(dsQuestions.Units)
            taWords.Fill(dsQuestions.Words)
        End Sub
    Private Function FindLabel(ByVal i As Integer) As Control
            ' Finds the right label in the word panel
            Dim label = panelWord.Controls.Find("lblLetter", False)(0)
            Return label
    End Function
    
    Private Sub SetRightLetterLabels(ByVal word As String, ByVal ch As Char)
            For i As Integer = 0 To word.Length - 1
                ' Check for each letter and set the appropriate letter
                If word.Chars(i) = ch Then
    FindLabel(i).Text = ch
                End If
            Next
        End Sub
    End Class

  15. #15
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Picture Box Issue

    Code:
    pboxHangman.ImageLocation = "Images \ Hangman-  & numWrongGuesses & .png"
    Anything inside of quotes is treated literally. So, given the following code:

    Code:
    Dim strMyMessage As String = "Hello!"
    Dim intMyNumber As Integer = 1
    
    MessageBox.Show("strMyMessage")
    MessageBox.Show(strMyMessage)
    MessageBox.Show("intMyNumber.ToString")
    MessageBox.Show(intMyNumber.ToString)
    Would pop up messages in the following order:

    Code:
    strMyMessage
    Hello!
    intMyNumber.ToString
    1
    From this, you can see that including a variable name inside of quotes does not result in the value stored in that variable to be used.

    To demonstrate this, take the path you are trying to use to load an image from, and pop it into a MessageBox statement:

    Code:
    MessageBox.Show("Images \ Hangman-  & numWrongGuesses & .png")
    You should see that what is displayed is probably not the intended path to one of your image files.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: Picture Box Issue

    Quote Originally Posted by OptionBase1 View Post
    Code:
    pboxHangman.ImageLocation = "Images \ Hangman-  & numWrongGuesses & .png"
    Anything inside of quotes is treated literally. So, given the following code:

    Code:
    Dim strMyMessage As String = "Hello!"
    Dim intMyNumber As Integer = 1
    
    MessageBox.Show("strMyMessage")
    MessageBox.Show(strMyMessage)
    MessageBox.Show("intMyNumber.ToString")
    MessageBox.Show(intMyNumber.ToString)
    Would pop up messages in the following order:

    Code:
    strMyMessage
    Hello!
    intMyNumber.ToString
    1
    From this, you can see that including a variable name inside of quotes does not result in the value stored in that variable to be used.

    To demonstrate this, take the path you are trying to use to load an image from, and pop it into a MessageBox statement:

    Code:
    MessageBox.Show("Images \ Hangman-  & numWrongGuesses & .png")
    You should see that what is displayed is probably not the intended path to one of your image files.
    okay... This is the location to the Images Folder C:\Users\ENJ1105\source\repos\HangmanApp\HangmanApp\Resources\Images

    Here are my instructions...

    A PictureBox control that will display the hangman images. You’ll set the ImageLocation property to load the hangman images.

    Check to see if the word (word) contains the guess (txtGuess).
    If it does, then call the SetRightLetterLabels method (provided below) and increment the numRightGuesses variable.
    If it doesn’t, set the image in the pboxHangman and increment the numWrongGuesses variable.
    Your code should resemble the following:
    pboxHangman.ImageLocation = “images\Hangman-” &
    numWrongGuesses & “.png”
    numWrongGuesses += 1

  17. #17
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Picture Box Issue

    Quote Originally Posted by EmilyM1105 View Post
    Your code should resemble the following:
    pboxHangman.ImageLocation = “images\Hangman-” & numWrongGuesses & “.png”
    Does your code resemble that? If not, then that's probably a good place to start.

  18. #18
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,531

    Re: Picture Box Issue

    Your code should resemble the following:
    pboxHangman.ImageLocation = “images\Hangman-” & numWrongGuesses & “.png”
    numWrongGuesses += 1
    But your code doesn't...
    your code looks like this:
    Code:
                pboxHangman.ImageLocation = "Images \ Hangman-  & numWrongGuesses & .png"
                numWrongGuesses += 1
    You're missing some quotes there.... that's what Option was trying to tell you... you've got some code inside the quotes that shouldn't be.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: Picture Box Issue

    Quote Originally Posted by techgnome View Post
    But your code doesn't...
    your code looks like this:
    Code:
                pboxHangman.ImageLocation = "Images \ Hangman-  & numWrongGuesses & .png"
                numWrongGuesses += 1
    You're missing some quotes there.... that's what Option was trying to tell you... you've got some code inside the quotes that shouldn't be.

    -tg
    Oh I didnt even notice that!! I changed that part..but Im still having trouble with setting the Image location on the Design part of it...

    Heres my updated Game Form Code:

    Code:
    Public Class GameForm
        ' Define variables
        Dim dsQuestions As String           'Object variable
        Dim numWord As Integer              ' To hold index of word
        Dim word As String                  ' Hold actual word
        Dim numWrongGuesses As Integer      ' Hold number of wrong guesses
        Dim numRightGuesses As Integer      ' Hold number of right guesses
        Dim gameStarted As Boolean = False  ' Indicate if game has started or not
    
        Private Sub GameForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' Disable txtGuess textbox.
            txtGuess.Enabled =
                ' Set numWord variable. 
            Dim numWord As Integer = 0
    
            ' Call the Load Data method.
            LoadData()
    
        End Sub
    
        Private Sub QuitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles QuitToolStripMenuItem.Click
            ' Close the form
            Me.Close()
        End Sub
    
        Private Sub NewToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles NewToolStripMenuItem.Click
            Dim dsQuestions As New QuestionDataSet
            Dim numWord As Integer = 1
            numWord += 1
            Dim word As String
            Dim numWrongGuesses As Integer = 10
            Dim numRightGuesses As Integer = 5
            Dim gameStarted As Boolean = True
    
            ' Initialize controls
            lblUnitDescription.Text = "Choose a game"
            txtGuess.Text = lblGuessALetter.Text
    
            ' Clear text of all labels.
            lblUnitDescription.Text = String.Empty
            txtGuess.Clear()
            lblLetter0.Text = String.Empty
            lblLetter1.Text = String.Empty
            lblLetter2.Text = String.Empty
            lblLetter3.Text = String.Empty
            lblLetter4.Text = String.Empty
            lblLetter5.Text = String.Empty
            lblLetter6.Text = String.Empty
            lblLetter7.Text = String.Empty
            lblLetter8.Text = String.Empty
            lblLetter9.Text = String.Empty
            lblLetter10.Text = String.Empty
            lblLetter11.Text = String.Empty
            ' Enable txtGuess. 
            txtGuess.Enabled = True
            ' Set the focus to txtGuess.
            txtGuess.Focus()
    
            ' Gets word column row
    
            Try
                word = CStr(dsQuestions.Words(numWord)(1))
                ' Gets unit description
                lblUnitDescription.Text = CStr(dsQuestions.Words(numWord).UnitsRow(1))
    
            Catch
    
                ' Set label control for character.
                lblLetter0.Text = "_"
                lblLetter1.Text = "_"
                lblLetter2.Text = "_"
                lblLetter3.Text = "_"
                lblLetter4.Text = "_"
                lblLetter5.Text = "_"
                lblLetter6.Text = "_"
                lblLetter7.Text = "_"
                lblLetter8.Text = "_"
                lblLetter9.Text = "_"
                lblLetter10.Text = "_"
                lblLetter11.Text = "_"
    
    
                Dim ch As Char = CChar(CStr(0))
                Dim ch1 As Char = "_"
                Dim ch2 As Char = "_"
                Dim ch3 As Char = "_"
                Dim ch4 As Char = "_"
                Dim ch5 As Char = "_"
                Dim ch6 As Char = "_"
                Dim ch7 As Char = "_"
                Dim ch8 As Char = "_"
                Dim ch9 As Char = "_"
                Dim ch10 As Char = "_"
                Dim ch11 As Char = "_"
            End Try
        End Sub
    
        Private Sub txtGuess_TextChanged(sender As Object, e As EventArgs) Handles txtGuess.TextChanged
            Dim gameStarted As Boolean = True
            Dim word As String = ""
            Dim numRightGuesses As Integer = 5
            Dim ch As Char
    
            If gameStarted = True Then
                If word.Contains(txtGuess.Text) Then
                    SetRightLetterLabels(word, ch)
                    numRightGuesses += 1
                End If
    
            ElseIf word.Contains(txtGuess.Text) = False Then
                pboxHangman.ImageLocation = “images\Hangman-” & numWrongGuesses & “.png”
                numWrongGuesses += 1
            End If
            CheckProgress()
    
        End Sub
    
        Private Sub CheckProgress()
            ' Calculates the total number of tries.
            Dim numTries As Integer
            numTries = numRightGuesses + numWrongGuesses
            ' Displays messages
            If numRightGuesses = 5 Then
                MessageBox.Show("You in 5 tries!" & ControlChars.CrLf &
                                "Do you want to play another game?", "You won!",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Information)
            ElseIf numWrongGuesses = 10 Then
                MessageBox.Show("You in 10 tries. The word was ''." & ControlChars.CrLf &
                                "Do you want to play another game?", "You lost",
                                MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation)
            End If
        End Sub
    
        Private Sub WonGame()
        End Sub
    
        Private Sub LostGame()
            Dim numWrongGuesses As Integer
            If numWrongGuesses > 6 Then
                numWrongGuesses = True
            End If
            End
    
            lblLetter0.Text = "_"
            lblLetter1.Text = "_"
            lblLetter2.Text = "_"
            lblLetter3.Text = "_"
            lblLetter4.Text = "_"
            lblLetter5.Text = "_"
            lblLetter6.Text = "_"
            lblLetter7.Text = "_"
            lblLetter8.Text = "_"
            lblLetter9.Text = "_"
            lblLetter10.Text = "_"
            lblLetter11.Text = "_"
        End Sub
    
        Private Sub LoadData()
            Dim dsQuestions As New QuestionDataSet
            Dim taUnits As New QuestionDataSetTableAdapters.UnitsTableAdapter
            Dim taWords As New QuestionDataSetTableAdapters.WordsTableAdapter
            taUnits.Fill(dsQuestions.Units)
            taWords.Fill(dsQuestions.Words)
        End Sub
    
        Private Function FindLabel(ByVal i As Integer) As Control
            ' Finds the right label in the word panel
            Dim label = panelWord.Controls.Find("lblLetter", False)(0)
            Return label
    End Function
    
    Private Sub SetRightLetterLabels(ByVal word As String, ByVal ch As Char)
            For i As Integer = 0 To word.Length - 1
                ' Check for each letter and set the appropriate letter
                If word.Chars(i) = ch Then
    FindLabel(i).Text = ch
                End If
            Next
        End Sub
    
    End Class
    location to the Images Folder C:\Users\ENJ1105\source\repos\HangmanApp\HangmanApp\Resources\Images
    Last edited by EmilyM1105; Feb 21st, 2018 at 04:33 PM.

  20. #20
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Picture Box Issue

    Quote Originally Posted by EmilyM1105 View Post
    but Im still having trouble with setting the Image location on the Design part of it...
    Can you clarify what you mean in the snippet I quoted above?

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: Picture Box Issue

    Quote Originally Posted by OptionBase1 View Post
    Can you clarify what you mean in the snippet I quoted above?
    Yes I can..so my instructions say to create an images folder that has the 6 different Hangman images..which I did

    The location to the folder is C:\Users\ENJ1105\source\repos\HangmanApp\HangmanApp\Resources\Images

    And the instructions say ... Add a PictureBox control that will display the hangman images. You’ll set the ImageLocation property to load the hangman images.

    So I created the picturebox control already but no matter what I put as the Image location it keeps giving me a red X in the Picturebox..?? (In design)
    Last edited by EmilyM1105; Feb 21st, 2018 at 06:57 PM.

  22. #22
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Picture Box Issue

    Don't get hung up on what the PictureBox looks like in Design mode, what matters is what it looks like when you run the program.

    I'm guessing that the intended idea is that when the form is loaded, you set the ImageLocation property of the PictureBox to load whatever the "first" image is supposed to be. Maybe there's one called "Hangman-0.png", I don't know. But you have the images, so you should know which image is supposed to be the one that shows up when the game begins.

    So in your GameForm_Load Sub, just add a line like:

    Code:
    pboxHangman.ImageLocation = "Images\WhateverTheFilenameOfTheInitialImageIs.png"

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: Picture Box Issue

    Quote Originally Posted by OptionBase1 View Post
    Don't get hung up on what the PictureBox looks like in Design mode, what matters is what it looks like when you run the program.

    I'm guessing that the intended idea is that when the form is loaded, you set the ImageLocation property of the PictureBox to load whatever the "first" image is supposed to be. Maybe there's one called "Hangman-0.png", I don't know. But you have the images, so you should know which image is supposed to be the one that shows up when the game begins.

    So in your GameForm_Load Sub, just add a line like:

    Code:
    pboxHangman.ImageLocation = "Images\WhateverTheFilenameOfTheInitialImageIs.png"
    Your exactly right the first file is called "Hangman-0.png" ..Okay so even if I just put one location itll still play out the others? I set the Image Location Property to C:\Users\ENJ1105\source\repos\HangmanApp\HangmanApp\Resources\Images\Hangman-0.png
    and the picture comes up fine. I was just concerned about the others.

  24. #24
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Picture Box Issue

    Quote Originally Posted by EmilyM1105 View Post
    Your exactly right the first file is called "Hangman-0.png" ..Okay so even if I just put one location itll still play out the others? I set the Image Location Property to C:\Users\ENJ1105\source\repos\HangmanApp\HangmanApp\Resources\Images\Hangman-0.png
    and the picture comes up fine. I was just concerned about the others.

    Getting the other images to display at the right time is the entire point of the below line of code that you were asking about earlier:

    Code:
    pboxHangman.ImageLocation = "images\Hangman-" & numWrongGuesses & ".png"
    So that's already done.

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Jan 2018
    Posts
    136

    Re: Picture Box Issue

    Quote Originally Posted by OptionBase1 View Post
    Getting the other images to display at the right time is the entire point of the below line of code that you were asking about earlier:

    Code:
    pboxHangman.ImageLocation = "images\Hangman-" & numWrongGuesses & ".png"
    So that's already done.
    oh okay I read it back over again I understand how its working now. Thank you for your help!! Im having a couple of errors on the code as well, I will start a new thread.

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