next button to grab next line of text in a text file - vb.net

I've still be trying to learn how to make a quiz in vb.net. I'm getting closer. I have a button that reads next and one that reads submit. I have a label that reads: Question 1: and a text box that has the question in it. I have a text box underneath the 1st text box that will have multiple choice answers. How do I get the next button to read the next question in my text file so the person can choose an answer before moving to next question when hitting the next button?
This is my code so far:
Private Sub Button40_Click(sender As Object, e As EventArgs) Handles Button40.Click
Dim FILE_NAME As String = "C:\Quiz\GenesisQuiz.txt"
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
TextBox1.Text = objReader.ReadToEnd & vbCrLf
objReader.Close()
Else
MessageBox.Show("Wrong answer, please try again!")
End If
End Sub
This of course reads the first question into the text box next to label Question 1: If I put any more lines of text into the text file then all the lines get read into the textbox.
Thank you for your help. I think it has to do with a For statement like:
For i As Integer = 1 To lineNumber - 1
Learning is fun!
Is this on the right track?
Public Class Quiz
Public question As String
Public choices As String
Public answer As String
Public Score As Integer
Public Sub New(question As String, choices As String, answer As String, score As Integer)
Me.question = question
Me.choices = Choices
Me.answer = Answer
Me.Score = Score
End Sub
End Class
Private Sub Button40_Click(sender As Object, e As EventArgs) Handles Button40.Click
Dim QuizList As New List(Of Quiz)()
End Sub
End Class
Continuing to learn! Thanks for your responses!

Thanks to Plutonix and BobRodes for helping me out!
Public Class Form1
Dim lines() As String
Dim answers() As String
Dim index As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lines = IO.File.ReadAllLines("C:\Quiz\GenesisQuiz.txt")
answers = IO.File.ReadAllLines("C:\Quiz\GenesisAnswers.txt")
Private Sub Button40_Click(sender As Object, e As EventArgs) Handles Button40.Click
TextBox1.Text &= lines(index) & Environment.NewLine
index += 1
If index > lines.GetUpperBound(0) Then Button40.Enabled = False
End Sub
Private Sub Button41_Click(sender As Object, e As EventArgs) Handles Button41.Click
TextBox1.Clear()
End Sub
Private Sub Button40_MouseClick(sender As Object, e As MouseEventArgs) Handles Button40.MouseClick
If File.Exists("C:\Quiz\GenesisAnswers.txt") Then
Dim ioFile As New StreamReader("C:\Quiz\GenesisAnswers.txt")
TextBox2.Text = ioFile.ReadLine()
TextBox3.Text = ioFile.ReadLine()
TextBox4.Text = ioFile.ReadLine()
TextBox5.Text = ioFile.ReadLine()
End If
End Sub
Private Sub Button41_MouseClick(sender As Object, e As MouseEventArgs) Handles Button41.MouseClick
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
TextBox5.Clear()
End Sub
so this is what I have so far for my quiz. Still a ways off but getting there. Still learning and having fun. Feel free to give constructive criticism!!!

Related

Saving and reading files on Visual basic

