What is the best loop to use for a big list? - vb.net

I was never any good with loops for some reason, but can not live without them. following is the code I use to read a list of user details in XXXX:XXXX format. (not 4 chars exactly but you know) I can load the first user and am able to split the string and add from the colon back to listbox1, but just cannot get a loop to work and cant walk away until this is done. Normally a break from work solves the war but not an option right now.
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim R As New IO.StreamReader(OpenFileDialog1.FileName)
Dim str As String() = R.ReadLine().Split(":")
Dim Info As String = str(1)
ListBox1.Items.Add(Info)
ListBox1.SelectedIndex = 0
TextBox5.Text = ListBox1.SelectedItem
R.Close()
End Sub
I know its right in front of me.
I just tried a for next loop which I thought for sure would work and didnt.
Please help. Someone

No need for loop:
Dim lines = IO.File.ReadAllLines(OpenFileDialog1.FileName)
Dim items = Array.ConvertAll(lines, Function(line) line.Split(":"c)(1))
ListBox1.Items.AddRange(items)

You need this loop.
Using sr As New StreamReader(OpenFileDialog1.FileName)
While Not sr.EndOfStream
ListBox1.Items.Add(sr.ReadLine().Split(":")(1))
End While
End Using

Related

how to a selection of data from a csv based on dates within the csv

im very new to vb.net. im making a piece of software and im very nearly finished (its my first piece of standalone software). i have a trackbar next to a button, which im trying to use to control how large a selection of this csv file should be, and then it can download it to a seperate file. my issue is im unsure how to parse the file itself and assign variables and do some variable maths and get the selection i need. ive written some pseudocode here to sort of show what im trying to do. any help or pointing in the right direction of what im looking for would be brilliant, thankyou.
Private Sub TrackBar4_Scroll(sender As Object, e As EventArgs) Handles TrackBar4.Scroll
Label44.Text = TrackBar4.Value
Dim MonthSelection As Integer = 3
MonthSelection = Label44.Text
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If Label32.Text = "C:\" Then
MessageBox.Show("Please select Log from sign first")
Else
Dim fbd As FolderBrowserDialog = New FolderBrowserDialog()
If fbd.ShowDialog() = DialogResult.Cancel Then Exit Sub
Dim outputPath As String = IO.Path.Combine(fbd.SelectedPath, "selection log.txt")
'pseudocode
'parse file with date, time, direction, speed fields in each line
'dim SelectionRangeStart As String = LastLine.Date - MonthSelection(1,2,3,4,5,6)
'dim CSVSelectionRange As String = SelectionRangeStart to LastLine
IO.File.WriteAllText(outputPath, CSVSelectionRange)
MessageBox.Show("Success!")
End If
End Sub
ive searched for how to parse but its very complex and all forums and guides seem to be very specific and a bit difficult to understand. any help at all is greatly appreciated :)

Double-click DataGridView row using sql server?

When I Double Click On Row I am facing this error.How Can Resolve this issue help me and sorry for English
Thanks In Advance
Never post pictures of code. We can't copy it and try it out. It works for me with a bit of change.
Private Sub DataGridView1_CellDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Dim aa = DataGridView1.CurrentRow.Cells(0).Value
Dim selRow = dt.Select($"ID = {aa}")
TextBox1.Text = selRow(0)("Type")
End Sub
I have a field called ID and a field called Type in the DataTable I am testing with. I think that .Item of a DataRow wants an ordinal so I just used the default which accepts a string.

Not read text file correct

