Check for certain text in multiple TexBoxes - vb.net

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

Related

how can i show specific values in the combobox based on the checkbox state

I want to show related values in the combo box when the user selects the checkboxes.
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If CheckBox1.Checked = True Then
ComboBox3.GetItemText("10")
End If
If CheckBox2.Checked = True Then
ComboBox3.Items.Add("20")
End If
End Sub
Any code suggestion?
On the CheckedChanged event, you may use the following code:
Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
If sender.Text = "Select 10" Then
ComboBox1.SelectedItem = "10"
Else
ComboBox1.SelectedItem = "20"
End If
End Sub
This is coded only for two CheckBoxes. The ComboBox1.SelectedItem tries to find if there's an option given in the string available in the ComboBox control. An example output is given:
Hope it helps!

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

How to clear Texboxes attached with message boxes in vb.net

For Learning the coding I built this application. It has Three text boxes.1 and 2 to enter numbers and 3 is to sum of 1 and 2. Button 2 is to get sum and 1 to clear textboxes.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text >= 101 Then
MsgBox("Enter numbers between 0 and 100")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox3.Text = Int(TextBox1.Text) + Int(TextBox2.Text)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
End Sub
End Class
Here I have limited the TextBox1 to get numbers between 0 and 100. if it is over 100, displays a warning massage box.
When I click the clear button it gives an error. It doesn't clear. after deleting textbox 1 clearing code it works fine. I mean textbox 2 & 3 clear fine. There is a problem with Textbox 1. The reason i believe is the msgbox attached it. I need to keep Msgbox.
How do I clear them?
You need to suppress text changed event. See TextBoxBase.Clear Method
Public Class Form1
Private flag As Boolean
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
' Check the flag to prevent code re-entry.
If flag = False Then
' Set the flag to True to prevent re-entry of the code below.
If TextBox1.Text >= 101 Then
MsgBox("Enter numbers between 0 and 100")
End If
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox3.Text = Int(TextBox1.Text) + Int(TextBox2.Text)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
flag = True
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
flag = False
End Sub
End Class
TextBox1.Clear() will fire TextBox1_TextChanged().
This line will do an implicit conversion from text to integer - it will fail with an error on blank or text entries:
If TextBox1.Text >= 101 Then
Instead, try this:
Dim number As Integer
If Int32.TryParse(TextBox1.Text, number) Then
If number >= 101 Then
MsgBox("Enter numbers between 0 and 100")
End If
End If

vb.net Find and REMOVE a line in a textbox

I'm very frustrated trying to get my code to work.
I'm trying to have a selected item in a listbox removed also in the textbox.
Getting ready to remove text;
Removed the text;
But it's still in the textbox.
Here is my code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Add(TextBox1.Text)
TextBox2.Text += TextBox1.Text & vbNewLine
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim filenames As String = "C:\log\log.txt"
My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filenames As String = "C:\log\log.txt"
If My.Computer.FileSystem.FileExists(filenames) Then
TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
Dim items()
items = TextBox2.Lines()
For Each item In items
ListBox1.Items.Add(item)
Next
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Clipboard.SetText(ListBox1.SelectedItem)
End Sub
End Class
The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null'
It happened every single time.
Please, before you mash the -1 button, at least tell me why. I'm new to this.
This should work for you
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub
End Class

TextBox Highlighted Text Visual Basic

I have a problem with my TextBox. I'm trying to make a small program like memo. When you write something in the textbox it saves it for next time for you to open it again. But the problem is when you open the program once again, the old text is highlighted. So how I remove that?
Public Class Form1
Private Sub
Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
My.Settings.memo = TextBox1.Text My.Settings.Save()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = My.Settings.memo + Environment.NewLine
End Sub
End Class
See the TextBox.SelectionStart property