Visual Basic (mistakes in code) - vb.net

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

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

Fibonacci Sequence Visual Basic

I have a quick question about another Visual Basic assignment I'm working on. I have all the code and everything has gone smoothly so far. The app is meant to display the first 100 Fibonacci numbers in a list box, adding the two previously displayed numbers to get the next in a loop. The only problem is that when I hit the button to display the code, the loop continues, and doesn't just stop at 100 numbers. Where did I go wrong?
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim dblA As Double = 0
Dim dblB As Double = 1
Dim dblC As Double
Dim intCounter As Integer
lstSequence.Items.Add(dblA.ToString)
lstSequence.Items.Add(dblB.ToString)
For intCounter = 1 To 100
dblC = dblA + dblB
dblA = dblB
dblB = dblC
lstSequence.Items.Add(dblC.ToString)
Next
End Sub
I just tried this. It works fine.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a As Integer = 0
Dim b As Integer = 1
Dim fib As Integer
Dim userinput, i As Integer
userinput = InputBox("how many interations?")
i = userinput
ListView1.Items.Add(1)
Do
fib = a + b
a = b
b = fib
ListView1.Items.Add(fib)
i = i + 1
Loop While fib < i
End Sub
End Class

Convert each String in ListBox1 to Integer and add to ListBox2: Incorrectly formatted input string