I want when I read txt file and add in ListBox1.items, add this text http://prntscr.com/on12e0 correct text §eUltra §8[§716x§8].zip not like this http://prntscr.com/on11kv
My code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
My.Computer.FileSystem.CopyFile(
appDataFolder & "\.minecraft\logs\latest.log",
appDataFolder & "\.minecraft\logs\latestc.log")
Using reader As New StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.minecraft\logs\latestc.log")
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
If line.Contains(" Reloading ResourceManager: Default,") Then
Dim lastpart As String = line.Substring(line.LastIndexOf(", ") + 1)
ListBox1.Items.Add(lastpart)
End If
End While
End Using
My.Computer.FileSystem.DeleteFile(appDataFolder & "\.minecraft\logs\latestc.log")
End Sub
This question is only different from your first question in that your have substituted a ListBox for a RichTextBox. It seems you got perfectly acceptable answers to your first question. But I will try again.
First get the path to the file. I don't know why you are copying the file so I didn't.
Add Imports System.IO to the top of your file. The you can use the File class methods. File.ReadAllLines returns an array of strings.
Next use Linq to get the items you want. Don't update the user interface on each iteration of a loop. The invisible Linq loop just adds the items to an array. Then you can update the UI once with .AddRange.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim appDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\.minecraft\logs\latest.log")
Dim lines = File.ReadAllLines(appDataFolder)
Dim lstItems = (From l In lines
Where l.Contains(" Reloading ResourceManager: Default,")
Select l.Substring(l.LastIndexOf(", ") + 1)).ToArray
ListBox1.Items.AddRange(lstItems)
End Sub
If this answer and the previous 2 answer you got don't work, please lets us know why.

Carry objreader file being read over from form to form

Okay so I'm making a multiple-choice quiz game for an assignment and I have a form for the list of categories and the actual questions. Because I'm quite new at coding and don't really have a whole lot of time to totally restructure my code in a way that I probably wouldn't understand, instead of adding all the buttons to one buttons "Handles" I just made a sub for each click event.
This is an example of one of these subs:
Public Sub btnMusic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMusic.Click
Questions.Show()
Me.Close()
Dim objReader As New System.IO.StreamReader("Music.txt")
End Sub
My text is read by the following structure:
Structure QuizQ
Dim Q As String
Dim A As String
Dim B As String
Dim C As String
Dim D As String
Dim Correct As String
End Structure
I then try to read the lines according to the structure with:
Dim I As Integer
For I = 0 To 5
MyQ(I).Q = objReader.ReadLine
MyQ(I).A = objReader.ReadLine
MyQ(I).B = objReader.ReadLine
MyQ(I).C = objReader.ReadLine
MyQ(I).D = objReader.ReadLine
MyQ(I).Correct = objReader.ReadLine
Next I
and then set the text for all of the buttons to the possible answers like so:
lblQuestion.Text = MyQ(qNum).Q
btnA.Text = MyQ(qNum).A
btnB.Text = MyQ(qNum).B
btnC.Text = MyQ(qNum).C
BtnD.Text = MyQ(qNum).D
But at this point the text isn't displayed on the buttons or where the question should be. I really have no idea what I could do from this point so any help really would be appreciated.
Thank you in advance!

HI, New to programming with textfiles loops and much more

i have a textfile which is in this format
and i am trying to use a stream reader to help me loop each word into a text box, i am new to programming and really need help because all other examples are too complicated for me to understand,
this is what i am trying to do :
Dim objectreader As New StreamReader("filepath")
Dim linereader(1) As String
linereader = Split(objectreader.ReadLine, ",")
For i As Integer = 0 To UBound(linereader)
Spelling_Test.txtSpelling1.Text = linereader(0)
Spelling_Test.txtSpelling2.Text = linereader(0)
Next
but only get the first line of the text file in to a textbox, i need it to loop to the next line so i can write the next line in!
your help would be much appreciated, and if possible then can you show it practically , if you dont understand what i am trying to do then please ask
It is a little confusing on what you are trying to do, it looks like your text file consists of a word and a hint, you only have one set of textbox's and 3 lines of information in your file.
This example show you how to incrementally read your Stream.
Public Class Form1
Dim objectreader As StreamReader
Dim linereader() As String
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If IsNothing(objectreader) Then
objectreader = New StreamReader("C:\Temp\data.txt")
End If
linereader = Split(objectreader.ReadLine, ",")
If String.IsNullOrEmpty(linereader(0)) Then
objectreader.Close()
objectreader = Nothing
Else
txtSpelling1.Text = linereader(0) 'Word
txtSpelling2.Text = linereader(1) 'Hint
End If
End Sub
End Class
But in your case, I would probably just use the File.ReadAllLines Method
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Dim result As String() = File.ReadAllLines("C:\Temp\data.txt")
For x = 0 To UBound(result)
Dim c As Control = Controls.Find("txtSpelling" + (x + 1).ToString, True)(0)
If Not IsNothing(c) Then
c.Text = Split(result(x), ",")(0)
End If
Next
End Sub