5 Replies - 609 Views - Last Post: 09 March 2015 - 05:46 AM Rate Topic: -----

#1 ZainHikary  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 15

Help with hold command

Posted 09 March 2015 - 12:59 AM

Hello ,this is my first post and i have no idea if im doing it right
Im trying to create a dice for yahtzee game in vb , i got everything working as in rolling random and stuff and images
The thing im not grtting right and its spinning my head around is
Im using labels with imagelist in it for dices
Im trying to find a way , so when i click on the label , it changes border style , i got that but i want it when i press of label it changes border and hold the dice from being rolled again ,?
Im using
iF command
But anyone can help , i searched everywhere !!
Any help would be appreciated :D

Is This A Good Question/Topic? 0
  • +

Replies To: Help with hold command

#2 frazereastm  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 88
  • Joined: 03-December 14

Re: Help with hold command

Posted 09 March 2015 - 01:30 AM

post the code you have please,
but if you want to do this a simple way you could declare a value at the top of your code i.e.
Dim clicked as integer = 1


add a click event to your label that controls the value of clicked
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
if clicked = 1 then
clicked =2
else clicked =1
end sub


then embed your rolling code into an if statement similar to
 if clicked = 2 then
'do something other than roll the dice
else 'put your dice rolling code here


I'd like to mention there are better ways of doing this this is just an easy method for a beginner to understand as most beginners encounter if statements and integer values quite early
Was This Post Helpful? 0
  • +
  • -

#3 ZainHikary  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-March 15

Re: Help with hold command

Posted 09 March 2015 - 01:46 AM

 Private Sub btnroll_Click(sender As Object, e As EventArgs) Handles btnroll.Click
        Dim Roll As New Random()
        die1 = Roll.Next(7)
        die2 = Roll.Next(7)
        die3 = Roll.Next(7)
        die4 = Roll.Next(7)
        die5 = Roll.Next(7)
        lbldie1.ImageIndex = die1
        lbldie2.ImageIndex = die2
        lbldie3.ImageIndex = die3
        lbldie4.ImageIndex = die4
        lbldie5.ImageIndex = die5
        Static clicks As Integer
        clicks += 1
        If clicks = 3 Then btnroll.Enabled = False 


thats what i got for the button that rolls that stops after the 3rd roll , which what i need
but when i wanna click on the label i just wanna hold it and not being rolled the other 2 times or any of the roll , unless i unhold it ,
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Help with hold command

Posted 09 March 2015 - 04:02 AM

I guess the way you have this set up, you could just check to see if the lbl is enabled before rolling the die#

If lbldie1.Enabled Then die1.Roll.Next(7)
of course using this same method for all 5 die

This post has been edited by CharlieMay: 09 March 2015 - 04:03 AM

Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1726
  • View blog
  • Posts: 5,704
  • Joined: 25-September 09

Re: Help with hold command

Posted 09 March 2015 - 05:43 AM

Instead of lbldie1.Enabled, the code above should check your label style and act accordingly.
Edit, also, you're going to want to fix up that rand.next as I don't think you want to roll 0's

This post has been edited by CharlieMay: 09 March 2015 - 05:43 AM

Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1432
  • View blog
  • Posts: 3,656
  • Joined: 01-February 13

Re: Help with hold command

Posted 09 March 2015 - 05:46 AM

Hi,

Changing the BorderStyle when any of the Labels are clicked is pretty easily done with a very short piece of code. You can add all the Label Click event handlers to one Click event sub so it is triggered no mater which Label is clicked.

Then inside the Click even sub you can use the "sender" object of the first parameter of the sub to get the Label that was clicked on. You would Cast the "sender" to a Label and then check it to see if the Label`s BorderStyle is None or FixedSingle and switch it to the opposite BorderStyle. Use a simple If Then Else statement to do that.

Notice i added all the Click handlers to the end of the sub.
    Private Sub Label1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.Click, Label2.Click, Label3.Click, Label4.Click, Label5.Click, Label6.Click
        Dim lbl As Label = DirectCast(sender, Label) 'cast the sender to a Label

        'Check if the BorderStyle of the label is None. If it is then set it to FixedSingle.
        'Else if it is not None then set it to None.
        If lbl.BorderStyle = BorderStyle.None Then
            lbl.BorderStyle = BorderStyle.FixedSingle
        Else
            lbl.BorderStyle = BorderStyle.None
        End If
    End Sub



Now, inside your Roll Button`s Click event you would check each Label`s BorderStyle to see if it is set to None before getting the new Random number and setting its Image from the ImageList.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Label1.BorderStyle = BorderStyle.None Then 'if BorderStyle is None then it is not being held so roll it

            d1 = R.Next(6) + 1 'die value needs to be between 1 and 6 so add 1 to the random number

            Label1.Image = ImageList1.Images(d1 - 1) 'subtract 1 from value because imagelists are 0 indexed 0 to 5 would be the 6 images

        End If
        If Label2.BorderStyle = BorderStyle.None Then 'if BorderStyle is None then it is not being held so roll it
            d2 = R.Next(6) + 1
            Label2.Image = ImageList1.Images(d2 - 1)
        End If
        If Label3.BorderStyle = BorderStyle.None Then 'if BorderStyle is None then it is not being held so roll it
            d3 = R.Next(6) + 1
            Label3.Image = ImageList1.Images(d3 - 1)
        End If
        If Label4.BorderStyle = BorderStyle.None Then 'if BorderStyle is None then it is not being held so roll it
            d4 = R.Next(6) + 1
            Label4.Image = ImageList1.Images(d4 - 1)
        End If
        If Label5.BorderStyle = BorderStyle.None Then 'if BorderStyle is None then it is not being held so roll it
            d5 = R.Next(6) + 1
            Label5.Image = ImageList1.Images(d5 - 1)
        End If
        If Label6.BorderStyle = BorderStyle.None Then 'if BorderStyle is None then it is not being held so roll it
            d6 = R.Next(6) + 1
            Label6.Image = ImageList1.Images(d6 - 1)
        End If

        rolls += 1
        If rolls = 3 Then Button1.Enabled = False
    End Sub



Also, unless you have more than 6 images in the ImageList for some reason you want to use 6 in the Random.Next method.

This post has been edited by IronRazer: 09 March 2015 - 05:53 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1