Button.Click event doesn't fire after TextBox.Leave event - vb.net

I use the below code to verify if the text into a TextBox was changed and ask for saving changes:
Private TBoxTxtBefore As String = String.Empty
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Enter
Try
TBoxTxtBefore = CType(sender, TextBox).Text
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Try
If CType(sender, TextBox).Text <> TBoxTxtBefore Then
Dim SN As MsgBoxResult = MsgBox("Save changes?", vbYesNo, "Save Changes")
If SN = vbYes Then Btn_SaveChanges.PerformClick()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
But when I click a button (for example button1) while the cursor is inside TextBox1, only TextBox1.Leave event was raised.
How can I have Button?.click event raise after TextBox1.Leave event?

How can I have button1.click event raise after TextBox1.Leave event?
If TextBox1 has the focus and you click a button the leave event will fire before the click event. To test click in Texbox1, then click Button1.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Debug.WriteLine("B1C")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Debug.WriteLine("B2C")
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
Debug.WriteLine("TBL")
Button2.PerformClick()
End Sub
Try to clarify your question please.
edit: This recreates the problem I think you are having,
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Debug.WriteLine("B1C")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Debug.WriteLine("B2C")
End Sub
Private Sub TextBox1_Leave(sender As Object, e As EventArgs) Handles TextBox1.Leave
MessageBox.Show("FOO")
Debug.WriteLine("TBL")
Button2.PerformClick()
End Sub
Did some searching and found that the Message box causes pending focus events to be lost.

Thanks to dbasnett's hints I was able to get the name of the control that fired the Leave event, so to call it (if it's a button) after the Message Box was showed:
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Try
Dim FiringCtrl As Control = Me.Activecontrol
If CType(sender, TextBox).Text <> TBoxTxtBefore _
AndAlso FiringCtrl.Name <> "Btn_SaveChanges" Then
Dim SN As MsgBoxResult = MsgBox("Save changes?", vbYesNo, "Save Changes")
If SN = vbYes Then Btn_SaveChanges.PerformClick()
If TypeOf FiringCtrl Is Button Then
DirectCast(FiringCtrl, Button).PerformClick()
End If
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Anyway Plutonix showed me a better solution here

You can call multiple events one after the another. Your TextBox1_Leave event could trigger Button1_click event.
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
'Take the control to another sub. Pass the sender control and the event details as arguments
Call Button1_Click(sender,e)
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'Do your stuff here
End Sub
Hope this helps.

Related

Key Press to Close Form VB.net

So I've been working on this project to autorun on my flash drive, whenever it gets plugged it. It just displays my contact info and a little message, so I can get it returned.
Because I want people to actually read the message, the close button is disabled. You have to check a little box, and then hit the "OK" button.
Of course, I don't want to do this whenever I plug in my own flash drive. I'm trying to make a keyboard shortcut to close it automatically. I saw this post here but it didn't work for me. Here's the code from that post:
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
Application.Exit()
End If
End Sub
Any help is very appreciated. Thanks!
Also, if you think my code is sloppy and want to clean it up, here it is.
Public Class Form1
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
End
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
ButtonOK.Enabled = True
Else
ButtonOK.Enabled = False
End If
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
End
End If
End Sub
End Class
Firstly, NEVER EVER use End. That is just plain bad under any circumstances.
What you should be doing is testing the Checked value of your CheckBox in the FormClosing event handler and cancelling if and only if it's not checked. Logically, you should then check the CheckBox and call Close on the form when you detect that desired key combination.
Actually, I'd probably not call Close either, but rather call PerformClick on the Button. That way, the key combination is guaranteed to do exactly the same thing as clicking the Button.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonOK.Click
Close()
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
ButtonOK.Enabled = CheckBox1.Checked
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.KeyCode = Keys.X Then
CheckBox1.Checked = True
ButtonOK.PerformClick()
End If
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
e.Cancel = Not CheckBox1.Checked
End Sub

TextBox1.TextChanged event shows MsgBox twice

I have a problem with the TextBox1.TextChanged event.
My code :
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
MsgBox("txt was changed")
TextBox1.Clear()
End Sub
The problem is that the MsgBox is shown twice, but I want to show it just one time and clear the TextBox. How can i do that?
Two ways:
Temporarily remove the handler to prevent the event firing again:
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
MsgBox("txt was changed")
RemoveHandler TextBox1.TextChanged, AddressOf TextBox1_TextChanged
TextBox1.Clear()
AddHandler TextBox1.TextChanged, AddressOf TextBox1_TextChanged
End Sub
or
Create a field to check if the event is originating from itself:
Dim textBoxAlreadyChanging As boolean = False
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If Not textBoxAlreadyChanging Then
MsgBox("txt was changed")
textBoxAlreadyChanging = True
TextBox1.Clear()
textBoxAlreadyChanging = False
End If
End Sub

combobox multiple thread error

