Dice Roller Populate TextBox(s) - vb.net

I am still learning basic programming, literally off Visual Basic 2012 v11.
Here is what I am stumped on:
I made a stupid-simple Table-Top Role-Playing Character Log application.
I made a simple dice roller/"random number" procedure and set it to fill a ListBox with each button click of "ROLL" until the max of 6 was reached. That worked fine.
However, I changed my app to link with a database to store simple character information for all of the people playing,etc. Which means I now have separate dataset TextBoxes for each click of "ROLL". Would someone please advise me on the best way to populate one TextBox per button click with a maximum of 6 clicks, please?
I was using a loop to fill the ListBox with exactly 6 entries, and started that line of thought for filling the TextBoxes, but my late-night tired brain cannot find a way to fill ONE box EACH TIME instead of all boxes every single time.
THANK YOU FOR YOUR HELP!
My Code:
Private Sub btnRoll_Click(sender As Object, e As EventArgs) Handles btnRoll.Click
'Roll a character stat up to 6 and add it to the lisbox
'D20 is standard die, but stats below 6 are forgiven for this campagin
Dim Dice As New Random
Dim DiceRoll As Integer = Dice.Next(6, 20)
Dim intCount As Integer = 0
Dim Stat As String = Convert.ToString(DiceRoll)
Do While intCount < 7
Loop
End Sub

I don't believe you've included enough information to give an informed answer.
From my understanding, you'd like to take the result of the roll, and input it into one of several textboxes (you haven't given a number of textboxes, but you'd like it done in less than 6 clicks).
Depending on what the different fields will be, you may want to include your calculations within the loop, along with this, to specify the output and then roll again X times within the loop:
Do While intcount < 7
Select Case intcount
Case 1
txtBox1 = stat
Case 2
txtBox2 = stat
Case 3
txtBox3 = stat
Case 4
txtBox4 = stat
Case 5
txtBox5 = stat
Case 6
txtBox6 = stat
Case Else
'Nothing if not specified with other arguments.
'Roll again
stat = Convert.ToString(Dice.Next(6,20))
Loop

Related

How Do I Count When Numbers Do Not Show Up

I have numbers from 1 to 25, four numbers will show up daily. I need to put a +1 on each of the four numbers and need to put a -1 on each of the 21 numbers didn't show up.
The four numbers that come up daily will be inputted in four different text boxes. The count being positive or negative needs to go on 25 separate text boxes labeled 1 thru 25.
I have tried "if textbox <> number, then count -= 1" but I get a count of -4 because it doesn't see the number in any of the four text boxes.
I only need a daily count not a textbox count. Sorry I don't have any code started and would greatly appreciate if someone can point me in the right direction. I'm doing this on Visual Studio 2012.
Thank you all for responding. Here is some code I've started but the count is not correct. My four input text boxes are in GroupBox2. Four numbers from 1 to 25 will draw daily like a lottery. The four numbers drawn will have a value of +1 each all others -1. I need to find the age of each number 1 thru 25. If a number has a +3 then that means that number has drawn 3 consecutive days. If a number has a -15 then that means that number has not drawn for the past 15 days.
Dim tb As New TextBox
Dim ctrl As Control
Dim Counter As Integer
For Each ctrl In GroupBox2.Controls
tb = ctrl
If tb.Text = 1 Then
Counter += 1
ElseIf tb.Text <> 1 Then
Counter -= 1
TextBox464.Text = Counter
End If
If tb.Text = 2 Then
Counter += 1
ElseIf tb.Text <> 2 Then
Counter -= 1
TextBox463.Text = Counter
End If
If tb.Text = 3 Then
Counter += 1
ElseIf tb.Text <> 3 Then
Counter -= 1
TextBox462.Text = Counter
End If
If tb.Text = 4 Then
Counter += 1
ElseIf tb.Text <> 4 Then
Counter -= 1
TextBox461.Text = Counter
End If
Next
We will need more information about how your going to approach it to be able to help you further, but as for your problem with this If Textbox <> number Then count -= 1 you can use something like this since your only going to be having numbers on the textboxes If Cint(Textbox.Text) <> number then count -= 1 since your using just Textbox its attempting to evaluate it as a control and not the property that your looking for, you need to read from its .Text Property, However since its evaluated as a String and not an Integer it will throw an error exception, thats why the Cint() is included (This may also be used to convert it to integer Ctype(number, Integer) Not sure if there is an execution speed difference or not, however Cint() is a faster way of writing it.) it will try and convert the String into an Integer and when its converted into an integer it can be evaluated like one using <>. No one is going to write a whole solution out for you, but while you attempt to create it yourself and more information can be added we are more than happy to help you with problems along the way.

