search and count occurances of a word in a textbox string - vb.net

Right now I have two textboxes. One is the input and the other the text string. It functions perfectly. Is there a way to get rid of the input box and have the "searched" word in the code so when I hit a button it gives the result.
Private Function FindWords(ByVal TextSearched As String, ByVal Paragraph As String) As Integer
Dim location As Integer = 0
Dim occurances As Integer = 0
Do
location = TextSearched.IndexOf(Paragraph, location)
If location <> -1 Then
occurances += 1
location += Paragraph.Length
End If
Loop Until location = -1
Return occurances
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = TextBox2.Text ' display result
Label2.Text = FindWords(TextBox1.Text, TextBox2.Text)
' MsgBox("The word " & TextBox2.Text & " has occured " & FindWords(TextBox1.Text, TextBox2.Text) & " times!!")
End Sub

There are a couple of small problems that you'll might see of you turn on option strict in your project's compile settings. Your function returns an integer. To properly assign it to a textbox label, you should really add .ToString to the end of the assignment like this
Label2.Text = FindWords(TextBox1.Text, TextBox2.Text).ToString
So leaving you code as you wrote it, in this sub, you just want to pass a string as the parameter for the text to find?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = TextBox2.Text ' display result
Label2.Text = FindWords(TextBox1.Text, TextBox2.Text)
' MsgBox("The word " & TextBox2.Text & " has occured " & FindWords(TextBox1.Text, TextBox2.Text) & " times!!")
End Sub
Just change it to this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim searchString as string="Hello" 'or whatever string you want to use
Label1.Text = TextBox2.Text ' display result
Label2.Text = FindWords(searchString, TextBox2.Text)
' MsgBox("The word " & TextBox2.Text & " has occured " & FindWords(TextBox1.Text, TextBox2.Text) & " times!!")
End Sub
but it might be better to change it a bit more so that your function only gets executed once if you want to display a messagebox with the number of occurences - like this
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
dim searchCount as Integer
dim searchString as string="Hello" 'or whatever string you want to use
Label1.Text = TextBox2.Text ' display result
searchCount = FindWords(searchString, TextBox2.Text)
Label2.Text = searchcount.ToString
' MsgBox("The word " & TextBox2.Text & " has occured " & searchcount.Tostring & " times!!")
End Sub

Related

how to change events in textbox binding source from contain to exact in vb.net

how to change events in textbox from contain to exact in vb.net?.
note : I use visual studio 2010
thanks
jack
Dim source1 As New BindingSource()
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text = "" Then
source1.Filter = ""
Me.DataGridView1.Refresh()
Else
source1.Filter = "CODE like '%' + '" & TextBox1.Text & "' + '%' "
Me.DataGridView1.Refresh()
End If
End Sub

Saving multiple items in a ComboBox control in VB.NET

I'm working on a student grading system and I want a suggestion to save all the data that were entered in the TextBox Controls into a text file, but I'm facing a problem. In my program the student can select multiple courses from the ComboBox Control and enter a grade for each one of them.
How can I save all the selected items from the ComboBox control with the grades entered for the same student?
For example: A student enters his name Adam K., and Adam K. selected six courses and put grades for each one of them.
How can I save all these pieces of information in order to be displayed like the following?
Adam K. , history 98/100, math 56/100, geography 78/100 and so on.
Since the information you describe is not very clear, I can only try to provide you with a solution based on the information you provide.
The effect is as follows:
In test.txt file:
You can try my method if you want this effect.
Public Class Form1
Dim name As String
Dim info As New Dictionary(Of String, String)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim course As String() = {"History", "Math", "English", "Chinese", "Science", "Biology"}
For Each item As String In course
ComboBox1.Items.Add(item)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ComboBox1.Text IsNot "" Then
If TextBox2.Text Is "" Then
Try
info.Add(ComboBox1.Text, "0 / 100") 'score default
Catch
MsgBox("The course " & ComboBox1.Text & " already exists.")
End Try
Else
Try
info.Add(ComboBox1.Text, TextBox2.Text & "/100")
Catch
MsgBox("The course " & ComboBox1.Text & " already exists.")
End Try
End If
Else
MsgBox("Please enter course info")
End If
ComboBox1.Text = "" 'clear course info
TextBox2.Text = ""
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
name = TextBox1.Text
TextBox3.Text &= name & " "
For Each kvp As KeyValuePair(Of String, String) In info
TextBox3.Text &= kvp.Key.ToString & ":" & kvp.Value.ToString & " "
Next kvp
TextBox3.Text &= vbCrLf
TextBox1.Text = ""
info.Clear() 'clear all info
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim file As System.IO.StreamWriter 'Write Text to Files with a StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("D:\test.txt", True)
file.WriteLine(TextBox3.Text)
file.Close()
TextBox3.Text = "" 'Append to Text Files in Visual Basic
'Dim inputString As String = "This is a test string."
'My.Computer.FileSystem.WriteAllText("D:\test.txt", inputString, True)
End Sub
End Class