Hi I'm creating a "Toilet paper tracker" on visual basic and I'm struggling with saving and reading files, I know I am missing stuff. The user should be able to login and input a threshold and when reached a warning will pop up saying "buy more toilet paper" (i haven't coded this yet) and the user can add to create a total and subtract from it too. The user should also be able to save the total to a file and I want the program to be able to read the file and change the total if the user wants to add or subtract again. It would be greatly appreciated if you pointed me in the right direction, I'm only young so it's relatively simple. Here is my program :)
Imports System.IO
Public Class frmTPT
Private Sub TPT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call loadchangetotal()
End Sub
Sub loadchangetotal()
cboChange.Items.Add("Add to Total")
cboChange.Items.Add("Subtract from Total")
End Sub
Private Sub cboVenue_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboChange.SelectedIndexChanged
If cboChange.Text = "Add to Total" Then
Dim frmChangeACopy As New frmChangeA
frmChangeACopy.Show()
Me.Hide()
ElseIf cboChange.Text = "Subtract from Total" Then
Dim frmChangeSCopy As New frmChangeS
frmChangeSCopy.Show()
Me.Hide()
End If
End Sub
Private Sub btnReturn_Click(sender As Object, e As EventArgs)
Dim frmLoginCopy As New frmLogin
frmLoginCopy.Show()
Me.Hide()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtThreshold.Text = ""
cboChange.Text = ""
txtTotal.Text = ""
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub
Private Sub LogoutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LogoutToolStripMenuItem.Click
Dim frmLoginCopy As New frmLogin
frmLoginCopy.Show()
Me.Hide()
End Sub
Private Sub btnReadTotal_Click(sender As Object, e As EventArgs) Handles btnReadTotal.Click
Dim FileReader As StreamReader
Dim result As DialogResult
result = OpenFileDialog1.ShowDialog
If result = DialogResult.OK Then
FileReader = New StreamReader(OpenFileDialog1.Filename)
txtFileContent.Text = FileReader.ReadToEnd() 'i want to be able to read a
'previously saved total so that
FileReader.Close() 'it can be used to find the new total
'after it has been added to
End If 'or subtratced
End Sub
Private Sub SaveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SaveToolStripMenuItem.Click
Call SaveFile()
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim A, S, NewTotal As Integer
A = Val(frmChangeA.txtAdd.Text)
S = Val(frmChangeS.txtSubtract.Text)
NewTotal = A - S 'I want to be able to load a previously saved total if one exists and add and
'subtract from it
End Sub
End Class
Sub SaveFile()
Dim FileWriter As StreamWriter
Dim results As DialogResult
results = SaveFileDialog1.ShowDialog
If results = DialogResult.OK Then
FileWriter = New StreamWriter(SaveFileDialog1.FileName, False)
FileWriter.Write(txtFileContent.Text) ' is txtFileContent supposed to be
' the name of my textbox?
FileWriter.Close()
End If
End Sub
Design
You didn't mention if you were using .Net Core or 4.x. If the later, you can sometimes use the Insert Snippet functionality to learn how to do common tasks. For example in this case you could right click in the code editor and select Insert Snippet then Fundamentals then File System and finally Write text to a file. This will result in the following VB code:
My.Computer.FileSystem.WriteAllText("C:\Test.txt", "Text", True)
Unfortunately, this option doesn't work with .Net core since the My namespace wasn't ported to core.
The key point of this problem lies in reading and writing data from text. It is a clear idea to write two methods to achieve read and write.
You can refer to the following code. The two methods in the following example are WriteTotal(), ReadTotal().
Design:
Public Class Form1
Dim Oldtotal As Integer
Dim Newtotal As Integer
Private Sub btnLoadTotal_Click(sender As Object, e As EventArgs) Handles btnLoadTotal.Click
WriteTotal()
ReadTotal()
End Sub
Private Sub btnUpdateTotal_Click(sender As Object, e As EventArgs) Handles btnUpdateTotal.Click
cboChange.Text = Nothing
WriteTotal()
ReadTotal()
MsgBox("Inventory updated")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
cboChange.SelectedIndex = 0
ReadTotal()
End Sub
Sub WriteTotal()
Using swriter As IO.StreamWriter = New IO.StreamWriter("D:/paperstore.txt", False, System.Text.Encoding.UTF8)
If cboChange.Text = "Add to Total" Then
Newtotal = Oldtotal + CType(txtThreshold.Text, Integer)
swriter.WriteLine(Newtotal)
ElseIf cboChange.Text = "Subtract from Total" Then
If CType(txtThreshold.Text, Integer) > Oldtotal Then
swriter.WriteLine(Oldtotal)
MsgBox("buy more toilet paper")
Else
Newtotal = Oldtotal - CType(txtThreshold.Text, Integer)
swriter.WriteLine(Newtotal)
End If
Else
swriter.WriteLine(txtTotal.Text)
End If
End Using
End Sub
Sub ReadTotal()
Using sreader As IO.StreamReader = New IO.StreamReader("D:/paperstore.txt", System.Text.Encoding.UTF8)
Try
Oldtotal = sreader.ReadLine()
txtTotal.Text = Oldtotal
Catch ex As Exception
MsgBox(ex.Message)
End
End Try
End Using
End Sub
End Class

when the text in a textbox is equal to a certain word i need to value in the combo box to be saved for that text

I have orders in text files in the debug folder and when i type the name of the order in a text box it displays the order is a list box and there is a combo box underneath where i can change the status of the meal preparation (Being prepared, ready to deliver etc,). If i go back to that form and type in the same order name into the textbox i need to previous prepartion status to be already in the textbox. Thanks for any help!
Public Class frmOrderStatus
Private Sub btnStatus_Click(sender As Object, e As EventArgs) Handles btnStatus.Click
Dim sr As IO.StreamReader = IO.File.OpenText(strTxtOrderNum & ".txt")
Do Until sr.EndOfStream
lstOrder.Items.Add(sr.ReadLine)
Loop
End Sub
Private Sub OrderStatus_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstOrder.Items.Clear()
btnStatus.Enabled = False
ChangeStatus.Enabled = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnValidate.Click
strTxtOrderNum = txtOrderNum2.Text
btnStatus.Enabled = True
ChangeStatus.Enabled = True
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
strSaveStatus = ChangeStatus.SelectedIndex
End Sub
Private Sub ChangeStatus_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ChangeStatus.SelectedIndexChanged
End Sub
End Class
It recognizes the file; it just tells you it is in use. A Stream must be closed and disposed. I don't see the StreamReader even being closed let alone disposed. A `Using...End Using block will close and dispose of objects even if there is an error.
I just used a text file I happened to have to test.
Private strTxtOrderNum As String = "host"
Private Sub ReadFile()
Using sr As IO.StreamReader = IO.File.OpenText(strTxtOrderNum & ".txt")
Do Until sr.EndOfStream
ListBox1.Items.Add(sr.ReadLine)
Loop
End Using
End Sub
Private Sub WriteFile()
Dim strSelectedItem = ComboBox1.Text
Using swVar As IO.StreamWriter = IO.File.AppendText(strTxtOrderNum & ".txt")
swVar.WriteLine(strSelectedItem)
End Using
End Sub

vb.net - How to get value from each line in richtextbox and show each values into textboxes

i have a question in my code, Question is
How to get value from each line in richtextbox and show each values into textboxes?
my code is :
Imports System.IO
Public Class Form1
Private Results As String
Private Sub UpdateText()
Dim xList As New List(Of KeyValuePair(Of String, String))
Results = Results.Replace(vbLf, "")
Dim LineSplit() As String = Results.Split(vbCr)
For Each xLine As String In LineSplit
If xLine <> "" Then
xList.Add(New KeyValuePair(Of String, String)(xLine.Split("=")(0), xLine.Split("=")(1).Trim.Replace(" ", "")))
End If
Next
'do some work here to put the values in the right textboxes
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
UpdateText()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Results = RichTextBox1.Text
End Sub
End Class
sorry for my bad english, i am from Indonesia, thanks..
Instead of List you could loop over controls of Textbox type.
If number of lines is Always equal to number of textboxes you could loop over your lines or textboxes or even do a static for x=0 to 11 then simply put line x into textbox x.
As we don't know how you named these textboxes I'll show you a way that should work for you:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim counter As Integer
For Each controlOnForm In Me.Controls
If TypeOf controlOnForm Is TextBox Then
if counter <12 then 'depending how you ordered them you may need to fix this sign
controlOnForm.text = TextBox1.Lines(counter).Split("=")(1)
counter += 1
end if
End If
Next
End Sub

How to add text after every textbox line in VB.NET

I've searched everywhere, I can't find a way.
I want to find a way to add text after every textbox line but I can't find a way to do it.
I have a textbox1 with:
example1
example2
example3
And so on...
and another textbox2 with #gmail.com
I want the textbox2 to be added to the end of every line in textbox1 like:
example1#gmail.com
example2#gmail.com
example3#gmail.com
And so on...
Any way to do it? Thanks in advance.
This solution is concise, and removes empty lines.
Private Function appendTextToOtherTextLines(textToAppend As String, otherText As String) As String
Return String.Join(Environment.NewLine, otherText.
Split(Environment.NewLine.ToArray(), StringSplitOptions.RemoveEmptyEntries).
Select(Function(s) s & textToAppend))
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox3.Text = appendTextToOtherTextLines(TextBox2.Text, TextBox1.Text)
End Sub
Here's your example working
And if you had an empty line, it is removed in the resulting string
Of course, you could overwrite the original textbox instead, but careful not to click the button twice!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = appendTextToOtherTextLines(TextBox2.Text, TextBox1.Text)
End Sub
Other option is an event handler which will make this happen automatically when pressing enter at the end of a new line. This is only useful if you are actively entering the lines manually.
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Enter Then
TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 2) & TextBox2.Text & Environment.NewLine
TextBox1.SelectionStart = TextBox1.Text.Length
End If
End Sub
(this option requires some discipline when pressing enter)
This is full working code.Enjoy it!!!!
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Me.TextBox1.Lines.Length = 0 Then
MsgBox("Please enter some data in textbox1")
Exit Sub
End If
'---------------------------
Dim b(Me.TextBox1.Lines.Length - 1) As String
Dim i As Integer = 0
For Each a As String In Me.TextBox1.Lines
b(i) = a + Me.TextBox2.Text
i = i + 1
Next
'-----------------
Me.TextBox1.Clear()
Me.TextBox1.Lines = b
End Sub

