Visual Studio Button Event - vb.net

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.

Related

the codes shows a notif 'Index was out of range. Must be non-negative and less than the size of the collection

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Cursor = Cursors.AppStarting
Dim id As Integer
Dim fx As frmItemEntry
id = DataGridView1.SelectedRows(0).Cells("id").Value
fx = New frmItemEntry(id)
Button4.PerformClick()
fx.ShowDialog()
Cursor = Cursors.Default
End Sub
try this code from a blog but, am don't know where is wrong
Obviously no rows are selected. Therefore you cannot access row 0. The best way to deal with this situation is to disable the button when no row is selected.
To do this you must handle the SelectionChanged event
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs) _
Handles DataGridView1.SelectionChanged
Button2.Enabled = DataGridView1.SelectedRows.Count > 0
End Sub
Also, disable the button by default in the Form designer. This code will enable it whenever a selection is made.
Another possibility is to do something like this:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim id As Integer
Dim fx As frmItemEntry
If DataGridView1.SelectedRows.Count > 0 Then
Cursor = Cursors.AppStarting
id = DataGridView1.SelectedRows(0).Cells("id").Value
fx = New frmItemEntry(id)
Button4.PerformClick()
fx.ShowDialog()
Cursor = Cursors.Default
Else
MessageBox.Show("Please select a row")
End If
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

I want to display a list of names(3) from a textbox(user input) into a label

This is the code I have so far but is not working properly. The textbox for the names user should input and the button show to display the names in a label in the order entered.
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim intcount, w1, w2, w3 As Integer
Dim intMax As Integer = 2
For intcount = 0 To intMax
strSurnames(intcount) = TextBox1.Text
Next
TextBox1.Clear()
End Sub
Private Sub btnShow_Click(sender As Object, e As EventArgs) Handles btnShow.Click
Dim intMax As Integer = 2
For intcount = 0 To intMax
lblShow.Text &= strSurnames(intcount)
Next
End Sub
I am guessing you are going to use only 1 textbox and click the "add button" multiple times to store the name. If this is true, you will first need to create
Dim arrayStr As New List(Of String)
Every single time you click on the add button, it will add into this array.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
arrayStr.Add(TextBox1.Text.Trim())
TextBox1.Clear()
End Sub
And to show the full name in just one label when you click on the "Show button", can do it like this
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = "" //The label text is cleared.
For i As Integer = 0 To arrayStr.Count() - 1
Label1.Text += arrayStr(i) + ", "
Next
End Sub
UPDATE - BASED ON YOUR COMMENT QUESTION
This is the updated solution based on your question. I am only going to show you with the "Sur Names" only. You can implement the "Weight" the same way.
First, create your array and declare a count as integer for the size of your array.
Dim surNameStr(20) As String
Dim count As Integer = 0
In the "Add Button", you increase the count number by 1 every time you add a new sur name. Once it reached your "maximum" number, you disable your button by BtnAdd.Enabled = False.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If count < 20 Then
surNameStr(count) = TextBox1.Text.Trim()
count = count + 1
Else
Button1.Enabled = False
Button2.Enabled = True
End If
End Sub
Then, in "Show Button", this is how you can show all the surnames stored.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label1.Text = ""
For i As Integer = 0 To 19
Label1.Text += surNameStr(i) + vbNewLine
Next
End Sub

How to fill a progress bar by clicking in VB.NET?

I'm making a game in VB.Net and I'm not familiar with the progress bar. I need something where the player need to press a button as fast as they can to fill up the progress bar and proceed to the next leve or if not fast enough then lose.I have no code for this because I don't know how to build up something like this. Any help would be grate.
Thanks
Say you have a button, Button1, and you have a progressbar, ProgressBar1.
You can add to the progressbar's value everytime you click on Button1 by using the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ProgressBar1.Value + 1 < ProgressBar1.Maximum Then
ProgressBar1.Value += 1
End If
End Sub
Now, notice the condition I wrapped the increment code with. This will make sure that the user does not surpass the maximum value allowed in the progressbar1.
Edit:
As for the rest of your program, you will need to use a timer in order to track the time.
for the proceed button, you will need to use the visible property which exists on buttons in order to hide a button until some condition is met.
Re-Edit:
Public Class Form1
Private intCurrentTime As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ProgressBar1.Value + 1 < ProgressBar1.Maximum Then
ProgressBar1.Value += 1
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If intCurrentTime = 10 Then
If ProgressBar1.Value = ProgressBar1.Maximum Then
'SHOW "Proceed" BUTTON
Else
MsgBox("You have failed!")
End If
intCurrentTime = 0
Else
intCurrentTime += 1
End If
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Start()
End Sub
End Class

Select item from ComboBox to open web links on WebBrowser?

i've been looking around on how to make a combobox list choice to access a webpage on webbrowser. For example, if i choose the first item in the combobox wich is named "Google" then i would press on the button next to it to access google on the webbrowser.
I got this code but it doesn't work, once i choose the first option, nothing happens.
If ComboBox1.SelectedIndex = 1 Then
WebBrowser1.Navigate("https://www.google.ca/?gws_rd=ssl")
End If
I seems so close, but i have no clues why it doesn't work..
Try this...
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Select Case ComboBox1.SelectedItem
Case "Please Select"
MsgBox("ERROR - No selection made in dropdown box!")
Case "Google"
WebBrowser1.Navigate("www.google.com")
Case "Microsoft"
WebBrowser1.Navigate("www.microsoft.com")
Case "Stack Overflow"
WebBrowser1.Navigate("www.stackoverflow.com")
End Select
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'
ComboBox1.Items.Add("Please Select")
ComboBox1.Items.Add("Google")
ComboBox1.Items.Add("Microsoft")
ComboBox1.Items.Add("Stack Overflow")
ComboBox1.SelectedIndex = 0
'
End Sub
Private Sub WebBrowser1_Navigating(sender As Object, e As WebBrowserNavigatingEventArgs) Handles WebBrowser1.Navigating
ProgressBar1.Visible = True
With ProgressBar1
.Minimum = 0
.Maximum = 50
.Step = 5
End With
For index As Integer = 0 To 50 Step 5
ProgressBar1.Value = index
System.Threading.Thread.Sleep(35)
Next
End Sub
End Class
Is the item that's selected first? The index is 0-based. Meaning the first item in the list is index #0. Try it with selectedindex = 0.