i have been trying to display the number of distinct words in a rich text box.i have tried every way but it's still the same.Kindly help.thanks
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim FILE_NAME As String = "C:\Users\User\Desktop\FOR\testit.txt" 'read file from an existing folder in the computer
If System.IO.File.Exists(FILE_NAME) = True Then 'check if file exists
Dim fReader As New System.IO.StreamReader(FILE_NAME)
RichTextBox2.Text = fReader.ReadToEnd 'read the file into the rich text box to the end
fReader.Close()
Else : MsgBox("No such file exists") 'if the file doesnt exist, prompt a msg box that no such file exists
End If
Dim newWords As String
newWords = RichTextBox2.Text
Dim word1 As Array
word1 = newWords.Split(" ")
Dim counter = 0
Dim input As Array = word1
Dim check As New Dictionary(Of String, String)()
For Each p As String In input
If Not check.ContainsKey(p) Then
check.Add(p, 0)
counter = counter + 1 'count distinct words in an array
End If
Label4.Text = counter 'display number of distinct words in label4
check(p) += 1
Next
You could always just do this:
Label4.Text =
RichTextBox2 _
.Text _
.Split(" ") _
.ToLookup(Function(x) x) _
.Count()
Related
I am having an issue, I have a program I'm working on and I have to be able to read multiple .txt files one after the other to update a string array called words(). The main problem is the first file I upload goes into the files()string array, when I run my drag and drop event the array is full and won't let me upload second. how do I reset the files()array after it has been uploaded successfully.
sample code:
Dim words(7) As String
Dim i As Integer = 0
Public Sub frmMain_DragDrop(sender As Object, e As DragEventArgs) Handles MyBase.DragDrop
Dim word As String = ""
Try
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each path In files
MsgBox(path)
Dim sr As New StreamReader(path)
Do Until sr.Peek = -1
word = sr.ReadLine()
words(i) = word
frmDefaultkWh.lbDefaultkWh.Items.Add(cbAppliances.Items(i) + " = " + words(i))
i = i + 1
Loop
Next
GetPower()
Catch ex As Exception
'MessageBox.Show(ex.ToString())
End Try
End Sub
Private Sub frmMain_DragEnter(sender As Object, e As DragEventArgs) Handles MyBase.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
I believe this is because you are not resetting i, which means that eventually i increments out of the range of your words() array which is only size 7, which by the way will also throw an error if one of those files has more than 7 lines.
For Each path In files
i = 0 'reset counter
MsgBox(path)
Dim sr As New StreamReader(path)
Do Until sr.Peek = -1
word = sr.ReadLine()
words(i) = word
frmDefaultkWh.lbDefaultkWh.Items.Add(cbAppliances.Items(i) + " = " + words(i))
i = i + 1
Loop
Next
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.
Hi I am looking for a way to take a text file that has 1 set of numbers per line and make a new text file that has taken the set of numbers and put them into 2 spaced out columns per line. Using Visual Basic 2010 Ex:
My First text file will look similar to this: test1.txt
12345
23456
34567
45678
56789
67890
But I would like to convert it to look like this and save it as a new file: text2.txt
12345 23456
34567 45678
56789 67890
With more spacing in between the set of numbers. I have been struggling with this for about a week now and I have put myself back at square one with it. Thank you all for your time and hope you all have a good day. Joe.
Here is my current code. I am embarrassed to say the least. I am taking an exsisting file and formatting it to have a space between each line of numbers and then reading it into a richtextbox to convert it to a barcode font then saving it. At that point I have no idea how to make the format the way I need it to be putting 2 sets of numbers on one line.
Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnLoad.Click
ListBox1.Items.AddRange(IO.File.ReadAllLines("C:\test\serintest.txt"))
End Sub
Private Sub btnConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnConvert.Click
Dim FILE_NAME As String = "C:\test\serscantemp.rtf"
Dim i As Integer
Dim t As Integer
TextBox1.Text = ListBox1.Items.Count.ToString(t)
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
For i = 0 To TextBox1.Text - 1
'objWriter.WriteLine(aryText(i))
objWriter.WriteLine(ListBox1.Items(i))
objWriter.WriteLine("")
Next
objWriter.Close()
Me.RichTextBox.LoadFile("C:\test\serscantemp.rtf", _
RichTextBoxStreamType.PlainText)
RichTextBox.SaveFile("c:\test\barcodetext.rtf", _
RichTextBoxStreamType.RichText)
End Sub
Joe,
Try this.
I am concatenating the numbers into a string variable and keeping a counter. If the counter = 2 then i write out the concatenated string to RichTextBox and clear counter and string variable
Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
Dim FILE_NAME As String = "C:\temp\serscantemp.rtf"
Dim i As Integer
Dim t As Integer
Dim s As String
Dim c As Integer
TextBox1.Text = ListBox1.Items.Count.ToString(t)
Dim objWriter As New System.IO.StreamWriter(FILE_NAME)
c = 0
s = ""
For i = 0 To TextBox1.Text - 1
c = c + 1
If s > "" Then
s = s & " "
End If
s = s & ListBox1.Items(i)
If c = 2 Then
objWriter.WriteLine(s)
objWriter.WriteLine("")
s = ""
c = 0
End If
'objWriter.WriteLine(aryText(i))
Next
objWriter.Close()
RichTextBox1.LoadFile("C:\temp\serscantemp.rtf", _
RichTextBoxStreamType.PlainText)
RichTextBox1.SaveFile("c:\temp\barcodetext.rtf", _
RichTextBoxStreamType.RichText)
End Sub
I hope this is what you are looking for.
Here's another way to do it that you might also want to try though almost the same as Ed's answer. Just call the routine from btnConvert click event. Also make sure to include Import System.IO
Private Sub Reformat(sourceFile As String, destFile As String)
Dim arr As String() = File.ReadAllLines(sourceFile)
Dim s As String = String.Empty
Dim lst As New List(Of String)()
Dim iCtr As Integer = 0
For i As Integer = 0 To arr.Length - 1
If Not String.IsNullOrWhiteSpace(arr(i)) Then
s += arr(i) & " "
iCtr += 1
If iCtr = 2 Then
lst.Add(s.Trim())
iCtr = 0
s = String.Empty
End If
End If
Next
If Not String.IsNullOrWhiteSpace(s.Trim()) Then
lst.Add(s)
End If
File.WriteAllLines(destFile, lst.ToArray())
End Sub
By the way, this is my first post in SO. Just saying.
How would one select item in listview (first column) that is most similar to string value from e.g. label or textbox.
Listview is populated with this code :
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListView1.Items.Clear()
ListView1.View = System.Windows.Forms.View.Details
ListView1.Columns.Add("COL1", 100, HorizontalAlignment.Left) 'KONTO
ListView1.Columns.Add("COL2", 140, HorizontalAlignment.Left) 'NAZIV
Dim FilePath As String = "W:\GLAVNI\KOR14\"
Dim DBF_File As String = "MATIKGL"
Dim ColName As String = "KONTO"
'Dim naz As String
Using con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & FilePath & _
" ;Extended Properties=dBASE IV")
con.Open()
Using cmd As New OleDbCommand("SELECT * FROM MATIKGL ORDER BY KONTO, NAZIV", con)
Using reader As OleDbDataReader = cmd.ExecuteReader()
If reader.HasRows Then
While (reader.Read())
Me.ListView1.Items.Add(reader("KONTO"))
'ListView1.Items(i).SubItems.Add(rdr.Item("YourColumnName").ToString)
'BELOW SELECTS ALL ITEMS THAT STARTS WITH 2020-
For i = 0 To ListView1.Items.Count - 1
If ListView1.Items(i).ToString.Contains("2020-") Then
Else
ListView1.Items.Remove(ListView1.Items(i))
End If
Next
End While
Else
End If
End Using
End Using
con.Close()
End Using
End Sub
I have one textbox and a button.
Textual input from textbox should be compared with all items in listview and closest should be selected. One more thing : All items are sorted alphabetically
Button code is :
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListView1.MultiSelect = False
ListView1.FullRowSelect = True
Dim checkInt As Integer = FindItem(ListView1, "2020-" & TextBox1.Text)'this one is changed since all items starts with "2020-"& UCASE TEXT
If checkInt <> -1 Then
ListView1.Items(checkInt).Selected = True
ListView1.Focus()
Else
Label1.Text = "Search string not found"
End If
End Sub
UPDATED CODE
Dim checkInt As Integer = FindItem(ListView1, "2020-" & TextBox3.Text)
If checkInt <> -1 Then
TextBox4.Focus()
Else
Label14.Text = "NEMA"
On Error GoTo ext
Dim li As ListViewItem
ListView1.SelectedItems.Clear()
ListView1.HideSelection = False
li = ListView1.FindItemWithText("2020-" & UCase(TextBox3.Text))
If Not (li Is Nothing) Then
Me.ListView1.Focus()
li.Selected = True
li.EnsureVisible()
ElseIf li Is Nothing Then
li = ListView1.FindItemWithText("2020-" & Strings.Left(TextBox3.Text, 1))
Me.ListView1.Focus()
li.Selected = True
li.EnsureVisible()
Else
End If
Exit Sub
ext:
TextBox3.Text = ""
TextBox3.Focus()
Label14.Text = "String not found"
End If
This one works.
I know it's not the best solution but it's working.
Could fixed this without your help, thank you Phillip Trelford
Define a function to score two strings for closeness then use LINQ to find the lowest score, i.,e.
' Example score function
Function Score(a As String, b As String) As Integer
Dim index = 0
While index < a.Length And index < b.Length
Dim diff = Math.Abs(AscW(a(index)) - AscW(b(index)))
If diff <> 0 Then Return diff
index += 1
End While
Return Math.Abs(a.Length - b.Length)
End Function
Function Closest(searchWord As String, words As String()) As String
Dim ordered =
From w In words
Select Word = w, Score = Score(w, searchWord)
Order By Score
Return ordered.First().Word
End Function
Sub Main()
Dim words = {"Alpha", "Apple", "Ask"}
Dim searchWord = "Ann"
Dim word = Closest(searchWord, words)
Console.WriteLine(word)
End Sub
Update
To select the value in a WinForms ListView, you need to do roughly this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListView1.MultiSelect = False
ListView1.FullRowSelect = True
Dim prefix = "2020-"
' Extract items from Listview
Dim items = New List(Of String)()
For Each item In ListView1.Items
items.Add(item)
Next
Dim words = items.ToArray()
Dim searchWord = TextBox1.Text
Dim resultWord = Closest(searchWord, words)
'this one is changed since all items starts with "2020-"& UCASE TEXT
Dim checkInt As Integer = FindItem(ListView1, prefix & resultWord)
If checkInt <> -1 Then
ListView1.Items(checkInt).Selected = True
ListView1.Focus()
Else
Label1.Text = "Search string not found"
End If
End Sub
I want to select a word to another word in a text box in vb.net with everything between them highlighted.
an example is
I went to the beach, had a pinic with my family and then went home at 6 o clock.
The starting word to be had and the end word being home and everything highlighted in between.
I have already used a little bit of code
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim a As String
Dim b As String
a = TextBox2.Text 'means what ever is in textbox2 string to the location where "a" is
b = InStr(RichTextBox1.Text, a)
If b Then
RichTextBox1.Focus()
RichTextBox1.SelectionStart = b - 1
RichTextBox1.SelectionLength = Len(a)
but its not exactly what i want it to do.
Addition to this was using RegEx Function as shown below
'gets rid of the enter line break eg <enter> command no new lines
Dim content As String = Replace(TextBox1.Text, Global.Microsoft.VisualBasic.ChrW(10), Nothing)
'searches for this tag in the brackets between ".*" will be the contents
Dim Regex As New Regex("<div.*class=""answer_text"".*id=editorText"".*""")
'Show the string
For Each M As Match In Regex.Matches(content)
'This will get the values, there are 3 atm meta.name des and content
Dim Description As String = M.Value.Split("""").GetValue(3)
'displays the content in the label
TextBox3.Text = "" & Description
Next
This will select everything between startWord and endWord excluding them both
Dim startWord As String = "had"
Dim endWord As String = "home"
Dim index As Integer = richTextBox1.Text.IndexOf(startWord)
richTextBox1.[Select](index + startWord.Length, richTextBox1.Text.IndexOf(endWord) - index - startWord.Length)
Here's a solution involving two TextBox controls:
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
Dim a As String
Dim b As String
Dim index_a As Integer
Dim index_b As Integer
a = TextBox1.Text
b = TextBox2.Text
index_a = InStr(RichTextBox1.Text, a)
index_b = InStr(RichTextBox1.Text, b)
If index_a And index_b Then
RichTextBox1.Focus()
RichTextBox1.SelectionStart = index_a - 1
RichTextBox1.SelectionLength = (index_b - index_a) + Len(b)
End If
End Sub
TextBox1 contains the first word, TextBox2 contains the second word. When clicking the button, it will highlight from the first word to the end of the second word.