Changing a button's text after an event - vb.net

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)

Related

Trigger "OK" on login by using Enter [duplicate]

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

Have to Click Form To Give Focus

I have a main form with buttons that open a custom message box form. It works fine if it's just a message and the user just needs to click OK. But if the answer to that message box is important, like "Are you sure you want to delete this file?" I use a while loop to wait for the user to respond and once they do then a flag is set from false to true and the response is recorded.
For some reason any response that uses a while loop to wait is causing the message box form to not have focus after being called. Requiring the user to first click on the form, and then click on OK.
So far I've tried using form.Activate() instead of form.Show(), as well as calling Application.DoEvents() inside the while loop since I believed the while loop was taking focus away from the message form immediately after being called. Neither solved the issue.
Code from a message box that works as intended:
If cmbLoadProgram.SelectedItem = "" Then
frmMessageBox.lblHeader.Text = "Set-Up"
frmMessageBox.lblMessageText.Text = "No Program Selected!"
frmMessageBox.Show()
Exit Sub
End If
Code from a message box that needs to be clicked twice:
If btnGetHexStart.Visible = False And cmbStartCondition.SelectedItem = "Pixel" Then
frmMessageBox.lblHeader.Text = "Hex Set-Up"
frmMessageBox.lblMessageText.Text = "Reset Hex Code Data?"
frmMessageBox.Show()
Me.Hide()
While Flag = False
If frmMain.OKCancel = "OK" Then
btnGetHexStart.Visible = True
btnGetHexStart.Enabled = True
btnGetHexStart.PerformClick()
Flag = True
End If
frmMain.delay(20)
End While
End If
I'm wanting both options to only need to be clicked once in order to confirm or cancel the action. Instead of the while loop questions needing to be clicked twice.
This just an idea from me, just how to open msgboxform, here we need MessageForm and one module1, for example:
Public Class MessageForm
'You can assign any variable to show any data in messageform display
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
theResult = MsgBoxResult.Ok
Me.Close()
Me.Dispose()
End Sub
Private Sub bttcancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttcancel.Click
theResult = MsgBoxResult.Cancel
Me.Close()
Me.Dispose()
End Sub
End Class
Module Module1
Public theResult As MsgBoxResult
'You can add some parameter here to submit to MessageForm
Public Function myMessageBox() As MsgBoxResult
myMessageBox = MsgBoxResult.Cancel
MessageForm.ShowDialog()
myMessageBox = theResult
End Function
End Module
and then you can call the myMessageBox anywhere like this:
'if myMessageBox procedure have parameter, apply the paramters too
dim myRslt = myMessageBox()
You don't need to create a form,You can use DialogResult and MessageBox.Show, code:
Dim Result As DialogResult = MessageBox.Show("Set-Up" & vbCrLf & "No Program Selected!", "Warning", MessageBoxButtons.OKCancel)
If Result = DialogResult.OK Then
ElseIf Result = DialogResult.Cancel Then
End If

Log Out message box appear twice when click on logout button

So I would like to perform a confirmation before i close the application or logout the application by clicking the logout button or just click to close the application. If i directly close the application then the message box just appear once. However when i use the logout button then the message box appear twice.
So the coding is basically look like this:
Private Sub btnLogOut_Click(sender As Object, e As EventArgs) Handles btnLogOut.Click
If logOut() Then
Me.Dispose()
frmLogIn.Show()
End If
End Sub
Private Sub frmHome_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If logOut() Then
Me.Dispose()
frmLogIn.Show()
Else
e.Cancel = True
End If
End Sub
Public Function logOut() As Boolean
Dim respond = MessageBox.Show("Are you sure you want to log out?", "Log Out", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If respond = DialogResult.Yes Then
blnResult = True
Else
blnResult = False
End If
logOut = blnResult
End Function
So anyone could help?
In your logout button event click
If logOut() Then //first line
Me.Dispose()//second line
frmLogIn.Show()//third line
End If//fourth line
in your second line which is Me.Dispose, you disposing the form, in other term you CLOSING it. So that, Your form_close event will be trigger because you dispose your form. That's why the message box pop up twice.

Start a process in a VB form without clicking a button

Here is my situation.
I have a windows from application in VB.NET. In the my first form there are two checkboxes. If the one is checked it goes to Form2, if only the second one is checked it goes to Form3. If both are checked then i want first to go on Form2 and when this process is finished go to Form3. All good until this point.
However, I want Form3 to start executing its code (which is inside a background worker) without pressing any button (which is the case when only the second checkbox is checked). Is this possible?
Thanks In advance!
Extra info....
To navigate between the Forms I use a Show/hide/Close scheme. For example:
If CheckBoxTrain.CheckState = CheckState.Checked Then
Me.Hide()
Form2.Show()
ElseIf CheckBoxTrain.CheckState = CheckState.Unchecked And CheckBoxEvaluate.CheckState = CheckState.Checked Then
Me.Hide()
Form3.Show()
At the end of the code in form2 i check if the second checkbox is also checked:
If Form1.CheckBoxTrain.CheckState = CheckState.Checked And Form1.CheckBoxEvaluate.CheckState = CheckState.Checked Then
Form3.Show()
Me.Hide()
End If
Then in Form3 there is a button in which after pressing it i have all the code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
For i = 1 To n_monte
Button1.Enabled = False
Button2.Enabled = True
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
Next
End Sub
All i want is for this last piece of code is to start running without pressing any buttons (only for the case in which both checkboxes are checked)
Declare a Boolean property in Form3 and set that property in Form1 based on whether or not both CheckBoxes are checked. In the Load event handler of Form3, start the BackgroundWorker or don't based on the value of that property.

How do I make it so that when a variable is true in "Form1," it does something in "Form2?"

So, I'm trying to make it so when the user clicks the login button on the login form, it sets a variable to true, then it makes something activate on the main form.
Some pseudo code that would be placed in Form 2:
If Form1.button = clicked Then
do something
end if
Thanks for any help!
By the way, I'm a complete noob with vb.net. Sorry.
On button click event write below
If Form1.button = clicked Then
Dim myForm As New Form2
if LoggedIn = True then
myForm.UserGroupBox.visible = true
me.close
myForm.Show()
end if
End if
Double click your Application name in the solution explorer then go to settings and create a new one with the name: clicked and the value on : 0 and write on the button1 in the form1
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
My.Settings.clicked = "1"
form1.show
me.hide
End Sub
and on the form were you want the something
if my.settings.clicked = "1" then
'Do Something what you want here
my.settings.clicked = "0"
end if
end sub
what this code does is:
when you click then button if gives the my.settings.clickde an value 1 = true
and when the other form loads its gonna check if the value = 1 if the value = 1 then it clicks the button and then changes the clicked value back to 0
so its not gonna repeat forever
hope this helps you and dont forget if it helps click button_up on my post