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

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?

Related

How to use SQL IN statement with list of Items selected from treeviewlist?

How can I join the accid string values from the For Next loop (below) into one single string?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim accid As String
Dim iLast As Integer
iLast = trv.Nodes.Count
Dim p As Integer = 0
For p = 0 To iLast - 1
If TrV.Nodes(p).Checked = True Then
accid = Strings.Left(TrV.Nodes(p).Text, 9)
MsgBox(accid)
End If
Next
End Sub
This gives me a separate output of string "accid"
I want this output: "accid1,accid2,accid3"
Thanks for supporting!
You need to build your string inside the loop, and then do the MsgBox outside the loop. Something like this should work:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim accid As String
Dim iLast As Integer
iLast = trv.Nodes.Count
Dim p As Integer = 0
For p = 0 To iLast - 1
If TrV.Nodes(p).Checked = True Then
accid = accid & Strings.Left(TrV.Nodes(p).Text, 9) & "," 'notice the change here
End If
Next
accid = accid.Remove(accid.Length - 1, 1)
MsgBox(accid)
End Sub
This could be done with very few lines of code using Linq and String.Join.
Dim texts = TrV.Nodes.Cast(Of TreeNode)().
Where(Function(n) n.Checked).
Select(Function(c) Strings.Left(c.Text,9))
Dim result = String.Join(",", texts)

How to enter additional data entered in text files in vb.net?

