How do I detect when any button is pressed without writing individual code for each button in VB.net? - 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

Related

How to make one-click buttons?

I have several buttons in which all of them have come codes. I want the user to only can make click the buttons once. How can I do it without having a long disable buttons code?
Dim oneClick As Integer
Private Sub btnPro_Click(sender As Object, e As EventArgs) Handles btnPro.Click
oneClick += 1
If oneClick = 1 Then
Dim ucP As New ucPro
fillMenu(ucP)
End If
End Sub
This is the code I come up with.
EDIT : The point is that I want to click a button once, but the others can be clicked. For example, I have a form with 6 buttons, every button has some codes. If I click Button1, it'll do such code only once, therefore if I click it again, it will not do that code, because it already did it. Then, if I click Button2, it'll do the code once. And what about if I click Button1 again? well, it'll be able to do that code because I clicked to another button.
Sorry for the way I explain myself. I hope you get it.
Keep track of last clicked button and you can use one handler for all involved buttons
Private _lastClickedButton As Button = Nothing
Private Sub Button_Click(sender As Object, e As EventArgs)
Dim button = DirectCast(sender, Button)
If button Is _lastClickedButton Then Exit Sub
_lastClickedButton = button
' Now based on button instance you can execute corresponding logic/method
End Sub
Based on your rather poor explanation, it sounds like what you actually mean is that you only want each button to act once in a row, i.e. once you click a button you need to click another button before the first button does anything again. In that case, the proper way to handle this is to not use Button controls.
Instead of using Button controls and handling their Click events, you should be using RadioButton controls and handling their CheckedChanged events. You can set the Appearance property to Button and they will look just like regular Buttons. When checked, they will appear depressed and that will indicate to the user that they can't be used again.
Here's an example of a form using such controls:
And here's what the appropriate code might look like:
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
If RadioButton1.Checked Then
'Do something.
End If
End Sub
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
If RadioButton1.Checked Then
'Do something else.
End If
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
If RadioButton1.Checked Then
'Do yet something else.
End If
End Sub
Here's what the form might look like when a RadioButton has been checked:
As you can see, it's nice and clear to the user which one they cannot use.
EDIT:
If you are really determined to use Button controls then you absolutely should be disabling them as that is the standard way that feedback has been provided to the user for decades. It's very simple to disable a Button if and only if it is the last one clicked. Here's an example:
Private Sub AllButtons_Click(sender As Object, e As EventArgs) Handles Button3.Click,
Button2.Click,
Button1.Click
For Each btn In Controls.OfType(Of Button)
btn.Enabled = (btn IsNot sender)
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Do something.
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Do something else.
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Do yet something else.
End Sub
Note that you can handle multiple events with a single method and multiple methods can handle a single event, both of which are demonstrated here. All four of those methods were generated automatically by the designer, so I just had to add the body code. Note that the first method assumes that all Button controls on the form will be treated this way. If that's not the case then would need a slight adjustment but nothing too major.

VB.NET simulate button click and how do I return it to normal?

I have difficulty with this code, I explain:
I have created a textbox and when I press enter, it simulates clicking on a button.
Sub TextBox1KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.Enter Then
'simulate to press Button
Button1Click(sender,New EventArgs)
Else
'code to return to normal
End If
End Sub
It works fine, but how do I return it to normal? I mean, when I click the button, it does not react.
Please wait for your help with this problem that arises
It is usually not a good idea to call an event procedure. Move your code from the Button1.Click to a separate sub then call it from both places.
Private Sub TextBox1_KeyDown_1(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
'simulate to press Button
MyButtonSub()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MyButtonSub()
End Sub
Private Sub MyButtonSub()
MessageBox.Show("Button One was clicked or pretend clicked.")
End Sub

VB.net Multiple Buttons using one function

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...

Disable sounds when pressed enter key to append text on richtextbox

I have this in my design form.
My codes are the follwoing
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RichTextBox1.AppendText(TextBox1.Text)
End Sub
Private Sub TextBox1_Keyup(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
If e.KeyCode = Keys.Enter Then
Call Button2_Click(sender, e)
End If
End Sub
The problem is when I click on textbox, typed something in it then pressed enter I will hear a beeping sound. That sound is the one that I want to disable.
I also notice that if I just typed in the textbox and click the button I can hear no sound, the sound occurs whenever I clicked on textbox, type something in it and pressed enter.
EDIT:
By conducting a thorough research, I realized that the ding sounds doesn't come from button pressed but from the last line of richtextbox. It is the same sound that we can heard when we pressed key_down and we are in the last line of a richtextbox. How do I disable it?
Just set the AcceptButton() property of the Form to Button2. Then you don't need the KeyUp code at all:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.AcceptButton = Button2
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
RichTextBox1.AppendText(TextBox1.Text)
End Sub
' Don't need this code anymore:
'Private Sub TextBox1_Keyup(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
' If e.KeyCode = Keys.Enter Then
' Call Button2_Click(sender, e)
' End If
'End Sub

Trying to enter a name in a textbox, hit ok, and have that form close and pass the info to another forms label

***THE CODE FROM THE FIRST WINDOW GOES INTO A TEXT BOX AND YOU HIT THE BUTTON HERE.
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Me.Close()
End Sub
***THE CODE ON THE FORM WHERE I WANT THE INFO TO BE PLACED IS HERE.
Private Sub Loan1Form_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.lblCompanyName.Text = DataEntryForm.txtCompanyNameInput.Text
End Sub
Anyway's that's what I found on a youtube video and im having trouble getting it to work. Any help would be appreciated.
Thanks.
Pass the data to an instance of the form:
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Dim f As New OtherForm
f.lblCompanyName.Text = txtCompanyNameInput.Text
f.Show()
Me.Close() 'make sure this form is not the one that closes the app if it closes
End Sub