making a bingo game and everyone has the same number - vb.net

I'm making a programme that creates a bingo card for 100 people and gives them all different numbers. but just now the code that i have is giving everyone the exact same 15 numbers. any help would be greatly appreciated, thanks.
Structure Number
Dim number As Integer
End Structure
Structure Player
Dim name As String
Dim numbers() As Number
Dim numbers_left As Integer
End Structure
Dim players As New List(Of Player)
Dim selectednumber As Integer
Dim used As New List(Of Integer)
Dim random As New Random
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Number As Number
Dim player As Player
ReDim player.numbers(14)
For i = 1 To 100
For j = 0 To 14
SelectNumber: Number.number = random.Next(1, 101)
If player.numbers IsNot Nothing Then
For Each item In player.numbers
If item.number = Number.number Then
GoTo SelectNumber
End If
Next
End If
player.numbers(j).number = Number.number
Next
player.name = ("Bill" & i)
player.numbers_left = 15
players.Add(player)
Next
End Sub

I'll admit I'm not completely following all the logic, but it looks like you need to re-initialize the player at each loop, since I think you're always testing against the first player.
Structure Number
Dim number As Integer
End Structure
Structure Player
Dim name As String
Dim numbers() As Number
Dim numbers_left As Integer
End Structure
Dim players As New List(Of Player)
Dim selectednumber As Integer
Dim used As New List(Of Integer)
Dim random As New Random
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 1 To 100
Dim Number As Number
Dim player As Player
ReDim player.numbers(14)
For j = 0 To 14
SelectNumber: Number.number = random.Next(1, 101)
If Player.numbers IsNot Nothing Then
For Each item In Player.numbers
If item.number = Number.number Then
GoTo SelectNumber
End If
Next
End If
Player.numbers(j).number = Number.number
Next
Player.name = ("Bill" & i)
Player.numbers_left = 15
players.Add(Player)
Next
End Sub

Related

Using listbox to display an excel file?

