Carry objreader file being read over from form to form - vb.net

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!

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 :)

Reading to a file line by line multiple choice quiz vb.net

I'm trying to create a multi choice quiz that will read questions and answers from a text file and if the RadioButton selected by the user contains text the same as the answer then the mark will increment by 2. I have tried every single loops and techniques but it either reads the last line only (for loop) or doesn't read the text file at all (do until / do while / while). I need help.
This is what I have done so far
Imports System.IO
Public Class Form5
Dim easy As Boolean
Dim medium As Boolean
Dim difficult As Boolean
Dim easytest As New System.IO.StreamReader("he.txt")
Dim lineseasy() As String = IO.File.ReadAllLines("he.txt")
Dim mediumtest As New System.IO.StreamReader("hm.txt")
Dim linesmedium() As String = IO.File.ReadAllLines("hm.txt")
Dim difficulttest As New System.IO.StreamReader("hd.txt")
Dim fulldata As String
Dim mark As Integer = 0
Private Sub Form5_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Label2.Text = Form3.ComboBox1.Text
Label3.Text = Form3.ComboBox2.Text
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
easytest.Read()
Dim counter As Integer = 0
Dim answer As String
While (easytest.Peek() <> -1)
Dim items() As String = lineseasy(1).Split(",")
Label4.Text = items(0)
RadioButton1.Text = items(1)
RadioButton2.Text = items(2)
answer = Str(3)
End While
End Sub
End Class
Why do you want a textfile where if u use Access database or sql database, it will be very simple .
For example, you create a db in either sql or access, create a table in it, create 3 columns, 1st column for question id,2nd column for question and the last one for the answer.Now add your question id(any random alpha-numeric/any string value),questions and answers in specific cells one by one. Now in you vb.net app,add a label(make it invisible) that will contain the question id, add reference to System.Data.SqlClient(for sql database) or System.Data.OleDb(for access). Now use the following code to check if the answer is correct or not ..
'add this code to all 4 radio buttons
Private Sub RadioBtn1_CheckedChanged
'create your connection string
connectionstring.open
Dim cmd as new SqlCommand("Select * from [table name from database-remove brackets if required] where [question id]=#quid and [answer]=#ans",connectionstring) 'use OleDbCommand of access db
cmd.parametres.add("#quid",sqldbtype.varchar).value=label1.text 'use oledbtype for access db
cmd.parametres.add("#ans",sqldbtype.varcha).value=radiobutton1.text 'when adding this code to radio button2, chage radiobutton1.text with radiobutton2.text , when using it in radio button 3 do the same
Dim adapter As New SqlDataAdapter(cmd) ' use oledbdataadapter for access
Dim table As New DataTable
adapter.Fill(table)
If table.Rows.Count() <= 0 Then
msgbox("WRONG ANSWER")
Else
marktext.text = mark.text+2 ' to increase point by 2
'what ever you want to do when the answer is correct
end if
Now, i hope u know how to load the question/question id(i mean the data) from the database...if you don't that please leave a reply and i'll add them

What is the best loop to use for a big list?

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

Openfile on event

I am getting my feet wet with VB .Net programming (total novice). I have a DataGridView with amongst other information, a file path to where a particular document is stored. I have added a DataGridViewButtonColumn to the DataGridView, but I cannot figure out how to get the button to open the file.
Sorry, I have no code to provide as starting point for where I get stuck.
Thanks in advance,
Sorry I didn't read the post clear enough the first time, and didn't explain my code enough so it got deleted. This uses the contentclick event.
Dim Filetext As String = "" 'At start of the class to make it available to the whole class
Private Sub DataGridView1_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim FilePathColumn As Integer = 0 'File path is in Column 0
Dim ButtonColumn As Integer = 1 'Column buttons are in
Dim RowClicked As Integer = e.RowIndex 'This gets the row that you clicked the button in
If e.ColumnIndex = ButtonColumn Then 'Make sure you clicked a button
Dim FILE_PATH As String = DataGridView1.Rows(RowClicked).Cells(FilePathColumn).ToString 'Get the path to the file
If System.IO.File.Exists(FILE_PATH) Then 'Make sure file exists
Filetext = System.IO.File.ReadAllText(FILE_PATH) 'Save file to a variable
'OR
Process.Start(FILE_PATH) 'To open it
End If
End If
End Sub
You can get rid of most of those lines but I wrote it like that to explain how it worked

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