vb.net array for slideshow button

Hey all, i am in the middle of trying to figure out how to go about doing this...
I have a form that i call like so:
call frmSlideShow.showWhat("1,4,8,9,11,22")
Each of the numbers represents a different image slide (slide1.png, slide4.png, etc..). The problem i am having is trying to create a "previous" and "next" button to flip through them all. Trying to figure out what number the user is on and going from there and seeing what numbers are still left from the list above that was sent, etc.
If anyone has an idea how i would go about doing that then that would be awesome! :)
UPDATE
here is the code..
Public Sub showWhat(ByVal theNumbers As String)
Dim theNums As String() = theNumbers.Split(New Char() {","c})
Dim theCurNum As String
For Each theCurNum In theNums
Debug.Print(theCurNum)
Next
End Sub
David
Put theNums string array one level up in your code along with an integer variable that keeps track of the current position in the array. Then your next button would check the upperbounds of theNums array to make sure you weren't on the last one. If you weren't, then it would increase the integer variable by one and then you could theNums(intTracker).
Public Class Form1
Dim theNums As String()
Dim intTracker As Integer = 0
Public Sub showWhat(ByVal theNumbers As String)
theNums = theNumbers.Split(New Char() {","c})
intTracker = 0
MsgBox("Currently showing " & theNums(intTracker))
If theNums.GetUpperBound(0) < 1 Then
btnNext.Enabled = False 'Only one number was passed, so disable the next button
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call showWhat("1,2,3")
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
btnNext.Enabled = True
intTracker -= 1
MsgBox("Now on " & theNums(intTracker))
If intTracker = 0 Then
btnLast.Enabled = False 'Disable the button because you're at the beginning
End If
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
btnLast.Enabled = True
intTracker += 1
MsgBox("Now on " & theNums(intTracker))
If theNums.GetUpperBound(0) = intTracker Then
btnNext.Enabled = False 'On the last slide, so disable the next button
End If
End Sub
End Class