Most efficient way to read the last line (lastindex) of listbox in vb.net

I am looking for the most efficient way to read the last line of listbox in visual basic
For example, if my listbox contains the following:
a
b
c
d
e
f
g
Then the output whenever I clicked the button should be the last line which is g
I am using this code to get the job done, however I am looking for the most effiicient and effective way for this when I have thousands or even hundred thousands of item in listbox.
Dim lastline As Integer = (lstData.Items.Count - 1)
lstData.Text = lstData.Items(lastline).ToString
events_class.events_function(lastline)
This will get a total number of contents in your list box.
'This code will set the selected index of the item to the last item in
'the list, regardless of how many items are in it.
If ListBox1.Items.Count > 0 Then _
ListBox1.SelectedIndex = ListBox.Items.Count - 1
Hope this will help you..
Regards

Visual Basic Data Grid View - Count Average

Okay, so I've got a datagrid view DataGridView1 which is something like the below example.
Name Points
Jack 15
zack 19
Cody 05
I want to be able to count the average of all of the points, However the amount of points will be dynamic and change from time to time. So my solution must be able to work even if there's just two numbers and when there's upwards of 20.
I have been looking a round for a way of doing this, but most posts are only relevant if the amount of 'points' are static. And a lot of the solutions appear to be written in C++, which isn't too much use to a newbie coder like myself on Visual Basic.
So could anyone help me out here?
I would force it to draw a nice extra line in the data grid, by using a union select and a blanks for each column in the sqldatabind. This would add a nice row to the dataset be formatted as a totals row.. You may need to number the rows so your totals row is last most because of ordering. add the term 'Total' as a control in the results or handle it another way.
After that its just a bit of tweaking when your grid draws the row..
something like ...
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim dRow As GridViewRow = sender
If dRow.RowType = DataControlRowType.DataRow Then
If dRow.Cells(0).Text = "Total" Then
Dim rx As Integer
Dim TotalValue As Double = 0
For rx = 0 To GridView1.Rows.Count - 2
TotalValue += CDbl(GridView1.Rows(rx).Cells(1).Text)
Next
dRow.Cells(1).Text = FormatNumber(TotalValue, 2)
End If
End If
End Sub

How can i get a large number of text boxs to subtract from each other

