Fibonacci Sequence Visual Basic - vb.net

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

Related

How to convert string to timespan ? visual basic, stopwatch, lap system

I am a beginer and making a stopwatch with lap feature using visual studio 2012, visual basic. Now I stuck in the lap feature. In the lap feature I want to display the result using listview ...
When the user click the lap button 2nd times it has to show the lap time between first click and 2nd click.. but i m not getting the result. Is here anybody to resolve my problem ? thank u in advance
here is the code of lap button event
Private Sub btnLap_Click(sender As Object, e As EventArgs) Handles btnLap.Click
Dim lap As String = (sw.Elapsed.Hours.ToString("00") & ":" & sw.Elapsed.Minutes.ToString("00") & _
":" & sw.Elapsed.Seconds.ToString("00") & ":" & sw.Elapsed.Milliseconds.ToString("00"))
lapcount += 1
Dim i As Integer = ListView1.Items.Count
If i <= 0 Then
ListView1.Items.Add(CStr(lapcount), i)
ListView1.Items(i).SubItems.Add(lap)
ListView1.Items(i).SubItems.Add(lap)
Else
ListView1.Items.Add(CStr(lapcount), i)
ListView1.Items(i).SubItems.Add(lap)
ListView1.Items(i).SubItems.Add((TimeSpan.parse(lap)- Timespan.parse(lastlap)).ToString) ''' I can't subtract the value from 2nd click to first click
End If
lastlap = (ListView1.Items(i).SubItems(1).ToString)
End Sub
To be honest, its usually a bad idea to use UI objects as your primary store of data. You would be much better storing the split times in a List (Of TimeSpan) and calculating calculating the lap times from that.
In my sample I declared some class objects so that the code would run. You could adapt these as necessary.
Dim sw As New Stopwatch
Dim lapcount As Integer = 1
Dim lastlap As Integer
Dim lapintervals As New List(Of TimeSpan)
Dim timeFormat As String = "hh\:mm\:ss\.fff"
So in your button click event you would have something like this ..
Private Sub BtnLap_Click(sender As Object, e As EventArgs) Handles BtnLap.Click
Dim ts As TimeSpan = sw.Elapsed
lapintervals.Add(ts)
AddTimeToListView(lapintervals.Count, lapintervals.Last)
lapcount += 1
End Sub
And you would add the info to your ListView like this
Private Sub AddTimeToListView(lapcount As Integer, lapsplit As TimeSpan)
Dim lvItem As New ListViewItem(lapcount.ToString, lapcount)
If lapcount = 1 Then
lvItem.SubItems.Add(lapsplit.ToString(timeFormat))
lvItem.SubItems.Add(lapsplit.ToString(timeFormat))
Else
lvItem.SubItems.Add(lapsplit.ToString(timeFormat))
lvItem.SubItems.Add((lapsplit.Subtract(lapintervals(lapcount - 2))).ToString(timeFormat))
End If
ListView1.Items.Add(lvItem)
End Sub
thenk you very much for answering...
finally i have finished my stopwatch application ..
i've made this and my leader gave me 8 out of 10 .. it pretty much good for me
here is the design form and code
visual basic 2012, stopwatch with lap button
enter image description here
[code]
Public Class Stopwatch
Public watch As Form 'スタートページから呼び出すため
Dim sw As New System.Diagnostics.Stopwatch()
Dim lapcount As Integer
Dim lastlap As String
'スタート・ストップボタン
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
If btnStart.Text = "Start" Then
sw.Start() '時間計測開始
Timer1.Interval = 10
Timer1.Enabled = True
btnStart.Text = "Stop"
Else
sw.Stop() '時間計測一時停止
Timer1.Enabled = False
btnStart.Text = "Start"
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
lblTime.Text = sw.Elapsed.ToString("hh\:mm\:ss\:ff")
End Sub
'cancel / reset ボタン
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
sw.Reset() '初期状態にする
Timer1.Enabled = False
lblTime.Text = "00:00:00:00"
ListView1.Items.Clear()
btnStart.Text = "Start"
lapcount = 0
End Sub
'ラップボタン
Private Sub btnLap_Click(sender As Object, e As EventArgs) Handles btnLap.Click
Dim lap = sw.Elapsed.ToString("hh\:mm\:ss\:fff") ' ラップ時間を変数に代入
lapcount += 1
Dim i As Integer = ListView1.Items.Count
If i <= 0 Then ' ListviewのアイテムのValueが0または0より小さければ
ListView1.Items.Add(CStr(lapcount), i)
ListView1.Items(i).SubItems.Add(lap) 'アイテムとサブアイテム表示
ListView1.Items(i).SubItems.Add(lap)
Else '文字列となっている結果時間を整数型に変換する/ // ラップ経過時間を取得するため
Dim day As Integer = 0
Dim hr As Integer = CInt(Strings.Left(lap, 2))
Dim min As Integer = CInt(Strings.Mid(lap, 4, 2))
Dim sec As Integer = CInt(Strings.Mid(lap, 7, 2))
Dim milli As Integer = CInt(Strings.Mid(lap, 10, 3))
Dim dayy As Integer = 0
Dim hrr As Integer = CInt(Strings.Mid(lastlap, 19, 2))
Dim minn As Integer = CInt(Strings.Mid(lastlap, 22, 2))
Dim secc As Integer = CInt(Strings.Mid(lastlap, 25, 2))
Dim millii As Integer = CInt(Strings.Mid(lastlap, 28, 3))
'現在のラップ時間に直前に記憶したラップ時間を引き、1回目から2回目のラップボタン押下までの経過時間を図る
Dim answer = createtimespan(day, hr, min, sec, milli) - createtimespan(dayy, hrr, minn, secc, millii)
Dim ansTS = answer.ToString("hh\:mm\:ss\:fff") 'ミリ秒3桁まで表示するように指定
ListView1.Items.Add(CStr(lapcount), i)
ListView1.Items(i).SubItems.Add(lap)
ListView1.Items(i).SubItems.Add((ansTS).ToString) 'ラップ時間の表示
End If
lastlap = ListView1.Items(i).SubItems(1).ToString '直前のラップタイムを記憶
End Sub
Private Function createtimespan(ByVal days As Integer, ByVal hours As Integer, ByVal minutes As Integer, _
ByVal seconds As Integer, ByVal milliseconds As Integer) As TimeSpan
Dim elapsedTime As New TimeSpan(days, hours, minutes, seconds, milliseconds)
Return (elapsedTime)
End Function
End Class

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

Get Character Of IndexOf

For an assigment my teacher is asking that we read from a file to find the characters of our name and place them at a label at the top of the form.
here is my code:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
searchFile = File.OpenText("AcademicEthicsandIntegrityStatement.txt")
Dim s As String = searchFile.ReadToEnd
Dim b As String = s.IndexOf("b"c)
Dim r As Integer = s.IndexOf("r"c)
Dim i As Integer = s.IndexOf("i"c)
Dim a As Integer = s.IndexOf("a"c)
Dim n As Integer = s.IndexOf("n"c)
Dim ec As Integer = s.IndexOf("e"c)
Dim bRead = GetChar(s, b)
Dim rRead = GetChar(s, r)
Dim iRead = GetChar(s, i)
Dim aRead = GetChar(s, a)
Dim nRead = GetChar(s, n)
Dim ecRead = GetChar(s, ec)
lblName.Text = bRead + rRead + iRead + aRead + nRead + nRead + ecRead
End Sub
The text that is reading into my lbl is "gmcaad" instead of "brianne"
Im sure that I am missing something here or there is a much easier way to do this. Any help is appreciated.
IndexOf returns a zero-based index.
GetChar accepts a one-based index.
For consistency,
if you want to use IndexOf, then use direct indexing instead of GetChar:
Dim bRead = s(b)
Dim rRead = s(r)
if you want to use GetChar, then use InStr instead of IndexOf that also returns one-based values.
Short Answer...case sensitive:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
With File.ReadAllText("AcademicEthicsandIntegrityStatement.txt")
For Each C As Char In "Brianne".ToCharArray
' Note this is case-sensitive because it uses a binary comparison
Dim Index As Integer = .IndexOf(C)
If Index >= 0 Then lblName.Text &= .Substring(Index, 1)
Next
End With
End Sub
... and non-case sensitive:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
With File.ReadAllText("AcademicEthicsandIntegrityStatement.txt")
For Each C As Char In "Brianne".ToCharArray
' Note this is not case-sensitive
Dim Index As Integer = .IndexOf(C.ToString, StringComparison.InvariantCultureIgnoreCase)
If Index >= 0 Then lblName.Text &= .Substring(Index, 1)
Next
End With
End Sub

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