Trigger "OK" on login by using Enter [duplicate] - vb.net

This question already has answers here:
OK-Cancel Dialog: handling the 'Enter' key press
(3 answers)
Closed 2 years ago.
I want to be able to press Enter on my keyboard for the "OK" button to activate on the login screen here.
At the moment when I press enter, the entered data on the textbox disappears.
I have the code all working for the "OK" button to be clicked but I would prefer if there was the availability of also pressing Enter. I have looked on other forums and haven't found the answer so any help is greatly appreciated.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnOK.Click
Dim a As Integer = 0
Dim attempt As Integer = 0
'Added Username and Password settings for the Form "Login"
If txtUsername.Text = My.Settings.Username Then
a += 1
End If
If txtPassword.Text = My.Settings.Password Then
a += 1
End If
If a = 2 Then
'Login Successful message displayed if correct details are entered
MsgBox("Login Successful", MsgBoxStyle.Information)
Me.Hide()
frmCustomerDetails.Show()
Else
'Login Unsuccessful message displayed if incorrect details are entered
'Username and Password Text Fields are cleared to allow user to easily re-enter information
Static cntAttempts = 0
cntAttempts += 1
If cntAttempts = 3 Then
MsgBox("Login Failed Too Many Times: Exiting Application", MsgBoxStyle.Critical)
Me.Close()
End If
MsgBox("Username or Password Incorrect", MsgBoxStyle.Exclamation)
txtUsername.Clear()
txtPassword.Clear()
End If
End Sub

In the forms designer select the form. In the properties window under "Misc" select your OK button as AcceptButton and select your Cancel button as CancelButton.
This will make the OK button the default button with a bold border. It will be activated when you press Enter unless another control with a default function attached to Enter has the focus (like another button).
When you press Esc this will activate the Cancel button.

On my login my password textbox is named "TxtPass" on the keydown I have this code:
Private Sub TxtPass_KeyDown(sender As Object, e As KeyEventArgs) _
Handles txtPass.KeyDown
If e.KeyCode = Keys.Enter Then
Call CmdLogin_Click(sender, e)
End If
End Sub
Usually a user will click on the button named CmdLogin but if instead he want to use the Enter key, it works as shown

Related

vb .net check if the only open window is current window

i'm working on a project that has several windows that the user opens AFTER clicking on a "start" button. i need to restrict the user by showing an error message if the "start" button is clicked WHEN other windows are still open.
here's what i got for the main window with the "start" button:
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Dim openForms As New FormCollection()
openForms = Application.OpenForms()
If openForms.Count > 1 Then
MessageBox.Show("ERROR MESSAGE", "Errors", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
'some codes here
End If
End Sub
the problem with the above code, is that not all windows are "disposed" properly, hence it remains in an "open" state even after they are closed. and i am not in the position to edit those other windows. any help would be appreciated.
You can try it like this:
If Application.OpenForms().OfType(Of Form2).Any Then
MessageBox.Show("Opened")
Else
'Else
End If

COINS.PH login panel from vb.net winform

I want to create a login form for coins.ph but the result isn't what I expected, once I clicked the Sign In button from the form, everything in the username and password textbox will be written in the webbrowser but it wont click the sign in button from the webbrowser. But when I tried clicking the sign in button from the webbrowser, it says that no data were written.
Heres my code:
Private Sub BunifuThinButton21_Click(sender As Object, e As EventArgs) Handles BunifuThinButton21.Click
For Each username As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
If username.OuterHtml.Contains("name=""username""") Then
username.SetAttribute("value", BunifuMetroTextbox1.Text)
Exit For
End If
Next
For Each password As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input")
If password.OuterHtml.Contains("name=""password""") Then
password.SetAttribute("value", BunifuMetroTextbox2.Text)
Exit For
End If
Next
For Each signIN As HtmlElement In WebBrowser1.Document.GetElementsByTagName("button")
If signIN.GetAttribute("class") = "login-button login-form__button" Then
signIN.InvokeMember("click")
Timer1.Enabled = False
Exit For
End If
Next
End Sub
Or what other approach should I use which is easier for me, they said that web page changes elements sometime. If api, theres no api for login pages.

The Ok button of MessageBox closes the form

I have a main form formA and empty form formB, formA opens formB, on the load event of formB it add a user control using Activator.CreateInstance(ControlType) .. the user control has Submit and Cancel buttons.
this is the btnOk.click event
Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
Try
If OnSubmit() = cSUCCESS Then
OnSubmitRaiseEvent(e)
Else
'
' what should I write here to Not close the form if onSubmit failed ?
'
End If
Catch ex As Exception
WriteToEventLog(ex)
End Try
End Sub
the OnSubmit() method checks for some data if valid or not, if not, it shows a message Box with OK button. The problem is that OK button on the message box closes the formB that is hosting the user control.
I want Message box to close and user can resume working on the form
How can I solve this?

Changing a button's text after an event

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Member = False Then
LoginForm1.Show()
Else <<<<(-after logged in successfully-)
Button1.Text = "Logged In" <<<(-I want to change this button text to Logged In-)
Button1.Enabled = False
End If
End Sub
So my problem is, the Button1.Text changes only works when I click on the button. If I did not click on the button, the text is still same as default "Login". I want to make the button text change instantly after the loginform1 closes. Help please and thank you
You can use LoginForm1.ShowDialog() instead. This shows the login as a modal form. This means you can not access the underlying form and the code execution in your sub stops until the modal form is closed.
So you could just use:
LoginForm1.ShowDialog()
If Member = True Then 'I guess this is how you check if the login was successful
Button1.Text = "Logged in"
Button1.Enabled = false
End if
instead of the whole If-Else-EndIf.
Somewhere in LoginForm1 when the Member has logged in you should include this code:
FORM1.Button1.Text = "Logged In"
FORM1.Button1.Enabled = False
Replace FORM1 with the name of your first form. (Form containing Button1_Click)

e.Cancel fires but other code also fires when a click event occurs

A form has many buttons. One of those buttons contains code to update the TableAdapterManager. One of the many TextBox controls has code in a Validating event handler. There is code to make sure a US phone number is formatted properly.
If the user tabs out of the TextBox the Validating code works perfectly and show a message to the user if the phone number is not properly formatted and the focus is place in the offending TextBox.
If the user clicks on a button that has code to update the TableAdapterManager the Validating code fires but instead of focus remaining on the offending TextBox, the code in the button Click handler also fires.
I would like to stop the button code from firing.
Here is the code for the Validating event of the TextBox:
Private Sub TextBoxPrimaryPhone_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBoxPrimaryPhone.Validating
' Make sure the phone is formatted correctly.
'--------------------------------------------
If PhoneFormat(TextBoxPrimaryPhone.Text) = "Fix Phone Number" Then
' Alert the user.
'----------------
MessageBox.Show("Please enter a 7 or 10 digit phone number.", _
"Entry Error", _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
e.Cancel = True
Else
' Format according to the length of the phone number entered by the user.
'------------------------------------------------------------------------
TextBoxPrimaryPhone.Text = PhoneFormat(TextBoxPrimaryPhone.Text)
End If
End Sub
What additional coding to I need to include so focus remains on the TextBox?
After e.Cancel = True is called do this to set the focus of the offending textbox:
TextBoxPrimaryPhone.Focus()
In your button click event handler you can do this:
TextBoxPrimaryPhone_Validating(sender, New System.ComponentModel.CancelEventArgs)
If Not TextBoxPrimaryPhone.Focused Then
'Do Work
End If