I have one TextBox, two ListBoxes and two Buttons. The first Button separates each String in the TextBox and adds them one by one to ListBox1. The second Button converts each String in to an Integer. But I get an exception thrown by the debugger:
Incorrectly formatted input string.
System.FormatException was unhandled
HResult=-2146233033
Message=Cadeia de caracteres de entrada com formato incorrecto.
Source=mscorlib
StackTrace:
em System.Number.StringToNumber(String str, NumberStyles options,
NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
em System.Number.ParseDecimal(String value, NumberStyles options,
NumberFormatInfo numfmt)
em System.Convert.ToDecimal(String value)
em g_.Form2.Button2_Click(Object sender, EventArgs e) em
C:\Users\Utilizador\Documents\Visual Studio
2012\Projects\g+\g+\Form2.vb:line 17
I have inserted code to clean the empty spaces but it continues to throw the same error.
After to reflecting to the problem i have decide to change the way to do it
This is my code:
Public Class Form2
Dim frequency
Dim interval
Dim textconverted
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myArray() As Char
myArray = Me.TextBox1.Text.ToCharArray
For Each chr As Char In Me.TextBox1.Text
ListBox1.Items.Add(chr)
Next
For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1
If ListBox1.GetItemText(ListBox1.Items(i)) = String.Empty Then
ListBox1.Items.RemoveAt(i)
End If
Next i
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For i As Integer = 0 To ListBox1.Items.Count - 1
If (ListBox1.Items(i).ToString.Contains("a")) Then
ListBox2.Items.Add("1") 'Indexing is zero-based
Exit For
End If
Next
End Sub
End Class
Well i find a different way to do it and its do what i need.
This is the working solution
Public Class Form2
Dim frequency
Dim interval
Dim textconverted
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim myArray() As Char
myArray = Me.TextBox1.Text.ToCharArray
For Each chr As Char In Me.TextBox1.Text
ListBox1.Items.Add(chr)
Next
For i As Integer = ListBox1.Items.Count - 1 To 0 Step -1
If ListBox1.GetItemText(ListBox1.Items(i)) = String.Empty Then
ListBox1.Items.RemoveAt(i)
End If
Next i
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim myVariable As String = "foo"
For x As Integer = 0 To ListBox1.Items.Count - 1
If ListBox1.Items(x) = "a" Then
ListBox2.Items.Add("1")
End If
If ListBox1.Items(x) = "b" Then
ListBox2.Items.Add("2")
End If
If ListBox1.Items(x) = "c" Then
ListBox2.Items.Add("3")
End If
Next
End Sub
End Class
But now i have other problem with it it only check one string
How can i verify multiple strings there?
I added .ToString to your button 2 code and it worked fine. Turn on Option Strict; you will avoid some runtime errors. I have posted code that will handle a-z instead of a bunch of if statements. Add .ToLower to your TextBox1.Text.ToLower myArray is never used neither is myVariable.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For x As Integer = 0 To ListBox3.Items.Count - 1
Dim s As String = (Asc(ListBox3.Items(x).ToString) - 96).ToString
ListBox2.Items.Add(s)
Next
End Sub

Visual Studio 2017 Global Declaration

Here I want to generate a number (n) of random number into a listbox and then using another listbox to show the generated random number in accending order.
Since it is separated into two private class, i tried to use public shared for my array mark() but it doesn't seems to work.
This is my complete code.
I have no issue generating the random number into my array mark() as i declared it in the private sub.
But when I do the sorting using another button, it shows error that I did not declare my array. If I re-declare my array, the number that i stored in it will be gone and the sorting turns into all 0.
Any idea how?
Public Class Form1
Dim n As Integer
Public Shared mark() As Integer
Dim i As Integer
Dim temp As Integer
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim i As Integer
n = Val(TextBox2.Text)
TextBox2.Text = ""
ListBox1.Items.Clear()
i = 0
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Clear()
Dim mark(n - 1) As Integer
For i = 0 To n - 1
mark(i) = Format("#", Rnd() * 100)
ListBox1.Items.Add(mark(i))
Next
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
ListBox2.Items.Clear()
For i = 0 To n - 1
For j = 0 To n - 2
If mark(j) > mark(j + 1) Then
temp = mark(j)
mark(j) = mark(j + 1)
mark(j + 1) = temp
End If
Next
Next
For k = 0 To n - 1
ListBox2.Items.Add(mark(k))
Next
End Sub
End Class

Save clicks to file

I got asked a couple of days ago how to save number of clicks you have done to each button in a program to a small file, to keep track of what is most used. Since it was ages ago i dabbled with visual basic i said i would raise the question here, so here goes.
There are 5 buttons labels Button1-Button5. When a button is clicked it should look for the correct value in a file.txt and add +1 to the value. The file is structured like this.
Button1 = 0
Button2 = 0
Button3 = 0
Button4 = 0
Button5 = 0
So when button1 is clicked it should open file.txt, look for the line containing Button1 and add +1 to the value and close the file.
I have tried looking for some kind of tutorial on this but have not found it so i'm asking the collective of brains here on Stackoverflow for some guidance.
Thanks in advance.
delete file first
new ver ..
Imports System.IO.File
Imports System.IO.Path
Imports System.IO
Imports System.Text
Public Class Form1
Dim line() As String
Dim strbild As StringBuilder
Dim arr() As String
Dim i As Integer
Dim pathAndFile As String
Dim addLine As System.IO.StreamWriter
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fileName As String = "myfile.txt"
Dim appPath As String = My.Application.Info.DirectoryPath
pathAndFile = Path.Combine(appPath, fileName)
If System.IO.File.Exists(pathAndFile) Then
'line = File.ReadAllLines(pathAndFile)
Else
Dim addLineToFile = System.IO.File.CreateText(pathAndFile) 'Create an empty txt file
Dim countButtons As Integer = 5 'add lines with your desired number of buttons
For i = 1 To countButtons
addLineToFile.Write("Button" & i & " = 0" & vbNewLine)
Next
addLineToFile.Dispose() ' and close file
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
writeToFile(1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
writeToFile(2)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
writeToFile(3)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
writeToFile(4)
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
writeToFile(5)
End Sub
Public Sub writeToFile(buttonId As Integer)
Dim tmpButton As String = "Button" & buttonId
strbild = New StringBuilder
line = File.ReadAllLines(pathAndFile)
Dim f As Integer = 0
For Each lineToedit As String In line
If InStr(1, lineToedit, tmpButton) > 0 Then
Dim lineSplited = lineToedit.Split
Dim cnt As Integer = lineSplited.Count
Dim newVal = addCount(CInt(lineSplited.Last()))
lineSplited(cnt - 1) = newVal
lineToedit = String.Join(" ", lineSplited)
line(f) = lineToedit
End If
f = f + 1
Next
strbild = New StringBuilder
'putting together all the reversed word
For j = 0 To line.GetUpperBound(0)
strbild.Append(line(j))
strbild.Append(vbNewLine)
Next
' writing to original file
File.WriteAllText(pathAndFile, strbild.ToString())
Me.Refresh()
End Sub
' Reverse Function
Public Function ReverseString(ByRef strToReverse As String) As String
Dim result As String = ""
For i As Integer = 0 To strToReverse.Length - 1
result += strToReverse(strToReverse.Length - 1 - i)
Next
Return result
End Function
Public Function addCount(ByVal arr As Integer) As Integer
Return arr + 1
End Function
End Class
Output in myfile.txt
Button1 = 9
Button2 = 2
Button3 = 4
Button4 = 0
Button5 = 20
Create a vb winform Application
Drag on your form 5 buttons (Button1, Button2, ... etc)
copy that :
Imports System.IO.File
Imports System.IO.Path
Imports System.IO
Imports System.Text
Public Class Form1
Dim line() As String
Dim strbild As StringBuilder
Dim arr() As String
Dim i As Integer
Dim pathAndFile As String
Dim addLine As System.IO.StreamWriter
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fileName As String = "myfile.txt"
Dim appPath As String = My.Application.Info.DirectoryPath
pathAndFile = Path.Combine(appPath, fileName)
If System.IO.File.Exists(pathAndFile) Then
line = File.ReadAllLines(pathAndFile)
Else
Dim addLineToFile = System.IO.File.CreateText(pathAndFile) 'Create an empty txt file
Dim countButtons As Integer = 5 'add lines with your desired number of buttons
For i = 1 To countButtons
addLineToFile.Write("Button" & i & " = 0" & vbNewLine)
Next
addLineToFile.Dispose() ' and close file
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
writeToFile(1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
writeToFile(2)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
writeToFile(3)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
writeToFile(4)
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
writeToFile(5)
End Sub
Public Sub writeToFile(buttonId As Integer)
Dim tmpButton As String = "Button" & buttonId
strbild = New StringBuilder
line = File.ReadAllLines(pathAndFile)
i = 0
For Each lineToedit As String In line
If lineToedit.Contains(tmpButton) Then
Dim lineSplited = lineToedit.Split
Dim newVal = addCount(CInt(lineSplited.Last()))
'lineToedit.Replace(lineSplited.Last, newVal)
lineToedit = lineToedit.Replace(lineToedit.Last, newVal)
line(i) = lineToedit
End If
i = i + 1
Next
strbild = New StringBuilder
'putting together all the reversed word
For j = 0 To line.GetUpperBound(0)
strbild.Append(line(j))
strbild.Append(vbNewLine)
Next
' writing to original file
File.WriteAllText(pathAndFile, strbild.ToString())
End Sub
' Reverse Function
Public Function ReverseString(ByRef strToReverse As String) As String
Dim result As String = ""
For i As Integer = 0 To strToReverse.Length - 1
result += strToReverse(strToReverse.Length - 1 - i)
Next
Return result
End Function
Public Function addCount(ByVal arr As Integer) As Integer
Return arr + 1
End Function
End Class
Have fun ! :)CristiC777
try this on vs2013
change If confition
line = File.ReadAllLines(pathAndFile)
i = 0
For Each lineToedit As String In line
If InStr(1, lineToedit, tmpButton) > 0 Then
'If lineToedit.Contains(tmpButton) = True Then
Dim lineSplited = lineToedit.Split
Dim newVal = addCount(CInt(lineSplited.Last()))
lineToedit = lineToedit.Replace(lineToedit.Last, newVal)
line(i) = lineToedit
End If
i = i + 1
Next