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
Related
How can I join the accid string values from the For Next loop (below) into one single string?
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim accid As String
Dim iLast As Integer
iLast = trv.Nodes.Count
Dim p As Integer = 0
For p = 0 To iLast - 1
If TrV.Nodes(p).Checked = True Then
accid = Strings.Left(TrV.Nodes(p).Text, 9)
MsgBox(accid)
End If
Next
End Sub
This gives me a separate output of string "accid"
I want this output: "accid1,accid2,accid3"
Thanks for supporting!
You need to build your string inside the loop, and then do the MsgBox outside the loop. Something like this should work:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim accid As String
Dim iLast As Integer
iLast = trv.Nodes.Count
Dim p As Integer = 0
For p = 0 To iLast - 1
If TrV.Nodes(p).Checked = True Then
accid = accid & Strings.Left(TrV.Nodes(p).Text, 9) & "," 'notice the change here
End If
Next
accid = accid.Remove(accid.Length - 1, 1)
MsgBox(accid)
End Sub
This could be done with very few lines of code using Linq and String.Join.
Dim texts = TrV.Nodes.Cast(Of TreeNode)().
Where(Function(n) n.Checked).
Select(Function(c) Strings.Left(c.Text,9))
Dim result = String.Join(",", texts)
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
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
I have 4 text box that accepts numbers from user and i want to check for what are the two largest numbers entered, can this be done using if then else statements
Dim Big_num_1 As Integer
Dim Big_num_2 As Integer
'Dim txtbox_1 As Integer
'Dim txtbox_2 As Integer
'Dim txtbox_3 As Integer
'Dim txtbox_4 As Integer
Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click
'if then else statements
End Sub
i am a beginer in Vb.net and i would really appreciate any help that i can get
If possible i would like a If then else statement solution for this
This can be done with an if-statement like this..
Dim Big_num_1 As Integer = 0
Dim Big_num_2 As Integer = 0
'Dim txtbox_1 As Integer
'Dim txtbox_2 As Integer
'Dim txtbox_3 As Integer
'Dim txtbox_4 As Integer
Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click
Dim numList(4) as Integer
numList(0) = txtbox_1
numList(1) = txtbox_2
numList(2) = txtbox_3
numList(3) = txtbox_4
For x as Integer = 0 to numList.count - 1
If numList(x) > Big_num_1 Then
Big_num_2 = Big_num_1
Big_num_1 = numList(x)
Else If numList(x) > Big_num_2 Then
Big_num_2 = numList(x)
End If
Next
End Sub
Private Sub Docheck()
Dim Numbers As String() = {TextBox1.Text, TextBox2.Text, TextBox3.Text}
Dim Ordered As String() = Numbers.OrderByDescending(Function(x) CInt(x)).ToArray
Dim Highest As Integer = CInt(Ordered(0))
Dim SecondHighest As Integer = CInt(Ordered(1))
MessageBox.Show(String.Concat(Highest , " " , SecondHighest))
End Sub
And in your button click event you call
Docheck()
You could use If/Else statements, but that would be the hard way to do this:
Private Sub btnShow2BigNum_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow2BigNum.Click
Dim Numbers = { TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text }
Dim results = Numbers.Select(Function(s) Integer.Parse(s)).OrderByDescending().Take(2).ToList()
Big_num_1 = results(0)
Big_num_2 = results(1)
End Sub
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