So after some changes, I have used this code:
Dim XlApp = New Microsoft.Office.Interop.Excel.Application
Dim oBook As Object = XlApp.Workbooks.Open("C:\file.xlsx")
Dim oSheet As Object = oBook.Worksheets(1)
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim StartedRow As Integer
Dim TotalRows As Integer
TotalRows = XlApp.ActiveWorkbook.Sheets(1).Range("a1").CurrentRegion.Rows.Count
For StartedRow = 1 To TotalRows
Me.ListBox1.Items.Add(oSheet.Cells(StartedRow, 1).text)
Me.ListBox1.Items.Add(oSheet.Cells(StartedRow, 2).text)
Next
MessageBox.Show("Succesful")
This one works but only shows two rows which is not in proper order, I need to show the whole file.
Sorry, since I'm new to Stack Overflow I find it kinda confusing for now :)
You need to declare your variables as specific types so you can use the properties and methods of that type. In general, don't let variables flap about as Object unless absolutely necessary.
I used .UsedRange to get row count. It seemed a bit simpler.
I used an interpolated string, indicated by the $, to get the range. In versions of Visual Studio prior to 2015 you will have to use
String.Format("A{0}", RowNum)
Excel is hard to get rid of. Therefore, put the Excel stuff in a separate method and add the two GC calls when the method returns.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DoExcelThing()
GC.Collect()
GC.WaitForPendingFinalizers()
'Even doing this will still leave a Zoombie Excel in Task Manager when debugging. (Hopefully not release)
MessageBox.Show("Succesful")
End Sub
Private Sub DoExcelThing()
Dim XlApp As New Microsoft.Office.Interop.Excel.Application
Dim oBook As Workbook = XlApp.Workbooks.Open("C:\file.xlsx")
Dim oSheet As Worksheet = CType(oBook.Worksheets(1), Worksheet)
Dim TotalRows = oSheet.UsedRange.Rows.Count
For RowNum = 1 To TotalRows
ListBox1.Items.Add(oSheet.Range($"A{RowNum}").Value)
ListBox1.Items.Add(oSheet.Range($"B{RowNum}").Value)
Next
End Sub
#Wakashio1234
This Will loop through all the cells..
TotalRows = XlApp.ActiveWorkbook.Sheets(1).Range("a1").CurrentRegion.Rows.Count
Totalcolumns = XlApp.ActiveWorkbook.Sheets(1).Range("a1").CurrentRegion.columns.Count
For StartedRow = 1 To TotalRows
For Startedcolumn = 1 To Totalcolumns
Me.ListBox1.Items.Add(oSheet.Cells(StartedRow, Startedcolumn).text)
Next
Next
MessageBox.Show("Successful")
The following code shows how to take out the data in Excel and put it into 'List(Of String())', then bind listbox control to the List and draw aligned columns of data in listbox.
Dim XlApp = New Microsoft.Office.Interop.Excel.Application
Dim oBook As Excel.Workbook = XlApp.Workbooks.Open("your file path")
Dim oSheet As Excel.Worksheet = oBook.Worksheets(1)
Dim lst As List(Of String()) = New List(Of String())()
Private RowHeight, RowWidth As Single
Private ColWidths As Single() = Nothing
Private Const RowMargin As Single = 10
Private Const ColumnMargin As Single = 10
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBox1.DrawMode = DrawMode.OwnerDrawVariable
ListBox1.DataSource = GetList()
End Sub
Private Function GetList() As List(Of String())
Dim rows As Integer = oSheet.UsedRange.Rows.Count
'If you want to display only two columns, set the value of 'cols' to 2
Dim cols As Integer = oSheet.UsedRange.Columns.Count
For r As Integer = 1 To rows
Dim Value As String() = New String(cols - 1) {}
For c As Integer = 1 To cols
Value(c - 1) = oSheet.Cells(r, c).Text
Next
lst.Add(Value)
Next
oBook.Close()
XlApp.Quit()
Return lst
End Function
Private Sub ListBox1_MeasureItem(sender As Object, e As MeasureItemEventArgs) Handles ListBox1.MeasureItem
If ColWidths Is Nothing Then
GetRowColumnSizes(e.Graphics, ListBox1.Font, lst, RowHeight, ColWidths)
For i As Integer = 0 To ColWidths.Length - 1
ColWidths(i) += ColumnMargin
Next
RowHeight += RowMargin
RowWidth = ColWidths.Sum()
End If
e.ItemHeight = CInt(RowHeight)
e.ItemWidth = CInt(RowWidth)
End Sub
Private Sub ListBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ListBox1.DrawItem
Dim values As String() = CType(ListBox1.Items(e.Index), String())
e.DrawBackground()
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
DrawRow(e.Graphics, ListBox1.Font, SystemBrushes.HighlightText, Nothing, e.Bounds.X, e.Bounds.Y, RowHeight, ColWidths, values, False)
Else
DrawRow(e.Graphics, ListBox1.Font, Brushes.Black, Nothing, e.Bounds.X, e.Bounds.Y, RowHeight, ColWidths, values, False)
End If
End Sub
Private Sub GetRowColumnSizes(ByVal gr As Graphics, ByVal font As Font, ByVal values As List(Of String()), ByRef max_height As Single, ByRef col_widths As Single())
Dim num_cols As Integer = values(0).Length
col_widths = New Single(num_cols - 1) {}
max_height = 0
For Each row As String() In values
For col_num As Integer = 0 To num_cols - 1
Dim col_size As SizeF = gr.MeasureString(row(col_num), font)
If col_widths(col_num) < col_size.Width Then col_widths(col_num) = col_size.Width
If max_height < col_size.Height Then max_height = col_size.Height
Next
Next
End Sub
Private Sub DrawRow(ByVal gr As Graphics, ByVal font As Font, ByVal brush As Brush, ByVal box_pen As Pen, ByVal x0 As Single, ByVal y0 As Single, ByVal row_height As Single, ByVal col_widths As Single(), ByVal values As String(), ByVal draw_box As Boolean)
Dim rect As RectangleF = New RectangleF()
rect.Height = row_height
Using sf As StringFormat = New StringFormat()
Dim x As Single = x0
For col_num As Integer = 0 To values.Length - 1
sf.Alignment = StringAlignment.Near
sf.LineAlignment = StringAlignment.Center
rect.X = x
rect.Y = y0
rect.Width = col_widths(col_num)
gr.DrawString(values(col_num), font, brush, rect, sf)
If draw_box Then gr.DrawRectangle(box_pen, rect.X, rect.Y, rect.Width, rect.Height)
x += col_widths(col_num)
Next
End Using
End Sub
Result of my test:
For more details you can see: Make an owner-drawn ListBox that justifies columns in C#

How to count selected items in the listbox by just select and see result in a label?

