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
Related
I have a prank antivirus program I'm making for a friend. Part of the software requires "activation" to remove the "virus". I have 4 TextBoxes when I click the button I want all 4 TexBoxes to be checked for the text "0000". When I have one TextBox it works great, but I need all 4 boxes to get checked before the message box appears. I hope this makes sense. See image here
[Edit] I'm going to mention i'm a total noob at programming.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "0000" Then
MsgBox("Registered")
Me.Hide()
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
End Class
There are many ways to do what you want. Here's a very simple one which you can build on:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' check all the TextBoxes in the array. Return if one isn't valid
For Each textbox As TextBox In {TextBox1, TextBox2, TextBox3, TextBox4}
If textbox.Text <> "0000" Then
Return
End If
Next
' If all TextBox contains the valid string, this will appear
MsgBox("Registered")
Me.Hide()
End Sub
Have fun!
EDIT:
To have 4 different strings: just chain 4 checks. Like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' If all TextBox contains the valid string, this will appear
If TextBox1.Text = "0000" AndAlso TextBox2.Text = "1111" AndAlso TextBox3.Text = "2222" AndAlso TextBox4.Text = "3333" Then
MsgBox("Registered")
Me.Hide()
End If
End Sub
I am trying to output a specific line from a text file into an array, where each Button will produce a different line. For example Button1 should output the first line in the text file and Button2 should output the second line in the text file.
Text file:
Red
Blue
Orange
Green
When I press Button1 I get the first line in the TextBox ("Red") however when I press Button2 I still get "Red".
Code
Public Class Form1
Dim i As Integer
Dim character As String = ""
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
i = 0
readfile()
TextBox1.Text = TextBox1.Text + character
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
i = 1
readfile()
TextBox1.Text = TextBox1.Text + character
End Sub
Sub readfile()
Dim SR As New StreamReader("Colours.txt")
Dim Characters(3) As String
Do Until SR.Peek = -1
character = SR.ReadLine
Characters(i) = character
Loop
SR.Close()
End Sub
End Class
I suggest using File.ReadAllLines to read the lines text file into a String array in the form's Load event. Then your Button.Click events can just copy the required line into the TextBox.
Public Class Form1
Private lines() As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lines = IO.File.ReadAllLines("Colours.txt")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = lines(0)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = lines(1)
End Sub
End Class
In your question, it's not clear if you want to just store the selected line in the TextBox, or append the line to the TextBox. If you want to append, you can use TextBox1.Text &= lines(0) 'or lines(1) (using &= instead of =) although in that case, you probably also want to add some kind of separator.
I have textbox1.text as the main textbox. Any text that is input into textbox1.text when the submit button is selected is supposed to input the text into the next available textbox which is textbox2.text, textbox3.text and textbox4.text. How can I tell vb.net that if textbox2.text is full then input the text into textbox3.text and if textbox3.text is full to input the text into textbox4.text. Here is where I got stuck. I'm still in the process of learning more about vb.net.
Private Sub SubBttn_Click(sender As Object, e As EventArgs) Handles SubBttn.Click
If TextBox1.Text.Trim.Length > 0 Then
TextBox2.Text = TextBox1.Text
If TextBox2.Text.Trim.Length > 0 Then
TextBox3.Text = TextBox1.Text
End If
End If
End Sub
More compact code to do the job
Private Sub btnsubmit_Click(sender As Object, e As EventArgs) Handles btnsubmit.Click
Dim LstTextBox As New List(Of TextBox) 'A list to contain text boxes excluding the main text box
LstTextBox.Add(TextBox2)
LstTextBox.Add(TextBox3)
LstTextBox.Add(TextBox4)
For Each Tbox As TextBox In LstTextBox
If String.IsNullOrWhiteSpace(Tbox.Text) = True Then 'If text box is empty or contains white spaces
Tbox.Text = TextBox1.Text 'Copies text from main text box
TextBox1.Clear() 'Clears main text box
Exit Sub
End If
Next
End Sub
You can compare the textbox's textlength with the textbox's max length, and if they match up, the next
Private Sub TextBox1_TextChanged(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If TextBox1.TextLength = TextBox1.MaxLength - 1 Then
TextBox2.Focus()
End If
End Sub
This will switch focus to textbox2 as soon as the last digit is typed.
If you want it in a button event, then do the same as above, but without the - 1
If you want to keep the text the same throughout, then do this:
Private Sub SubBttn_Click(sender As Object, e As EventArgs) Handles SubBttn.Click
If TextBox1.TextLength = TextBox1.MaxLength Then
TextBox2.Text = TextBox1.Text
TextBox2.Focus()
End If
End Sub
You would repeat the same action for each textbox, for example:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.TextLength = TextBox1.MaxLength Then
TextBox2.Text = TextBox1.Text
TextBox2.Focus()
End If
If TextBox2.TextLength = TextBox2.MaxLength Then
TextBox3.Text = TextBox2.Text
TextBox3.Focus()
End If
If TextBox3.TextLength = TextBox3.MaxLength Then
TextBox4.Text = TextBox3.Text
TextBox4.Focus()
End If
End Sub
Hope this helps!
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!!!
the string is looks like 11,33,44
i made a split into three strings into 3 textboxes, and then when i do ListBox1.Items.Remove(ListBox1.SelectedItem) it doesn't work.
it says ss.Split(",") Object reference not set to an instance of an object.
here is my code
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim ss As String = ListBox1.SelectedItem
Dim aryTextFile(2) As String
aryTextFile = ss.Split(",")
TextBox1.Text = (aryTextFile(0))
TextBox2.Text = (aryTextFile(1))
TextBox3.Text = (aryTextFile(2))
ss = String.Join(",", aryTextFile)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text + "," + TextBox2.Text + "," + TextBox3.Text)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
End Sub
When you remove an item from the ListBox by pressing the Button2, the SelectedIndexChanged of the ListBox1 is being called. There, the selected item will be nothing, so to solve this, add the following lines inside the SelectedIndexChanged event before assigning the string variable.
If ListBox1.SelectedItem Is Nothing Then
Exit Sub
End If
Try this:
listbox.selecteditem.remove()
It will remove the selected item in the listbox.