How would I insert items from textbox into listview? - vb.net

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

Related

Sorting Numbers in Textbox in VB.Net

I need help with my program I want my output box to be in ascending order like:
0
1
2
3
4
5
6 and so on
but my output is like this 1234567890 and so on.
Here's my code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim count As Integer
If Integer.TryParse(TextBox1.Text, count) Then
Dim strNumbers As String = ""
For x As Integer = 0 To count - 1
strNumbers &= x
Next
TextBox2.Text = strNumbers
End If
End Sub
End Class
I believe I may have solved your problem :)
If you change textbox2 into a rich textbox, it will now allow you to output multiple lines
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim count As Integer
If Integer.TryParse(TextBox1.Text, count) Then
Dim strNumbers As String = ""
For x As Integer = 0 To count - 1
strNumbers &= x & vbNewLine & vbNewLine
Next
RichTextBox1.Text = strNumbers
End If
End Sub
Here is a little something I threw together to test the output

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

How to insert a numbered bullet list in richtextbox

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

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

How to display result in GridView?

NET WinForms.
VB code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Beginning"
Dim a As Integer = 20
Dim b As Integer = 3
Do Until b > a
a & " " & b
a = a - 2
b = b + 1
Loop
Label2.Text = "End"
End Sub
I want to display the result of this row a & " " & b in GridView.
How should I change the code to make this work properly?
I would recommend you store the value into DataTable and bind into the DataGridView
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Beginning"
'Create a new datatable here
Dim dt As New DataTable
dt.Columns.Add("Result")
Dim a As Integer = 20
Dim b As Integer = 3
Do Until b > a
'Create DataRow here and put the value into DataRow
Dim dr As DataRow = dt.NewRow
dr("result") = a.ToString & " " & b.ToString
'a & " " & b
dt.Rows.Add(dr)
a = a - 2
b = b + 1
Loop
'Bind your dt into the GridView
DataGridView.DataSource = dt
Label2.Text = "End"
End Sub
Add DataGridView to your form, and add 2 columns, then next updated code will do that
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label1.Text = "Beginning"
' If the DataGridView is not bound to any data source, this code will clear content
DataGridView1.Rows.Clear()
Dim a As Integer = 20
Dim b As Integer = 3
Do Until b > a
'a & " " & b
' add the row to the end of the grid with the Add() method of the Rows collection...
DataGridView1.Rows.Add(New String(){a.ToString(), b.ToString()})
a = a - 2
b = b + 1
Loop
Label2.Text = "End"
End Sub