I have a problem with my code. I keep getting Multiple thread Error with backgroundworker, because of the combobox item display. Please look at my code below its a very simple code which I am planning to use on big scale, all I want it to do is "If item "1" selected show item "1" in label1. I can only assume that problem exists because Combobox runs in different thread....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
BackgroundWorker1.runworkerasync()
BackgroundWorker1.WorkerReportsProgress = True
Me.Cursor = Cursors.WaitCursor 'Cursor changes to wait
End Sub
Public Structure controlwithtext
Public controlname As Control
Public text As String
Public Sub New(ByVal ctrl As Control, ByVal text As String)
Me.controlname = ctrl
Me.text = text
End Sub
End Structure
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
If comboBox1.SelectedItem = "1" then
BackgroundWorker1.ReportProgress(5, New controlwithtext(Label1, ComboBox1.SelectedItem))
End If
End Sub
Private Sub SetBackgroundWorker_ProgressChanged(ByVal sender As Object,
ByVal e As ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
If TypeOf e.UserState Is controlwithtext Then
Dim cwt As controlwithtext = CType(e.UserState, controlwithtext)
cwt.controlname.Text = cwt.text
End If
End Sub
Here's an example of how to read from and write to controls from the BackgroundWorker thread:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
While True
System.Threading.Thread.Sleep(250)
Dim selection As String = Me.Invoke(Function()
If Not IsNothing(ComboBox1.SelectedItem) Then
Return ComboBox1.SelectedItem.ToString
Else
Return String.Empty
End If
End Function).ToString
If selection = "1" Then
Me.Invoke(Sub()
Label1.Text = ComboBox1.SelectedItem.ToString
End Sub)
Else
Me.Invoke(Sub()
Label1.Text = "something else"
End Sub)
End If
End While
End Sub

How can I call keyDown event by passing arguments, Winforms Vb.net

The Following is a combo box keydown event
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
If e.KeyCode = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
I would like to trigger same event from combobox_leave by passing 'enter key' I did as follows but not working, how to achieve this?
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1_KeyDown(Me, Keys.Enter)
End Sub
Why not just extract the method from the actual event?
Private Sub ComboBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyDown
performAction(e.KeyCode);
End Sub
Private Sub performAction(e as Keys)
If e = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
performAction(Keys.Enter);
End Sub
You could also use the SendKeys.Send Method
When the user leaves the Combobox (like in your example),
You could set back the Focus to the combobox
and then use SendKeys.Send("{ENTER}") to trigger the enter keydown.
much like this:
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs) Handles ComboBox1.Leave
ComboBox1.Focus()
SendKeys.Send("{ENTER}")
End Sub
However this prevents users from focusing to another component. To prevent this, you could use an if statement that if the user clicks or focuses on another component after focusing on the combobox, the user can still "leave" the combobox.
Your kind of approach is not advisable and this leads to a misunderstanding in the part of the user.
try this :
Private Sub ComboBox1_KeyDown(sender As Object, e As
keyEventArgs) Handles ComboBox1.KeyDown
Dim _KeyCode As Short
If e Is Nothing Then
_KeyCode = 13
Else
_KeyCode = Keys.Enter
End If
If _KeyCode = Keys.Enter Then
TextBox2.Text = ComboBox1.Text
TextBox2.Focus()
End If
End Sub
Private Sub ComboBox1_Leave(sender As Object, e As EventArgs)
Handles ComboBox1.Leave
Dim keypress As System.Windows.Forms.KeyPressEventArgs
ComboBox1_KeyDown(sender, keypress)
End Sub

Button Click Event Within Another

I have two buttons Start button and Stop button .I run my program by clicking on start button. I want to stop the program during the start button . But the program will not responde until the start buttun finish its job. How i can do check the stop buttun during the start. i heard about threading but i do not know how i can do it.
Private Sub Button_Start (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
//my code
//check always if the user push stop if no continue if yes go to this sub
//my code
end sub
Private Sub Button_Stop (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
stopClick = True
Dim Response As Integer
Response = MessageBox.Show("Do you really want to exit?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If Response = vbYes Then
Me.Close()
End If
End Sub
you can use threading put button1 code in a function and use the thread .
you can refer to this example
'Thread created to handle the Background process for start_function
Dim t As System.Threading.Thread
Private Sub start_function()
While True
'your code here for Eg:
Dim i As Integer
i = i + 1
End While
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
t = New System.Threading.Thread(AddressOf Me.start_function)
t.Start()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
t.Abort()
End Sub
Drag a backgroundworker component onto your form.
Imports System.ComponentModel
Public Class Form1
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Me.Text = "Busy Doing Work"
BackgroundWorker1.WorkerSupportsCancellation = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
Me.Text = "Asking to Cancel"
BackgroundWorker1.CancelAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
While Not BackgroundWorker1.CancellationPending
System.Threading.Thread.Sleep(1000)
End While
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
Me.Text = "Cancelled"
End Sub
End Class