getting the logic of counting is easy but in practice it gets hard sometimes.
Now I've a list with many items on it. how to count those items if there were repeated and i want to convert that to a number using the FOR loop since i know how many items in the list.
I tried some codes but i did not succeed
'''''''''''''''
' VB 2015
''''''''''''
Public Class Form1
Private Sub lstWinners_List_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWinners_List.SelectedIndexChanged
If lstWinners_List.SelectedIndex <> -1 Then
Dim count As Integer = 0
Dim strselection As String = lstWinners_List.Items(lstWinners_List.SelectedIndex).ToString
For i As Integer = 0 To lstWinners_List.Items.Count - 1
If lstWinners_List.Items(i) = strselection Then
count = count + 1
End If
Next
lblOutput.Text = count.ToString
End If
End Sub
End Class
for EX:
i wanna count the word "michigan " how many times repeated in the list by just clicking on it ?
Here's an example using Jim Hewitt's comment:
Private Sub lstWinners_List_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWinners_List.SelectedIndexChanged
If lstWinners_List.SelectedIndex <> -1 Then
Dim selection As String = lstWinners_List.Items(lstWinners_List.SelectedIndex).ToString
Dim wins As Integer = (From team As String In lstWinners_List.Items Where team.Equals(selection)).Count
lblOutput.Text = wins.ToString
End If
End Sub
Edit
Here's an equivalent, manual, indexed for loop:
Private Sub lstWinners_List_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWinners_List.SelectedIndexChanged
If lstWinners_List.SelectedIndex <> -1 Then
Dim count As Integer = 0
Dim selection As String = lstWinners_List.Items(lstWinners_List.SelectedIndex).ToString
For i As Integer = 0 To lstWinners_List.Items.Count - 1
If lstWinners_List.Items(i) = selection Then
count = count + 1
End If
Next
lblOutput.Text = count.ToString
End If
End Sub
Dim count As Integer = 0
For Each n As String In ListBox1.Items
If n = "red" Then
count += 1
End If
Next
lblOutput.Text(count)
something like this do you mean?

Fibonacci Sequence Visual Basic

I have a quick question about another Visual Basic assignment I'm working on. I have all the code and everything has gone smoothly so far. The app is meant to display the first 100 Fibonacci numbers in a list box, adding the two previously displayed numbers to get the next in a loop. The only problem is that when I hit the button to display the code, the loop continues, and doesn't just stop at 100 numbers. Where did I go wrong?
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim dblA As Double = 0
Dim dblB As Double = 1
Dim dblC As Double
Dim intCounter As Integer
lstSequence.Items.Add(dblA.ToString)
lstSequence.Items.Add(dblB.ToString)
For intCounter = 1 To 100
dblC = dblA + dblB
dblA = dblB
dblB = dblC
lstSequence.Items.Add(dblC.ToString)
Next
End Sub
I just tried this. It works fine.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Integer = 0
Dim b As Integer = 1
Dim fib As Integer
Dim userinput, i As Integer
userinput = InputBox("how many interations?")
i = userinput
ListView1.Items.Add(1)
Do
fib = a + b
a = b
b = fib
ListView1.Items.Add(fib)
i = i + 1
Loop While fib < i
End Sub
End Class

Read each number from array and making sum

I am kinda new at coding.
I am reading some numbers from a txt files and i have them into a array,i managed to make them descending,now i want to make a sum with this numbers,for example:
I will have an input number 1000,on the list i will have numbers from 100 to 1000 and i want to make a sum with them to reach 1000 with minimum rest.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fStream As New System.IO.FileStream("C:\Users\Alex\Desktop\test.txt", IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(fStream)
Dim List As New List(Of Double)
Do While sReader.Peek >= 0
List.Add(sReader.ReadLine)
Loop
Dim i As Long
Dim txt As String
Dim thisArray As Double() = List.ToArray
For i = LBound(thisArray) To UBound(thisArray)
txt = thisArray(i) & vbCrLf
Next i
Array.Sort(thisArray)
For j As Integer = 0 To thisArray.Count - 1
Dim FILE_NAME As String = "C:\Users\Alex\desktop\test2.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objWriter As New System.IO.StreamWriter(FILE_NAME, IO.FileMode.Append)
objWriter.WriteLine(thisArray(j))
objWriter.Close()
Else
MessageBox.Show("File Does Not Exist")
End If
Next
fStream.Close()
sReader.Close()
End Sub
I dont know where to start to do this..input number will be from a textbox.

VB.net multithreading for loop, parallel threads

I have a simple form with 2 RichTextBoxes and 1 button, the code grabs the url address from RichTextBox1 and phrases the page for the title field using regex and appends it to RichTextBox2. I want to multithread everything in such way that none of the url's are skipped and the thread numbers can be set ( according to the system free resources ) For example, let's say 10 threads to run in parallel. I searched everything and the best that I managed to do is run everything in a background worker and keep the GUI from freezing while working. A short code sample will be of much help, I am a beginner in VB.net.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 0 To RichTextBox1.Lines.Length - 1
If RichTextBox1.Lines(i).Contains("http://") Then
Dim html As String = New System.Net.WebClient() _
.DownloadString(RichTextBox1.Lines(i))
Dim pattern As String = "(?<=\<title\>)([^<]+?)(?=\</title\>)"
Dim match As System.Text.RegularExpressions.Match = _
System.Text.RegularExpressions.Regex.Match(html, pattern)
Dim title As String = match.Value
RichTextBox2.AppendText(title & vbCrLf)
End If
Next
End Sub
End Class
Updated code ( throwing "Index was outside the bounds of the array." errors. )
Imports System
Imports System.Threading
Public Class Form1
Public Sub test(ByVal val1 As String, ByVal val2 As String)
Dim zrow As String
zrow = RichTextBox1.Lines(val1)
If zrow.Contains("http://") Then
Dim html As String = New System.Net.WebClient().DownloadString(zrow)
Dim pattern As String = "(?<=\<title\>)([^<]+?)(?=\</title\>)"
Dim match As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(html, pattern)
Dim title As String = match.Value
RichTextBox2.AppendText(val2 & title & vbCrLf)
End If
End Sub
Public Sub lastfor(ByVal number)
Dim start As Integer = number - 100
For x = start To number - 1
Try
test(x, x)
RichTextBox2.AppendText(x & RichTextBox1.Lines(x).Trim & vbCrLf)
Catch ex As Exception
'MsgBox(ex.Message)
RichTextBox3.AppendText(ex.Message & vbCrLf & vbCrLf)
End Try
Next
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Control.CheckForIllegalCrossThreadCalls = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim TotalLines As String = RichTextBox1.Lines.Length - 1
Dim TotalThreads As Integer = 10
Dim LinesPerThread As Integer = TotalLines / TotalThreads
Dim increment As String = LinesPerThread
Dim zdata(TotalThreads) As String
For i = 0 To TotalThreads - 1
zdata(i) = increment
increment = increment + LinesPerThread
Next
Dim lst As New List(Of Threading.Thread)
For Each bump As String In zdata
Dim t As New Threading.Thread(Function(l As String)
'Do something with l
'Update GUI like this:
If bump = String.Empty Or bump Is Nothing Then
Else
lastfor(l)
'MsgBox(l)
End If
End Function)
lst.Add(t)
t.Start(bump)
Next
'test(1)
End Sub
End Class
There are two ways two achieve this:
First, if you are using .NET 4.0, you could use a Parallel.ForEach loop:
Parallel.ForEach(RichTextBox1.Lines, Function(line As String)
' Do something here
' To update the GUI use:
Me.Invoke(Sub()
' Update GUI like this...
End Sub)
Return Nothing
End Function)
The other way is to do this manually (and you will have slightly more control):
Dim lst As New List(Of Threading.Thread)
For Each line In RichTextBox1.Lines
Dim t As New Threading.Thread(Function(l As String)
'Do something with l
'Update GUI like this:
Me.Invoke(Sub()
'Update Gui...
End Sub)
End Function)
lst.Add(t)
t.Start(line)
Next
Both of these are very crude, but will get the job done.
EDIT:
Here is a sample code that will control the number of threads:
Dim lst As New List(Of Threading.Thread)
Dim n As Integer = 1 ' Number of threads.
Dim npl As Integer = RichTextBox1.Lines / n
Dim seg As New List(Of String)
For Each line In RichTextBox1.Lines
For i = npl - n To npl
seg.Add(RichTextBox1.Lines.Item(i))
Next
Dim t As New Threading.Thread(Function(l As String())
For Each lin In l
' TO-DO...
Next
'Do something with l
'Update GUI like this:
Me.Invoke(Sub()
'Update Gui...
End Sub)
End Function)
lst.Add(t)
t.Start(seg.ToArray())
Next
*The above code might have bugs.