VB.net Multiple Buttons using one function - vb.net

I dynamically rename buttons and I need to perform a function based on the button text. I have working code (5 sections for each button). Is there was good way to just use a single function for all 5 buttons using DirectCast or CType or really any other function so I don't have to have multiple functions that are doing the same thing?
My code example for two of the 5 buttons:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Button1.Text = "GO" Then
MsgBox("GO")
ElseIf Button1.Text = "STOP" Then
MsgBox("STOP")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If Button2.Text = "GO" Then
MsgBox("GO")
ElseIf Button2.Text = "STOP" Then
MsgBox("STOP")
End If
End Sub
Thanks in advance!

Instead of direct reference Buton1 you can try this (Not sure about VB.NET sintax):
Dim myButton as Button
myButton=(button)sender
If myButton.Text = "GO" Then...

Related

How do I detect when any button is pressed without writing individual code for each button in VB.net?

I'm very new to coding and currently I am coding a VB.net Windows Form Hangman game. I have 26 letter buttons that when pressed I would like the text of them (A,B,C,Etc.) to be put into a Text box so the player knows what letter they have inputted and they can then submit their guess. However, so far the only way I have figured out how to detect when any of the buttons is pressed is by writing the code individually for each button which looks very messy and inefficient. I was wondering if it was possible to detect when any of the buttons is pressed (And know which one) without writing code for each individual button?
This is only for 4 of the buttons so I have to do this 22 more times for what I want to do and more for any additional buttons:
Current Code:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox2.Text = Button3.Text
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
TextBox2.Text = Button4.Text
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
TextBox2.Text = Button5.Text
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
TextBox2.Text = Button6.Text
End Sub`
This is the game when run. When any of the letter buttons are pressed I would like them to be detected. The text box on the right shows the button that is pressed:(https://hi.stack.imgur.com/92sDy.png)
I hope someone can help,
thank you in advance,
Georgitzu
Here's the basic idea ...
' assign all button click events to the same routine
Private Sub buttonHandler(sender As Object, e As EventArgs) _
Handles Button1.Click, Button1.Click .... Button26.Click
' create a generic button object to handle the button clicked
Dim obtn as Button = CType(sender, Button)
' display the text
TextBox2.Text = obtn.Text
Hope this helps,
Mike

Converting check List in VB.NET to integer

This is my first time working with VB.Net It's a college assignment and is already due for submission.I want to create a simple program that determines if a user has Ebola or not. So, there is a list of checkbox containing the core symptoms of Ebola. If the user selects 4 or more there should be a prompt message saying the user most likely has Ebola otherwise user does not have Ebola.
I have created the form and it works but don't know how to implement the checkbox into numbers that will be summed up.
Here is the code for the form
Public Class Checkbxvb
Private Sub Checkbxvb_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = "Click to select the symptoms you are having"
CheckBox1.Text = "Fever"
CheckBox2.Text = "Loss of appetite"
CheckBox3.Text = "sore throat"
CheckBox4.Text = "Gastrointestinal Symptoms"
CheckBox5.Text = "Unexplained bleeding or bruising"
CheckBox6.Text = "Loss of weight"
Button1.Text = "Submit"
Button2.Text = "Close"
End Sub
I want to create a button that will collect the user input like I said earlier on. Thanks
If you are using a CheckListBox you can used its CheckedItems collection Count property to get what you need.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If CheckedListBox1.CheckedItems.Count > 3 Then
MessageBox.Show("You may have Ebola.")
Else
MessageBox.Show("You probably don't have Ebola.")
End If
End Sub
If you are using CheckBox controls add them to a List(Of T). The T stands for Type. Create a variable for the list at Form level so it can be seen from the Form.Load and the button click. Then you can loop through your collection and increment a counter when the Checked property is True.
Private CkBxList As New List(Of CheckBox)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
CkBxList.AddRange({CheckBox1, CheckBox2, CheckBox3, CheckBox4})
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim counter As Integer
For Each cb In CkBxList
If cb.Checked Then
counter += 1
End If
Next
If counter > 3 Then
MessageBox.Show("You may have Ebola.")
Else
MessageBox.Show("You probably don't have Ebola.")
End If
End Sub
As you can see the code is much simpler with a CheckedListBox. Try to use the control that best suits your purpose.

VB.NET Trigger Datagridview Cell Click on Button CLick

I'm trying to trigger this command when the button is clicked
Private Sub ClickDataGridview(sender As Object, e As DataGridViewCellMouseEventArgs)
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow = DataGridView1.Rows(e.RowIndex)
TextBox1.Text = row.Cells(0).Value.ToString
TextBox2.Text = row.Cells(1).Value.ToString
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ClickDataGridview()
End Sub
but sadly I received two error
Argument not specified for parameter 'sender' of 'Private Sub ClickDataGridview(sender As Object, e As DataGridViewCellMouseEventArgs)'.
Argument not specified for parameter 'e' of 'Private Sub ClickDataGridview(sender As Object, e As DataGridViewCellMouseEventArgs)'
Should I make it an if statement to work? or should I try something else to trigger this event
There is a way but you have to careful about selection of cells . If you have to do only row operations then it is ok with this. I recommend don't do this instead that put button in gridview for each row and perform operation
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Try
If DataGridView1.SelectedCells.Count > 0 Then
'Here you can change Datagridview row selection property and get selectedrows instead of selected cells
Dim i_rowindex As Integer = DataGridView1.SelectedCells(0).RowIndex
Dim i_colIndex As Integer = DataGridView1.SelectedCells(0).ColumnIndex
DataGridView1_CellMouseClick(sender, New DataGridViewCellMouseEventArgs(i_colIndex, i_rowindex, 0, 0, New MouseEventArgs(MouseButtons.Left, 1, 0, 0, 0)))
End
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
As I may have mentioned once or twice, don't call event handlers directly. Put the common code in it's own method and then call that method from each event handler as appropriate. In this case:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GetRowValues(DataGridView1.CurrentRow)
End Sub
Private Sub DataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseClick
If e.Button = MouseButtons.Left AndAlso e.RowIndex >= 0 Then
GetRowValues(DataGridView1.Rows(e.RowIndex))
End If
End Sub
Private Sub GetRowValues(row As DataGridViewRow)
TextBox1.Text = row.Cells(0).Value.ToString()
TextBox2.Text = row.Cells(1).Value.ToString()
End Sub
I may be missing something, however… I do not understand why you would want to force the user to “click” a button to set the text boxes. If you wire up the grids “SelectionChanged” event and update the text boxes in that event, then the user neither has to click on a button or a cell. If the user uses the Arrow keys, Enter key, Tab key or even “clicks” a cell, the text boxes will change automatically without the user having to click a button.
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
If (DataGridView1.CurrentRow IsNot Nothing) Then
TextBox1.Text = DataGridView1.CurrentRow.Cells(0).Value.ToString()
TextBox2.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString
End If
End Sub
Or... better yet... if the grid uses a data source, then "Binding" the text boxes to that "same" data source is all that is needed. You will not have to wire up any grid events.
TextBox1.DataBindings.Add(New Binding("Text", datasource, "datasourceColumnName1"))
TextBox2.DataBindings.Add(New Binding("Text", datasource, "datasourceColumnName2"))

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

Visual Basic Multiple Button to show Mulitple Picturebox/Richtext

So here's what I want. When i click on a button it will show 1 PictureBox and 1 RichtextBox (Which is btw Visible=False so that it will not show unless clicked when the program is opened) as you can see here on my screenshot:
enter image description here
But when I click other button it doesn't change it stays with the first button i clicked when i opened the program. I really don't know the code since I'm new with VB.
Here's the code I used:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PictureBox1.Visible = True
RichTextBox1.Visible = True
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PictureBox2.Visible = True
RichTextBox2.Visible = True
End Sub
End Class
Thanks!
Since (I guess) both of the images are right on top of each other, you need to set the first image to invisible again. If not controlled which image is on top depends on the order the images where added.
You could do something like this :
Private Sub hideElements()
For i as Integer = 1 to 6
Me.Controls("PictureBox" & i).Visible = False
Me.Controls("RichTextBox" & i).Visible = False
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Call hideElements()
PictureBox1.Visible = True
RichTextBox1.Visible = True
End Sub
This loop sets all PictureBox1 - Picturebox6 and RichtextBox1 - RichTextBox6 to invisible, now you can set the one you want to show to visible.
So just call hideElements at the beginning of all of your button handler.
If u want to change the amount of images/richtextboxes, you only need to adjust the 6 in the loop.
Hope I could help.