Sorting Numbers in Textbox in VB.Net - 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

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!

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

Vb Write a program to print multiples of 2 and 3

For my class, i need to write a program to find multiples of 2 and 3. The code i have would get me multiples of any number inputted into the program. My problem is that nothing is showing up in the message box that i've created and i don't know why. here's the code.
Public Class form1
Private Sub Button1_Click(ByVal Sender)
Dim Number1 As Integer
Dim Number2 As Integer
Dim Multiplier As Integer
Dim Answer As Integer
Dim i As Integer
Number1 = Val(TextBox1.Text)
Number2 = Val(TextBox2.Text)
Multiplier = 1
Do While Multiplier <= 10
For i = Number1 To Number2
Answer = i * Multiplier
ListBox1.Items.Add(i & "*" & Multiplier & "=" & Answer)
Next i
Multiplier = Multiplier + 1
Loop
End Sub
End Class
Any help at all would be appreciated.
I have not tested it but I think, this is what you looking for - all numbers that can be divided by 3 and 2 using multipliers from 1 to 10 over the range of numbers in your text boxes. In your code, I don't see where you weeding out your numbers that can be divided by 2 and 3
Private Sub Button1_Click(ByVal sender as Object, ByVal e as EventArgs) Handles Button1.Click
Dim num1 As Integer = Integer.Parse(TextBox1.Text)
Dim num2 As Integer = Integer.Parse(TextBox2.Text)
' may be need to check num2 > num1
Dim sum As Integer
For mult as Integer = 1 to 10
For i as integer = num1 To num2
total = i * mult
If sum Mod 2 = 0 OrElse sum Mod 3 = 0 Then
ListBox1.Items.Add(i.ToString() & " * " & mult & " = " & sum.ToString())
End If
Next i
Next
End Sub
This is my best guess as to what you are wanting. You said you were wanting multiples of 2 and 3 for any numbers given to the program, that that's what this does. If you wanted multiples of anything else, just add onto the part inside the {} in the coefficients declaration. Instead of using text boxes for input, I suggest using a NumericUpDowninstead; the GUI will be more intuitive to the end user.
Imports System.Text
Public Class Form1
Private Property maxNumb As Integer
Private Property minNumb As Integer
Private coefficients() As Integer = {2, 3}
Private Sub Button1_Click(sender As Button, e As EventArgs) Handles Button1.Click
Dim sb As New StringBuilder
For i = Me.minNumb To maxNumb Step 1
For Each coef As Integer In coefficients
sb.Append(i & " * ").Append(coef).Append(" = ").Append(i * coef)
Me.ListBox1.Items.Add(sb.ToString)
sb.Clear()
Next
Next
Me.ListBox1.Refresh()
End Sub
Private Sub NumericUpDown_ValueChanged(sender As NumericUpDown, e As EventArgs) Handles min_NumericUpDown1.ValueChanged, max_NumericUpDown2.ValueChanged
If sender.Name.Contains("max") Then
Me.maxNumb = sender.Value
Else
Me.minNumb = sender.Value
End If
End Sub
End Class

Visual Basic (mistakes in code)

I'm new to Visual Studio.I tried to write a simple program in Visual Basic that takes a 13-digit number from a text box and writes its digits to an array.Then it writes the second member of the array (second digit of the number) to another text box, but it doesn't work. Here's the code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim array(12) As Integer
Dim index As Integer = 11
Dim code As Long = TextBox1.Text
Do While index >= 0
array(index) = code Mod 10
code /= 10
index -= 1
Loop
TextBox2.Text = array(1)
End Sub
End Class
Can you tell me what's wrong?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim array(12) As Integer
Dim index As Integer = 11
Dim code As Char() = TextBox1.Text.ToCharArray()
For i As Integer = 0 To code.Count - 1
array(i) = Integer.Parse(code(i))
Next
TextBox2.Text = array(1)
End Sub