How to insert a numbered bullet list in richtextbox - vb.net

I am trying to insert a numbered bullet list when a user selects the button it will increment the counter
I have searched questions online and different types of loop condition statements
Private Sub ToolStripButton16_Click(sender As Object, e As EventArgs) Handles ToolStripButton16.Click
ToolStripButton16.CheckOnClick = False
Dim i As Integer
i = 0
Dim numbList As String
Dim buttonClickCount As Integer
buttonClickCount = 0
Do While (i = buttonClickCount)
i += 1
numbList = " " & i & "." & vbCrLf
RichTextBox1.AppendText(numbList)
Loop
buttonClickCount += 1
End Sub
Expected result:
1.
2.
3.
4.

Probably not the most elegant solution but it should put you in the right direction.
'Using a class variable here allows you to keep an "application state".
Dim buttonClickCount as Integer = 0
Private Sub ToolStripButton16_Click(sender As Object, e As EventArgs) Handles ToolStripButton16.Click
ToolStripButton16.CheckOnClick = False
buttonClickCount += 1
Dim newValue As String = " " & buttonClickCount & "." & vbCrLf
RichTextBox1.AppendText(newValue)
End Sub

Related

Making a random array of numbers, with each number being unique

I'd like for a string of 24 numbers to appear in the text box, and then for each to have a random number from 0 to 23.
so far I have the following code made, but it makes a duplicate number appear occasionally.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Randomize()
Dim Output As String = String.Empty
Dim NumberArray(24) As Integer
For n As Integer = 0 To 23
Dim RandomNumber As Integer = CInt(Int(Rnd() * 25) + 1)
NumberArray(n) = RandomNumber
Output &= "Index #" & n.ToString & Space(4) & NumberArray(n) & vbCrLf
Next
TextBox1.Text = Output
End Sub
End Class
any help greatly appreciated!

Sync Two Textbox Lines VB.Net