I'm trying to make a program where a user inputs numbers into a subtraction equation and the program tells them if they are right or wrong and what the correct answer is in a label. There are 20 different equations with 3 text boxes each. The first two text boxes are for the two numbers that are being subtracted and the third text box is the answer. I declared them into a array but I can't figure out how make them subtract. The code i have so far is:
Dim i As Integer
Dim txtNumber1() As TextBox = {txt1Number1, txt2Number1, txt3Number1, txt4Number1, txt5Number1, txt6Number1, txt7Number1, txt8Number1, txt9Number1, txt10Number1, txt11Number1, txt12Number1, txt13Number1, txt14Number1, txt15Number1, txt16Number1, txt17Number1, txt18Number1, txt19Number1, txt20Number1}
Dim txtNumber2() As TextBox = {txt1Number2, txt2Number2, txt3Number2, txt4Number2, txt5Number2, txt6Number2, txt7Number2, txt8Number2, txt9Number2, txt10Number2, txt11Number2, txt12Number2, txt13Number2, txt14Number2, txt15Number2, txt16Number2, txt17Number2, txt18Number2, txt19Number2, txt20Number2}
Dim txtAnswer() As TextBox = {txt1Answer, txt2Answer, txt3Answer, txt4Answer, txt5Answer, txt6Answer, txt7Answer, txt8Answer, txt9Answer, txt10Answer, txt11Answer, txt12Answer, txt13Answer, txt14Answer, txt15Answer, txt16Answer, txt17Answer, txt18Answer, txt19Answer, txt20Answer}
Dim intAnswer() As Integer
For i = 0 To txtNumber1.Length - 1
intAnswer(i) = txtNumber1(i) - txtNumber2(i)
Next
I also can't figure out how i would make each answer display into a label. I think it would be some like
If intAnswer(0) = txtAnswer(0) Then
Me.lblAnswer1.Text = "Correct:" & intAnswer(0)
Else
Me.lblAnswer1.Text = "Incorrect:" & intAnswer(0)
End If
But I'm not sure how i would loop that to make it do all 20 labels, or would i just need to have it 20 different times, one for each label.
Thanks for the help.
Best to create a user control with 3 labels and 3 textboxes on each. Then you only need to code this much, and wrap this logic in a loop to repeat as many times as you want. Basically, narrow down your problem to "I only have 1 equation", solve it using this approach, the rest is as easy as using adding a loop to your code.

VBA display selection options

I am trying to write a code that will display a value depending on what checkbox is selected. There are a total of 5 checkboxes and I will be adding additional checkboxes in the future so I was wondering if there is an easy way to determine which checkboxes are checked to determine which values to display. I can do this in a really round about way but I would like minimize my code if possible.
In other words, if i write each scenario out I would have to write a separate code for all of the different selection possbilities:
1 only,2 only,3 only,4 only,5 only
1+2, 1+3, 1+4, 1+5, 2+3, 2+4, 2+5, 3+4, 3+5, 4+5
1+2+3, 1+2+4,1+2+5, 1+3+4,1+3+5, 1+4+5,2+3+4, 2+3+5,3+4+5
1+2+3+4, 1+2+3+5, 1+3+4+5, 2+3+4+5
1+2+3+4+5
Each value is associated with a sub that will fill the array if it is selected. And after the arrays are filled I need to perform an additional function on the ones that are selected. The function performed is the same but I do not want to perform the function if a value is not selected because it will defeat the purpose of my function otherwise. The function itself is to select duplicates from the arrays that were selected into another array.
You can use binary numbers for each checkbox:
First value is 1 (=20)
Second value is 2 (=21)
Third value is 4 (=22)
Fourth value is 8 (=22)
Fifth value is 16 (=24)
Hence, when you sum each permutation, you have a unique number you can check.
1 only is 0, 2 only is 1 and so on
1+2 is 3, 1+3 is 5, 1+4 is 9, 1+5 is 17
and so on
You can create arrays with every case you want to check.
At least, i hope this will give you some ideas or tips.
Regards,
What #JMax is referring to is more commonly known as bit-masking. Here's a quick tutorial:
Create a sample form named Form1 with 5 checkboxes named Check1, Check2, Check3, Check4, Check5. Then add the following two functions in a standard code module:
Function GetSelectedBoxes() As Long
Dim Ctl As Control, Total As Long
For Each Ctl In Forms!form1
If Ctl.ControlType = acCheckBox Then
If Ctl Then
Total = Total + 2 ^ CLng(Mid(Ctl.Name, 6))
End If
End If
Next Ctl
GetSelectedBoxes = Total
End Function
Sub ShowSelectedBoxes()
Dim Total As Long, i As Integer
Total = GetSelectedBoxes
For i = 1 To 5
If Total And 2 ^ i Then Debug.Print "Check" & i & " selected"
Next i
End Sub
Now open Form1 and check boxes 1, 3, and 4. Run the ShowSelectedBoxes routine and you should get the following output in the immediate window:
Check1 selected
Check3 selected
Check4 selected
I used the For...Loop for compactness in my sample, but you can just as easily break it out into separate If...Then statements to do something more meaningful with the checked boxes.
This approach will support up to 31 or 32 separate checkboxes.