I'm currently working on a project where new orders are added to text files and the details of these orders must be displayed in text boxes.
I managed to get the application for 'Order 1' but I am unsure as to how to display details for orders that have not yet been added to the text files e.g. Order 20.
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim query = IO.File.ReadAllLines("Order1.txt")
Dim name = query(0).Split(","c)(0)
Dim address = query(0).Split(","c)(1)
Dim phonenum = query(0).Split(","c)(2)
Dim method = query(0).Split(","c)(3)
txtName.Text = name
txtAddress.Text = address
txtPhoneNum.Text = phonenum
txtMethod.Text = method
End Sub
I do not know if I understand correctly what you want.
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim yourModelLists As New List(yourModel)
Do
Dim query = IO.File.ReadLine("Order1.txt")
Dim yourModelItem As yourModel
If Not (String.IsNullOrEmpty(query) Or String.IsNullOrWhiteSpace(query) Then
Dim column As Integer = 0
Dim items As String() = query.Split(";")
For Each item In items
If Not (String.IsNullOrEmpty(item) Or String.IsNullOrWhiteSpace(item)) Then
Select Case column
Case 0
yourModelItem.name = item
Case 1
yourModelItem.address = item
Case 2
yourModelItem.phonenum = item
Case 3
yourModelItem.method = item
Case Else
'Do something
End Select
End If
Next
End If
yourModelLists.Add(yourModelItem)
Loop Until (String.IsNullOrEmpty(query) Or String.IsNullOrWhiteSpace(query))
End Sub
All you need is in yourModelLists.

Need help comparing a string to a listbox with a loop

Code is below. I'm trying to compare a user input string with a large listbox and if it finds a match, it terminates the loop and prints a response. I keep getting hung up with it freezing or printing the wrong response.
Private Sub btnAction_Click(sender As Object, e As EventArgs) Handles btnAction.Click
Dim input As String = txtIn.Text
Dim i As Integer = 0
While i <= lstRoseBowl.Items.Count - 1
If input = CStr(lstBox.Items(i)) Then
txtOut.Text = "Yes"
Else
txtOut.Text = "No"
End If
End While
End Sub
You need to increment "i"
Private Sub btnAction_Click(sender As Object, e As EventArgs) Handles btnAction.Click
Dim input As String = txtIn.Text
Dim i As Integer = 0
While i <= lstRoseBowl.Items.Count - 1
If input = CStr(lstBox.Items(i)) Then
txtOut.Text = "Yes"
Else
txtOut.Text = "No"
End If
i += 1 ' <-----
End While
End Sub
Or better yet, use a for loop
Private Sub btnAction_Click(sender As Object, e As EventArgs) Handles btnAction.Click
Dim input As String = txtIn.Text
For i As Integer = 0 To lstRoseBowl.Items.Count - 1
If input = CStr(lstBox.Items(i)) Then
txtOut.Text = "Yes"
Else
txtOut.Text = "No"
End If
Next
End Sub
Now, this will compile and run but might not give you the result you want since if it find the items on the first try, it would then display No on the second try. There are better ways of doing it but for the least amount of code change, it could look like this.
Private Sub btnAction_Click(sender As Object, e As EventArgs) Handles btnAction.Click
Dim input As String = txtIn.Text
txtOut.Text = "No"
For i As Integer = 0 To lstRoseBowl.Items.Count - 1
If input = CStr(lstBox.Items(i)) Then
txtOut.Text = "Yes"
Exit For
End If
Next
End Sub

Score not being calculated correctly

Hi I'm created a program for a project and I've now started running some tests and the score for the user isn't being calculated correctly and I believe that it can't compare the answer given to the correct answer. I'm very confused and need any help that can be given. My code looks like this, any confusing parts and I'll try and explain.
Imports System.IO
Public Class QuestionScreen
Dim score As Integer = 0
Dim count As Integer
Dim Difficulty_ext As String
Dim questions, answers As New List(Of String)()
Private i As Integer
Sub ReadFile()
If Main.Diff_DDown.Text = "Easy" Then
Difficulty_ext = "questions - Easy"
ElseIf Main.Diff_DDown.Text = "Medium" Then
Difficulty_ext = "questions - Medium"
Else
Difficulty_ext = "questions - Difficult"
End If
Randomize()
Dim countline = File.ReadAllLines("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt").Length
Dim numline As Integer
Dim values() As String
Using sr As New StreamReader("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt")
While Not sr.EndOfStream
values = sr.ReadLine().Split(","c)
questions.Add(values(0))
answers.Add(values(1))
End While
End Using
numline = Int(Rnd() * countline)
For i As Integer = 0 To numline
Question.Text = questions(i)
Act_ANS.Text = answers(i)
Next
End Sub
Private Sub Pass_Btn_Click(sender As Object, e As EventArgs) Handles Pass_Btn.Click
If count < 10 Then
Call ReadFile()
count = count + 1
Ans_TxtBx.Text = ""
Hid_Score.Text = score
Else
ResultsScreen.Show()
Me.Hide()
End If
End Sub
Public Sub Submit_Btn_Click(sender As Object, e As EventArgs) Handles Submit_Btn.Click
If count < 10 Then
Call ReadFile()
count = count + 1
If Ans_TxtBx.Text = answers(i) Then
score = score + 1
End If
Hid_Score.Text = score
Else
ResultsScreen.Show()
Me.Hide()
count = 0
End If
Ans_TxtBx.Text = ""
End Sub
Private Sub QuestionScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call ReadFile()
End Sub
End Class
OK..... Try this, I have debugged your code trying to leave it pretty much as you had it, there were a number of problems, nothing major just a few lines of code in the wrong place....
Imports System.IO
Public Class Form1
Dim score As Integer = 0
Dim count As Integer
Dim Difficulty_ext As String
Dim questions, answers As New List(Of String)()
Private i As Integer
Sub ReadFile()
If Diff_DDown.Text = "Easy" Then
Difficulty_ext = "questions - Easy"
ElseIf Diff_DDown.Text = "Medium" Then
Difficulty_ext = "questions - Medium"
Else
Difficulty_ext = "questions - Difficult"
End If
Randomize()
Try
Dim countline = File.ReadAllLines("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt").Length
Dim numline As Integer
Dim values() As String
' clear the list of questions and answers
answers.Clear()
questions.Clear()
'''''''''''''''''''''''''''''''''''''''''
Using sr As New StreamReader("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt")
While Not sr.EndOfStream
values = sr.ReadLine().Split(","c)
questions.Add(values(0))
answers.Add(values(1))
End While
End Using
numline = Int(Rnd() * countline)
For i = 0 To numline
Question.Text = questions(i)
Act_ANS.Text = answers(i)
Next
Catch ex As Exception
End Try
End Sub
Private Sub Pass_Btn_Click(sender As Object, e As EventArgs) Handles Pass_Btn.Click
If count < 10 Then
count = count + 1
Ans_TxtBx.Text = ""
Hid_Score.Text = score
Else
ResultsScreen.Show()
Me.Hide()
End If
Call ReadFile() ' move this to the bottom
End Sub
Public Sub Submit_Btn_Click(sender As Object, e As EventArgs) Handles Submit_Btn.Click
If count < 10 Then
count = count + 1
If Ans_TxtBx.Text = answers(i - 1) Then ' need to subtract 1 here
score = score + 1
End If
Hid_Score.Text = score
Else
ResultsScreen.Show()
Me.Hide()
count = 0
End If
Ans_TxtBx.Text = ""
Call ReadFile() ' move this to the bottom
End Sub
Private Sub QuestionScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call ReadFile()
End Sub
End Class

How to make a program that would detect the same characters within two strings

so i made this but when i enter a string it would only detect one character
and it wont convert the entered string to lower case too
Dim readme, readme2 As String
Dim j, i As Integer
Dim Compare As Integer
readme = TextBox1.Text
readme2 = TextBox2.Text
readme.ToLower.Substring(i, readme.Length)
readme2.ToLower.Substring(j, readme2.Length)
For i = 0 To readme.Length
For j = 0 To readme2.Length
If readme = readme2 Then
Compare = +1
End If
Next
Next
Label4.Text = Compare`enter code here`
Strings are immutable. You cannot apply a method to a string and expects that string to change in response to the inner operations of that method.
You need to reassign the result of the operation to the same string that you have used to call the method
readme = readme.ToLower()
readme2 = readme2.ToLower()
The second part of your question is more confused, are you trying to count the number of equal chars in the same position?
In that case your loop should be
Dim maxLenToCheck = Math.Min(readme.Length, readme2.Length)
For i = 0 To maxLenToCheck - 1
If readme(i) = readme2(i) Then
Compare += 1
End If
Next
In that loop you set always the Compare to 1, the correct syntax to increment the Compare variable is
Compare += 1
Following your comment below, then I presume that your loop should be written as
Dim Compare = 0
For i = 0 To readme.Length - 1
for j = 0 to readme2.Length -1
If readme(i) = readme2(j) AndAlso _
Not Char.IsWhiteSpace(readme(i)) Then
Compare += 1
End If
Next
Next
Based on the comments in the solution by Steve, the author wants to know the count of letters occurring in both strings, ignoring case and white spaces.
By my count, however, the solution should be 21, not 20. Here is a solution using LINQ that also gives visual feedback on where those letters are located:
Public Class Form1
Private Class LetterCount
Public Letter As Char
Public Count As Integer
Public Overrides Function ToString() As String
Return Letter & " : " & Count
End Function
End Class
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "This is a Test"
TextBox2.Text = "This should be tryed before"
RichTextBox1.ReadOnly = True
RichTextBox1.Font = New Font("MS Courier", 14) ' monospaced font
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Text = TextBox1.Text & vbCrLf & TextBox2.Text & vbCrLf
Dim charsA As New List(Of Char)(TextBox1.Text.ToLower.ToCharArray.Where(Function(x) Not Char.IsWhiteSpace(x)))
Dim charsB As New List(Of Char)(TextBox2.Text.ToLower.ToCharArray.Where(Function(x) Not Char.IsWhiteSpace(x)))
Dim DistinctCommonLetters = (From A In charsA, B In charsB Where A = B Select A).Distinct
Dim DistinctCounts =
From Letter In DistinctCommonLetters, C In charsA.Concat(charsB)
Where C = Letter
Group By Letter Into Group
Select New LetterCount With {.Letter = Letter, .Count = Group.Count}
Dim TotalMatches = DistinctCounts.Sum(Function(x) x.Count)
ListBox1.DataSource = DistinctCounts.ToList
Label1.Text = "TotalMatches: " & TotalMatches
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim LC As LetterCount = ListBox1.SelectedItem
RichTextBox1.SelectAll()
RichTextBox1.SelectionColor = Color.Black
Dim index As Integer = RichTextBox1.Find(LC.Letter, 0, RichTextBoxFinds.None)
While index <> -1
RichTextBox1.Select(index, 1)
RichTextBox1.SelectionColor = Color.Red
index = RichTextBox1.Find(LC.Letter, index + 1, RichTextBoxFinds.None)
End While
End Sub
End Class