I need it so the next number generated is greater than the last... this is my first project with object-oriented programming, so I don't know much. Also, how do I make it so it runs a certain number of simulations before it lands on a number grouping? It would be greatly appreciated.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As Object, e As EventArgs) Handles Button1.Click
Randomize()
TextBox1.Text = Rand(1, 100)
TextBox2.Text = Rand(1, 100)
TextBox3.Text = Rand(1, 100)
TextBox4.Text = Rand(1, 100)
TextBox5.Text = Rand(1, 100)
TextBox6.Text = Rand(1, 100)
TextBox7.Text = Rand(1, 100)
TextBox8.Text = Rand(1, 200)
End Sub
Public Function Rand(ByVal Low As Long, ByVal High As Long) As Long
Rand = Int((High - Low + 1) * Rnd()) + Low
End Function
End Class
Without getting into any other coding issues with your example:
TextBox2.Text = Rand(Long.Parse(TextBox1.Text), 100)
TextBox3.Text = Rand(Long.Parse(TextBox2.Text), 100)
' ... etc.
The 100 is based off your code, you may have some algorithm for setting the next higher range other than set values. If your first random number is 100 then the rest of your calculations are going to be non-random!
Create a list of the text boxes you want to fill at the form level.
Private lstTextBoxes As List(Of TextBox)
Fill the list in the Form.Load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lstTextBoxes = New List(Of TextBox) From {TextBox1, TextBox2, TextBox3}
End Sub
Use the .net Random class. It is easier than the old one.
Private Rand As New Random
Now you can loop through your text boxes and fill with a "random" number. Each iteration will be a higher number than the last but will stop when it reaches 100.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim highNumber As Integer
For Each txtBox As TextBox In lstTextBoxes
If highNumber >= 99 Then
Return
End If
highNumber = Rand.Next(highNumber + 1, 100)
txtBox.Text = highNumber.ToString
Next
End Sub
Related
When running code on a Form, sometimes (usually when looping) the program title displays "Not Responding" even though its still running fine.
In my case, I'm looping and filling an array > array to DataTable > DataTable to DataGridView.
example:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim ArrayNew(1000001, 2) As Object
For i = 0 To 1000000
For j = 0 To 1
ArrayNew(i, j) = "TEST"
Next j
Next i
Table1 = New DataTable("EXAMPLETABLE")
Table1.Columns.Add("COLUMN1")
Table1.Columns.Add("COLUMN2")
Table1.Columns.Add("COLUMN3")
Dim RowNo As Integer = 0
Do
Table1.Rows.Add(New String() {RowNo + 1, ArrayNew(RowNo, 0), ArrayNew(RowNo, 1)})
RowNo = RowNo + 1
Loop Until ArrayNew(RowNo, 1) = ""
DataGridView1.DataSource = Table1
End Sub
I read somewhere that I should be using threads? or BackgroundWorker but I'm unsure how to use these here like a lot of questions point to BackgroundWorker Class.
But I don't understand C#.
The first thing to do is to drop a BackgroundWorker component onto the form. Click on the component and setup handlers for the DoWork, RunWorkerCompleted, and ProgressChanged events. Then, assuming you use all the default names, your code will look like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
backgroundWorker1.RunWorkerAsync()
End Sub
Private Sub backgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles backgroundWorker1.DoWork
Dim worker As BackgroundWorker = DirectCast(sender, BackgroundWorker)
Dim Table1 As New DataTable("EXAMPLETABLE")
Table1.Columns.Add("COLUMN1")
Table1.Columns.Add("COLUMN2")
Table1.Columns.Add("COLUMN3")
Dim Size As Integer = 1000000
For i As Integer = 1 To Size
Table1.Rows.Add(New String() {i.ToString(), "Test", "Test"})
If i Mod 50 = 0 Then
worker.ReportProgress(i * 100.0 / Size) 'Report as a percentage of the total
End If
Next
e.Result = Table1
End Sub
Private Sub backgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs)
DataGridView1.DataSource = DirectCast(e.Result, DataTable)
End Sub
Private Sub backgroundWorker1_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles backgroundWorker1.ProgressChanged
'Check e.ProgressPercentage here
End Sub
This is the basic example. You can also use this to do things like report progress and make sure it's not already busy before starting it. More information (including samples in C#, but translates to VB.Net easily) is here:
https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.backgroundworker?view=netcore-3.1
This is the code I have so far but is not working properly. The textbox for the names user should input and the button show to display the names in a label in the order entered.
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim intcount, w1, w2, w3 As Integer
Dim intMax As Integer = 2
For intcount = 0 To intMax
strSurnames(intcount) = TextBox1.Text
Next
TextBox1.Clear()
End Sub
Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
Dim intMax As Integer = 2
For intcount = 0 To intMax
lblShow.Text &= strSurnames(intcount)
Next
End Sub
I am guessing you are going to use only 1 textbox and click the "add button" multiple times to store the name. If this is true, you will first need to create
Dim arrayStr As New List(Of String)
Every single time you click on the add button, it will add into this array.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
arrayStr.Add(TextBox1.Text.Trim())
TextBox1.Clear()
End Sub
And to show the full name in just one label when you click on the "Show button", can do it like this
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = "" //The label text is cleared.
For i As Integer = 0 To arrayStr.Count() - 1
Label1.Text += arrayStr(i) + ", "
Next
End Sub
UPDATE - BASED ON YOUR COMMENT QUESTION
This is the updated solution based on your question. I am only going to show you with the "Sur Names" only. You can implement the "Weight" the same way.
First, create your array and declare a count as integer for the size of your array.
Dim surNameStr(20) As String
Dim count As Integer = 0
In the "Add Button", you increase the count number by 1 every time you add a new sur name. Once it reached your "maximum" number, you disable your button by BtnAdd.Enabled = False.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If count < 20 Then
surNameStr(count) = TextBox1.Text.Trim()
count = count + 1
Else
Button1.Enabled = False
Button2.Enabled = True
End If
End Sub
Then, in "Show Button", this is how you can show all the surnames stored.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = ""
For i As Integer = 0 To 19
Label1.Text += surNameStr(i) + vbNewLine
Next
End Sub
I am new to VB and I need to create a program for a skating rink. The number of judges varies from week to week and at the end of each skater's routine each judge assigns a score 0-10 to the skater. The application needs to allow the manger of the rink to enter each judge's score for a specific skater. It should also calculate and display the skater's average, the number of scores entered, and the total score. You select each score from a List box and then click the Record Score button.
I have no idea where to go next. Everything is incorrect when you select the first score from the list box and its also re-adding the score to the bottom of the list box. Below is the code and any help is appreciated.
Thanks!
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
'Fill list box with values
For intScore As Integer = 0 To 10 Step 1
lstScore.Items.Add(intScore.ToString)
Next intScore
lstScore.SelectedItem = "0"
End Sub
Private Sub btnRecordScore_Click(sender As Object, e As EventArgs) Handles btnRecordScore.Click
'accumulates the scores for each skater
Dim intScore As Integer
Dim intNumScores As Integer
Dim intTotalScore As Integer
Dim decAvgScore As Decimal
Integer.TryParse(lstScore.SelectedItem.ToString, intScore)
'add score to score
lstScore.Items.Add(lstScore.SelectedItem)
'update average
intNumScores = 0
intTotalScore = 0
For Each selScore As Object In lstScore.Items
If Integer.TryParse(selScore.ToString, intScore) Then
intNumScores = intNumScores + intScore
intTotalScore = intTotalScore + 1
End If
Next
decAvgScore = intNumScores \ intTotalScore
lblTotalScore.Text = intNumScores.ToString
lblNumScores.Text = intTotalScore.ToString
lblAvgScore.Text = decAvgScore.ToString("N1")
End Sub
Private Sub btnNextSkater_Click(sender As Object, e As EventArgs) Handles btnNextSkater.Click
'clears screen, sets focus
lblTotalScore.Text = String.Empty
lblNumScores.Text = String.Empty
lblAvgScore.Text = String.Empty
lstScore.Focus()
End Sub
End Class
Try this out.
Public Class Form1
'Declare TotalScore and NumScore here so they're values stay constant
Dim intTotalScore As Integer = 0
Dim intNumScores As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Populate list box with scores
For intScore As Integer = 0 To 10 Step 1
lstScore.Items.Add(intScore.ToString)
Next intScore
lstScore.SelectedItem = "0"
End Sub
Private Sub btnRecordScore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecordScore.Click
Dim intScore As Integer
Dim AvgScore As Decimal
'add score to score
intScore = lstScore.SelectedIndex
intTotalScore = intTotalScore + lstScore.SelectedIndex
'update average
intNumScores = intNumScores + 1
AvgScore = intTotalScore / intNumScores
'Populate labels
lblTotalScore.Text = intTotalScore.ToString
lblNumScores.Text = intNumScores.ToString
lblAvgScore.Text = AvgScore.ToString
End Sub
Private Sub btnNextSkater_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextSkater.Click
'clears screen, sets focus
intTotalScore = 0
intNumScores = 0
lblTotalScore.Text = String.Empty
lblNumScores.Text = String.Empty
lblAvgScore.Text = String.Empty
lstScore.SelectedItem = "0"
lstScore.Focus()
End Sub
End Class
Remember that lstScore.Items.Add(lstScore.SelectedItem) will only add another item to the listbox.
Hope this helps.
Im trying to make my visual basic code speak multiple text boxes with a delay of 30 seconds between each textbox, so far i have:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
End If
If My.Computer.FileSystem.FileExists(OpenFileDialog1.FileName) Then
Dim ioFile As New System.IO.StreamReader(OpenFileDialog1.FileName)
TextBox1.Text = ioFile.ReadLine()
TextBox2.Text = ioFile.ReadLine()
TextBox3.Text = ioFile.ReadLine()
TextBox4.Text = ioFile.ReadLine()
TextBox5.Text = ioFile.ReadLine()
TextBox6.Text = ioFile.ReadLine()
TextBox7.Text = ioFile.ReadLine()
TextBox8.Text = ioFile.ReadLine()
TextBox9.Text = ioFile.ReadLine()
TextBox10.Text = ioFile.ReadLine()
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim SAPI
SAPI = CreateObject("SAPI.spvoice")
SAPI.Speak(TextBox1.Text)
End Sub
End Class
Basically i have 10 text boxes that have 10 words in them (loaded form a txt file) and i have a txt to speech code saying the first textbox but i want that text to speech code linked to button 2 to say all the textboxes with a 30 second delay between each textbox, how would i go about doing this?
For this to work you probably want a multithreaded application. However if you don't a quick and dirty way of making it would would be:
system.threading.thread.sleep(30000)
But then the UI would lock up. So you really want this running on a different thread. A timer is the easiest way to implement this. Just have the timer.tick value set to 30,000. When button 2 is pressed have it create an array with all the textbox strings in. The have a global variable (integer). Each time the timer is ticked it speaks the text in the array at the ndex of the global variable, then increment the global variable!
http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.110).aspx
Here's an example using a BackgroundWorker() and an Enumerator built from the words in the TextBoxes:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim matches() As Control
Dim lines() As String = System.IO.File.ReadAllLines(OpenFileDialog1.FileName)
For i As Integer = 1 To 10
matches = Me.Controls.Find("TextBox" & i, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
If i <= lines.Count Then
DirectCast(matches(0), TextBox).Text = lines(i - 1)
Else
DirectCast(matches(0), TextBox).Text = ""
End If
End If
Next
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Not BackgroundWorker1.IsBusy Then
Dim words As New List(Of String)
Dim matches() As Control
For i As Integer = 1 To 10
matches = Me.Controls.Find("TextBox" & i, True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
Dim TB As TextBox = DirectCast(matches(0), TextBox)
If TB.Text.Trim <> "" Then
words.Add(TB.Text.Trim)
End If
End If
Next
If words.Count > 0 Then
BackgroundWorker1.RunWorkerAsync(words.GetEnumerator)
End If
End If
End Sub
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim SAPI = CreateObject("SAPI.spvoice")
Dim wordsEnum As IEnumerator(Of String) = DirectCast(e.Argument, IEnumerator(Of String))
While wordsEnum.MoveNext
SAPI.Speak(wordsEnum.Current)
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(30).TotalMilliseconds)
End While
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
MessageBox.Show("Done!")
End Sub
End Class
Public Class Form1
Dim num1 As Integer = CInt(Int((10 * Rnd()) + 1))
Dim num2 As Integer = CInt(Int((10 * Rnd()) + 1))
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
TextBox2.Text = num1 & "*" & num2
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = num1 * num2 Then
Label2.Text = "Correct!!11"
Else
Label2.Text = "Incorrect, sorry about that"
End If
End Sub
End Class
When i run this code, it only generates one question. which is 6*8. If i input 48, it works.But if i click the button again it will not generate another question. It will only generate 6*8. I need it to be able to generate random multiplication questions from 1-10
You're only generating num1 and num2 once, when the instance of the form is initialized, so every time you click the button, it's just reusing the same value.
You should generate a new values every time the button is clicked:
Dim num1 As Integer
Dim num2 As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
num1 = CInt(Int((10 * Rnd()) + 1))
num2 = CInt(Int((10 * Rnd()) + 1))
TextBox2.Text = num1 & "*" & num2
End Sub
If you get 6*8 every time you run the program, including completely stopping and rerunning,then that's really strange and I'm not sure what's up with that.
But if it's just that you're getting the same question every time you click the button, its because you put in a Rnd() when you declared the variables. If you want a new random number each time, you will have to put in some kind of loop where you reassign the variable each time.