How do I sync 2 Textboxes? I mean, if I randomize the first Textbox (Randomize Text Lines) how do the 2nd Textbox be synchronized after the first Textbox?
I also want the 4 Textboxes containing the items to be saved in (Answer.dat) for example if in the first Textbox I have the element (BlackJack) in the 2nd Textbox element (21) in the 3rd Textbox the Poker element and the fourth Textbox element Bingo.
I want to save this in the new line (in my text file) to be something like the model (Blank Empty + Word(Textbox3) + Space + Word(Textbox4) + Space + Word(Textbox5) + Space + Word(Textbox6) this is the Screenshot how the items want to be saved. Unfortunately, I'm not doing too well with the blank at first.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = System.IO.File.ReadAllText(My.Application.Info.DirectoryPath + ("\Data\Question.dat"))
TextBox2.Text = System.IO.File.ReadAllText(My.Application.Info.DirectoryPath + ("\Data\Answer.dat"))
End Sub
End Class
So how can I do to save in a new line of Textbox (Save to my text file) the question and the answers in the textbox? following the example given?
The code below will keep the rows synchronized on a random shuffle. If you don't want repeated rows, you will have to code validation to throw-out draws that have already occurred.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim text1 As String
Dim text2 As String
Dim textarray1 As New ArrayList
Dim textarray2 As New ArrayList
Dim NextMember As String = ""
Dim Rand As New Random
Dim RandNum As Integer = 0
TextBox1.Clear()
TextBox2.Clear()
text1 = "one" & vbCrLf & "two" & vbCrLf & "three" & vbCrLf
text2 = "A" & vbCrLf & "B" & vbCrLf & "C" & vbCrLf
For i = 1 To Len(text1)
Do Until Mid(text1, i, 1) = vbCr
NextMember = NextMember & Mid(text1, i, 1)
i = i + 1
Loop
textarray1.Add(NextMember)
i = i + 1
NextMember = ""
Next
For i = 1 To Len(text2)
Do Until Mid(text2, i, 1) = vbCr
NextMember = NextMember & Mid(text2, i, 1)
i = i + 1
Loop
textarray2.Add(NextMember)
i = i + 1
NextMember = ""
Next
For i = 0 To textarray1.Count - 1
RandNum = Rand.Next(textarray1.Count)
TextBox1.Text = TextBox1.Text & textarray1(RandNum) & vbCrLf
TextBox2.Text = TextBox2.Text & textarray2(RandNum) & vbCrLf
Next
End Sub

need histogram from these results VB.net

I need some help with the histogram. My program counted the words with three, four, five and six letters. Now I need to make simple histogram from the received answers. This is my code:
Public Class Form1
Dim tekst As String
Dim rijec() As String
Dim trazena As String
Dim brojac1 As Integer = 1
Dim brojac2 As Integer = 0
Dim brojac3 As Integer = 0
Private Sub btnUcitaj_Click(sender As Object, e As EventArgs) Handles btnUcitaj.Click
If OpenFileDialog1.ShowDialog = DialogResult.OK Then
RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(OpenFileDialog1.FileName)
End If
End Sub
Private Sub btnPrebroj1_Click(sender As Object, e As EventArgs) Handles btnPrebroj1.Click
tekst = RichTextBox1.Text
rijec = tekst.Split(CChar(" "))
Dim count = From x In rijec Where x.Length = 3
txtTri.Text = ("Number of words: " & count.Count.ToString())
End Sub
Private Sub btnPrebroj2_Click(sender As Object, e As EventArgs) Handles btnPrebroj2.Click
tekst = RichTextBox1.Text
rijec = tekst.Split(CChar(" "))
Dim count = From x In rijec Where x.Length = 4
txtCetiri.Text = ("Number of words: " & count.Count.ToString())
End Sub
Private Sub btnPrebroj3_Click(sender As Object, e As EventArgs) Handles btnPrebroj3.Click
tekst = RichTextBox1.Text
rijec = tekst.Split(CChar(" "))
Dim count = From x In rijec Where x.Length = 5
txtPet.Text = ("Number of words: " & count.Count.ToString())
End Sub
Private Sub btnPrebroj4_Click(sender As Object, e As EventArgs) Handles btnPrebroj4.Click
tekst = RichTextBox1.Text
rijec = tekst.Split(CChar(" "))
Dim count = From x In rijec Where x.Length = 6
txtSest.Text = ("Number of words: " & count.Count.ToString())
End Sub
i need histogram from these results from picture.
Visual Basic actually has a control called a chart which is nice for this kind of situation. I found a tutorial here if you would like to check it out.
I wrote up this function that should work with your code, all you need to do is add a data chart and call it Chart1 and call this function.
Private Sub ShowData()
Chart1.Series.Clear() ' Delete the default data series.
Dim tekst = RichTextBox1.Text
Dim rijec = tekst.Split(CChar(" "))
With Chart1.Series.Add("Word Lengths") ' Add a new series called Word Lengths
For wordLength As Integer = 3 To 6 ' Check every word length from 3 to 6
Dim count = From x In rijec Where x.Length = wordLength ' Count how many words there are
.Points.AddXY(wordLength, count.Count) ' Add a new data point
Next
End With
End Sub
Good luck, I hope this helped!
~Nic

How would I insert items from textbox into listview?

I have a listview with two columns. I also have a textbox. In the textbox, there are lines with strings that I want to insert into the listview.
Every 1st line will be inserted into the first column and every 2nd line will be inserted into the second column. How would I achieve this.
This is my current code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox3.Text = My.Resources.clsid
Dim ListArr(230) As String
Dim i As Integer = 0
For Each line As String In TextBox3.Lines
ListArr(i) = line(i)
i += 1
For Each litem As String In ListArr
AddLitem(litem, litem)
Next
Next
End Sub
Public Function AddLitem(ByVal Desc As String, ByVal CLSID As String)
Dim litem As ListViewItem
If i = 0 Then
Lstr(0) = Desc
i += 1
ElseIf i = 1 Then
Lstr(1) = Desc
litem = New ListViewItem(Lstr)
ListView1.Items.Add(litem)
ListView1.Items.RemoveAt(ListView1.Items.Count)
End If
Lstr(0) = Desc
'Lstr(1) = CLSID
End Function
Note:
You can us Math.Mod to check if you have to handle the first or second row.
For example
0 mod 2 result 0
1 mod 2 result 1
2 mod 2 result 0
3 mod 2 result 1
I use ListView1.Clear to make sure, i'm refilling an empty ListView.
Example Code. This copies all lines from the TextBox1 to the ListView1.
Separeted by a pipe |.
Delete the IIf(String.IsNullOrEmpty(strView2), "", "|") & if you don't want this separation.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strView1 As String = ""
Dim strView2 As String = ""
For i As Integer = 0 To TextBox1.Lines.Count - 1
If (i Mod 2) Then
strView2 &= IIf(String.IsNullOrEmpty(strView2), "", "|") & TextBox1.Lines(i)
Else
strView1 &= IIf(String.IsNullOrEmpty(strView1), "", "|") & TextBox1.Lines(i)
End If
Next
ListView1.Clear()
ListView1.Items.Add(strView1)
ListView1.Items.Add(strView2)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "cat" & vbCrLf & "street" & vbCrLf & "dog" & vbCrLf & "house" & vbCrLf & "bird" & vbCrLf & "garden"
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