How to validate a null value in a maskedtextbox phone number input mask

I want to check for null values. using this code below. i am still getting values in the textbox. The values i am getting in the textbox are "( ) -"...
If Text_Phone.Text IsNot "" Then
If BuildSqlFlag = True Then
BuildSql = BuildSql & " AND " & "Phone = " & Text_Phone.Text
Else
BuildSql = "Phone = " & Text_Phone.Text
End If
BuildSqlFlag = True
End If
i am not exactly sure what is needed from my code to be changed to i even tried the following:
If Text_Phone.Text IsNot "( ) -" Then
But that was no help.
Set the TextMaskFormat to exclude prompt and literals.
Text_Phone.TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
MaskFormat Enumeration
Then when you do Text_Phone.Text it will be equal to "" if it's empty.
'Validate phone number in this format: 999-999-9999
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim phoneNumber As New Regex("\d{3}-\d{3}-\d{4}")
If phoneNumber.IsMatch(TextBox1.Text) Then
TextBox2.Text = "Valid phone number"
Else
TextBox2.Text = "Not Valid phone number"
End If
End Sub
End Class
'Validate phone number in this format (999)999-9999
Private Sub Button1_Click_1(sender As System.Object, _
e As System.EventArgs) Handles Button1.Click
Dim phoneNumber As New Regex("\(\d{3}\)\d{3}-\d{4}")
If phoneNumber.IsMatch(TextBox1.Text) Then
TextBox2.Text = "Valid phone number"
Else
TextBox2.Text = "Not Valid phone number"
End If
End Sub

How to get a information from a listbox to textboxes with radio buttons

