How to clear Texboxes attached with message boxes in vb.net - 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

Related

Check for certain text in multiple TexBoxes

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

Input Strings into String Arrays(Dynamic)

So I have two string arrays, one for my friends, the other for their numbers. I go to my combo box (displays list of names) I click on it and it displays their number on a label. My problem is I want the user to a enter a new name and number in 2 textboxes, then transfer the name on the combo box. Once their name is in the box, I click on it and it display's their number on label. Is there a way to transfer the new items onto my arrays?
Option Explicit On
Module MainModule
Public strPeople() As String = {"Kyle", "John", "Jake", "Donna", "Carly", "Ty", "Mavis"}
Public strPhoneNumbers() As String = {"945-1232", "804-2329", "290-7321", "928-4569", "205-9893", "320-0195", "305-4520"}
Public tempList As New List(Of String)
End Module
Here Is My Main Form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange(strPeople)
End Sub
Private Sub AboutToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem1.Click
AboutBox1.ShowDialog()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim strPhoneNums As String = strPhoneNumbers(ComboBox1.SelectedIndex)
Label3.Text = "Phone Number: " & strPhoneNums
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
//Add Contact Button
If TextBox1.Text <> "" Then
ReDim Preserve strPeople(7)
strPeople(7) = TextBox1.Text
ComboBox1.Items.Add(strPeople(7))
End If
If TextBox2.Text <> "" Then
ReDim Preserve strPhoneNumbers(7)
strPhoneNumbers(7) = TextBox2.Text
End If
TextBox1.Clear()
TextBox2.Clear()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub

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

Visual Studio Button Event

I would like to code a button that after 3 clicks, links the user to my site. The first 2 should generate a code in the textbox and the last one should then link them. This is what i have so far
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(3)
If ProgressBar1.Value = 100 Then
TextBox1.Text = "Thank you"
Timer1.Stop()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Then
Timer1.Start()
ElseIf RadioButton2.checked = True Then
Timer1.Start()
Else
TextBox1.Text = "Please Select Option"
End If
End Sub
I dont know what is the use of ProgressBar and Timer. But if you wan to know how many times user has click on a button, a counter variable should do.
Private _clickCounter As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
_clickCounter += 1
If _clickCounter = 3 Then
MsgBox("3 times")
_clickCounter = 0
'link to your site
Else
'generate code in textbox
End If
End Sub
Use an integer that increases each time the button is clicked and a Select Case statement to detect that;
Dim NumberOfClicks As Integer = 0
Dim webAddress As String = "http://www.YourWebsite.com/"
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
number = number + 1
Select Case number
Case 1
''''' Code for your textbox
Case 2
''''' Code for your textbox
Case 3
Process.Start(webAddress)
End Sub
This should do. Please mark my answer as solved if it helps.

Error Message TryParse

I am struggling with my TryParse method. The program is to calculate the total rainfall and average rainfall. User enters each month in inches. The totals are accurate, but the error message "enter numeric value" pops up when you click calculate. The program also calculates with a symbol or letter, how would i go about stopping it at that point.
Public Class Korszun
Private Sub Korszun_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Please enter each month's rainfall average (ex: 1.11 inches)")
End Sub
Private Sub txtJuly_TextChanged(sender As Object, e As EventArgs) Handles txtJuly.TextChanged
End Sub
Private Sub txtOctober_TextChanged(sender As Object, e As EventArgs) Handles txtOctober.TextChanged
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'declare the variables
Dim intCount As Integer = 0
Dim dblAverageRainfall As Double
Dim dblTotalRainfall As Double
Dim dblInches As Double
'create txtbox variable
'go through each TextBox control in the Groupbox and add them all up
For Each txtBox As TextBox In Me.GroupBox1.Controls.OfType(Of TextBox)()
If Not Double.TryParse(txtBox.Text, dblInches) Then
MessageBox.Show("Please enter numerical values only.")
ElseIf CDbl(txtBox.Text) < 0 Then
MessageBox.Show("Please enter all positive numbers.")
ElseIf TypeOf txtBox Is TextBox Then
'add the total value of rainfall and put it in the decTotalRainfall variable
dblTotalRainfall += CDbl(txtBox.Text)
End If
Next
'get the average
dblAverageRainfall = dblTotalRainfall / 12
'show the results in the labels
lblTotal.Text = dblTotalRainfall.ToString("n2")
lblAverage.Text = dblAverageRainfall.ToString("n5")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'Clear the Textboxes
txtJanuary.Clear()
txtFebruary.Clear()
txtMarch.Clear()
txtApril.Clear()
txtMay.Clear()
txtJune.Clear()
txtJuly.Clear()
txtAugust.Clear()
txtSeptember.Clear()
txtOctober.Clear()
txtNovember.Clear()
txtDecember.Clear()
lblAverage.Clear()
lblTotal.Clear()
'Set focus
txtJanuary.Focus()
End Sub
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub txtAverage_TextChanged(sender As Object, e As EventArgs) Handles lblAverage.TextChanged
End Sub
End Class