I have been trying to figure out what I am doing wrong. I have two radio buttons one for a new user a the other for an existing user. The new user radio button takes the information input into the textboxes and adds to the list box. The existing user button is supposed to take the line selected in the listbox and break it up then put it back into my textboxes. I have written code for the radio buttons but they are not working properly. How can I get the information to process correctly and show in the textboxes? I have tried a split and a function but this was unsuccessful. Can anyone help? This is what I have for the radio buttons so far I can post the rest of the code if you need me to.
Private Sub rbtnNew_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtnNew.CheckedChanged
lstCustomer.Items.Add(RevName(txtName.Text.ToUpper) & " , " & (txtAddress.Text.ToUpper) & " , " & (txtCity.Text.ToUpper))
End Sub
Private Sub rbtnExisting_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtnExisting.CheckedChanged
Dim name1 As String
Dim address2 As String
Dim city2 As String
Dim output() As String
name1 = txtName.Text
address2 = txtAddress.Text
city2 = txtCity.Text
output = Split(lstCustomer.SelectedItem.ToString(), ",")
txtName.Text = output(0)
txtAddress.Text = output(1)
txtCity.Text = output(2)
lstResults.Items.Clear()
txtChairs.Clear()
txtSofas.Clear()
End Sub
End Class
In this case, the radiobuttons are very difficult to manage when they have the AutoCheck property set to True. Anyway I give you a couple of possible solutions:
Solution #1 (AutoCheck property set to True):
Private Sub rbtnNew_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbtnNew.CheckedChanged
Static wasInvoked As Boolean
If Not wasInvoked Then
If rbtnNew.Checked Then
lstCustomer.Items.Add(RevName(txtName.Text.ToUpper) & " , " & (txtAddress.Text.ToUpper) & " , " & (txtCity.Text.ToUpper))
Else
If lstCustomer.SelectedItem Is Nothing Then
wasInvoked = True
rbtnNew.Checked = True
wasInvoked = False
MessageBox.Show("Please select a customer from the list.")
End If
End If
End If
End Sub
Private Sub rbtnExisting_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles rbtnExisting.CheckedChanged
If rbtnExisting.Checked Then
Dim name1 As String
Dim address2 As String
Dim city2 As String
Dim output() As String
name1 = txtName.Text
address2 = txtAddress.Text
city2 = txtCity.Text
output = Split(lstCustomer.SelectedItem.ToString(), ",")
txtName.Text = output(0)
txtAddress.Text = output(1)
txtCity.Text = output(2)
lstResults.Items.Clear()
txtChairs.Clear()
txtSofas.Clear()
End If
End Sub
Solution #2 (RadioButtons's AutoCheck property set to False):
Private Sub rbtnNew_CheckedChanged(sender As Object, e As System.EventArgs) Handles rbtnNew.CheckedChanged
rbtnExisting.Checked = Not rbtnNew.Checked
End Sub
Private Sub rbtnExisting_CheckedChanged(sender As Object, e As System.EventArgs) Handles rbtnExisting.CheckedChanged
rbtnNew.Checked = Not rbtnExisting.Checked
End Sub
Private Sub rbtnNew_Click(sender As Object, e As System.EventArgs) Handles rbtnNew.Click
If Not rbtnNew.Checked Then
rbtnNew.Checked = True
lstCustomer.Items.Add(RevName(txtName.Text.ToUpper) & " , " & (txtAddress.Text.ToUpper) & " , " & (txtCity.Text.ToUpper))
End If
End Sub
Private Sub rbtnExisting_Click(sender As Object, e As System.EventArgs) Handles rbtnExisting.Click
If Not rbtnExisting.Checked Then
If lstCustomer.SelectedItem Is Nothing Then
MessageBox.Show("Please select a customer from the list.")
Else
rbtnExisting.Checked = True
Dim name1 As String
Dim address2 As String
Dim city2 As String
Dim output() As String
name1 = txtName.Text
address2 = txtAddress.Text
city2 = txtCity.Text
output = Split(lstCustomer.SelectedItem.ToString(), ",")
txtName.Text = output(0)
txtAddress.Text = output(1)
txtCity.Text = output(2)
lstResults.Items.Clear()
txtChairs.Clear()
txtSofas.Clear()
End If
End If
The radiobutton checkedchanged event may be trigger either it is checked or unchecked. Make sure check the value/attribution of the radiobutton in the event function.
Edit: Do you have the error-checking code for the case that listbox has no items at all? Since no item is seleced in listbox will cause ListBox.SelectedItem return null or something which the Split() function can not handle.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditem%28v=vs.110%29.aspx
In addition, using the string manipulation function provided by the system, such as join(), concat().
http://msdn.microsoft.com/en-us/library/aa903372%28v=vs.71%29.aspx
Check the size/length of the resulting string array from split().

When appending data to RichTextBox from a TextBox1_TextChanged field, part of my code auto-adds a redudant date. Something wrong with the logic

What I'm trying to do is to automatically add whatever is scanned to TextBox1 field to RichTextBox1 and then clear the TextBox1 field, but when TextBox1 is cleared, RichTextBox1.AppendText(scanData + " " + currentTime + vbLf) takes action and adds a space + TimeOfDay. So the outputs to RichTextBox1 are coming like this:
011546 1:30 PM
1:30 PM
011879 1:31 PM
1:31 PM
How do I get rid of the redundant secondary time that is inserted in the new line? Here is my current code:
Public Class Form1
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim scanData As String = TextBox1.Text
Dim currentTime As String = TimeOfDay
TextBox1.Text = scanData
RichTextBox1.AppendText(scanData + " " + currentTime + vbLf)
TextBox1.Clear()
End Sub
Any help is really appreciated.
You should use an if statement like this in order to avoid the RichTextBox1.AppendText(scanData + " " + currentTime + vbLf) takes place when TextBox1 is cleared.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Dim scanData As String = TextBox1.Text
Dim currentTime As String = TimeOfDay
TextBox1.Text = scanData
If (TextBox1.Text <> "") Then RichTextBox1.AppendText(scanData + " " + currentTime + vbLf)
TextBox1.Clear()
End Sub
Excuse any gramatical mistakes.