How to detect which event is being handled from multiple events? - vb.net

My problem:
I need to detect what event (e.g. TextChanged) is currently being handled from within the handling Sub that's handling multiple objects/events. I cannot find examples online (though this may be a terminological issue). I already know how to detect the object but I do not know how to detect the event.
To reproduce:
In Visual Studio, in design mode, drag two text boxes onto a fresh form.
Use the following as is:
Code:
Private Sub TextBox_CombinedEvents(sender As Object, e As EventArgs) Handles TextBox1.Enter,
TextBox2.Enter, TextBox1.TextChanged, TextBox2.TextChanged,
TextBox1.Leave, TextBox2.Leave
Dim detectedEvent as String = "?"
If detectedEvent = "Enter" Then
MessageBox.Show("Text has been entered.")
Elseif detectedEvent = "TextChanged" Then
MessageBox.Show("Text has been changed.")
Elseif detectedEvent = "Leave" Then
MessageBox.Show("Text box has been left.")
Else
MessageBox.Show("Event not detected.")
End If
End Sub

Related

Running a BackGroundWorker more than once

I wonder if you can help?, please bear in mid I’m an absolute novice.
I trying to update a program I made a few years ago for booking in serial numbers into a CRM application.
Currently it runs the following command for each of the 100 textboxes and has worked a treat booking in more than 81000 serial numbers.
If TextBox1.Text.Length > 1 Then
Clipboard.SetText(TextBox1.Text)
RetBat1 = Shell("C:\Windows\BookIN.exe", , True)
Endif
The new version of the app I’ve added a listbox1 with the serial numbers in, I’m then running the below For Each loop.
The For Each loop copies each Item into the clipboard and the BookIN.exe tabs to the right location in the CRM and pastes the information, then clicks a button in the CRM for a new Line and then runs again. This works fine, but I want to add a stop button or a stop checkbox.
For Each items In ListBox1.Items
Clipboard.SetText(items)
RetBat1 = Shell("C:\Windows\BookIN.exe", , True)
Next
I have tried adding the Retbat1 to a backgroundworker, which checks if Checkbox1.checked then exit the for each loop.
The first serial number works, but when the backgroundworker runs more than once I get the following error.
If CheckBox1.Checked = True Then
BackgroundWorker1.CancelAsync()
Else
Dim RetBat1 As String
RetBat1 = Shell("C:\Windows\BookIN.exe", , True)
End If
System.InvalidOperationException: 'This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.'
Sorry if this makes not sense, thanks James
The way it would go is that you would run the BackgroundWorker and have your loop in the DoWork event handler and check whether a cancellation has been requested within that loop. As you've described it, you would then handle the CheckedChanged even of your CheckBox and request the cancellation when the event is raised. I would not use a CheckBox though, because that implies that you can uncheck it to uncancel the work. I would suggest using a Button and handling its Click event, e.g.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'We must get the data from the UI here and pass it into the background thread.
Dim items = ListBox1.Items.Cast(Of String)().ToArray()
'Start the background work and pass in the data.
BackgroundWorker1.RunWorkerAsync(items)
'Enable the Cancel button.
Button2.Enabled = True
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Disable the Cancel button so it cannot used again.
Button2.Enabled = False
'Request that background processing be cancelled.
BackgroundWorker1.CancelAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
'Get the data that was passed in.
Dim items = DirectCast(e.Argument, String())
'Process each item.
For Each item In items
'Check whether processing has been cancelled.
If BackgroundWorker1.CancellationPending Then
'Cancel processing.
e.Cancel = True
Exit For
End If
'Process the current item here.
Next
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Cancelled Then
MessageBox.Show("The background operation was cancelled")
Else
'Disable the Cancel button.
Button2.Enabled = False
MessageBox.Show("The background operation completed successfully")
End If
End Sub
The Cancel Button should be disabled by default and notice that this code ensures that it is only enabled when background processing is in progress. Note that the other Button probably ought to be disabled while the processing is in progress too. If you don't do that, at least check IsBusy on the BackgroundWorker. The visual feedback to the user is better though.

How to do a backgroundworker about SAPI SPEAK

I want to create a SAPI speaking background worker in Visual Basic .NET in order to allow my client to continue doing something while listens to the SAPI talk.
I've reached that point, but the problem is if I want to reproduce another speaking, I cannot cancel the current speaking and occurs an exception.
I have the following code:
'MODULE IMPORTED IN THE MAIN WORK: argsBackgroundWorker.vb
Public Class argsBackgroundWorker
Public text_to_speak As String
End Class
Private talk As argsBackgroundWorker = New argsBackgroundWorker()
Private Sub sapitalk_background_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles sapitalk_background.DoWork
If (My.Settings.help_voice = True) Then 'PASS TRUE
Dim reproduceText
Dim text As argsBackgroundWorker = e.Argument
'I have put this code to cancel... So? :(
If sapotalk_background.CancellationPending Then Exit Sub
reproduceText = CreateObject("Sapi.spvoice")
reproduceText.speak(talk.text_to_talk)
Else
sapitalk_background.CancelAsync()
End If
End Sub
Private Sub btn_saysomething_Click(sender As Object, e As EventArgs) Handles btn_saysomething.Click
'Support in order to cancel tasks.
sapitalk_background.WorkerSupportsCancellation = True
talk.text_to_speak = "SOMETHING SOOOOOO SOO SOOOOOO LONG..."
'Cancel another text being spoken.
sapitalk_background.CancelAsync()
'Then, talk the new text.
sapitalk_background.RunWorkerAsync(talk)
End Sub
Private Sub principal_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Support in order to cancel tasks.
sapitalk_background.WorkerSupportsCancellation = True
talk.text_to_speak = "SOMETHING SOOOOOO SOO SOOOOOO LONG..."
'Cancel another text being spoken.
sapitalk_background.CancelAsync()
'Then, talk the new text.
sapitalk_background.RunWorkerAsync(talk)
End Sub
It works great as background speaking but notice that when I compile the app, it starts speaking a long text. And if I click a button to cancel the current speaking and speak another text, it fails and shows me that it is running a current background worker.
The only idea I have is
While Not sapitalk_background.IsBusy
sapitalk_background.RunWorkerAsync(talk)
End While
If it gets stuck in this loop until the voice stops then I am thinking that you cant stop the voice once its running on a bg thread.

Prevent Repetitive KeyDown Code From Running (vb.net)

If you hold down the key in a KeyDown Sub, it repeats the code until it is released.
Is there any way to prevent the code from continuously running and keep it so that it only runs once? Thanks.
You need to handle more than one key events to do that. For example
Public Class Form1
Private keyHolding As Boolean = False
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If Not keyHolding Then
Label1.Text &= "Keydown event detected "
keyHolding = True
'Place the code that you want to run only once in the key down event here...
Else
Label1.Text &= "User is holding the key down "
'Place the code that you want to run continuously in the key down event here...
End If
End Sub
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
Label1.Text &= "KeyUp event detected "
keyHolding = False
End Sub
End Class
Just keep in mind that this approach is good for standard windows forms applications. If you are developing a game, for example, then this approach will cause various problems and there are better solutions either via native API calls or some game developing framework.
Hope this helps.

Backcolor change of radiobutton, with button appearance

I'm creating a laboratory application in VS2013, where the user enters a rack number, and gets back a visual grid of which samples he/she has to weigh for a certain analysis, for that specific rack with samples.
I'm getting the information from an Oracle DB, based on the requested/send SQL string.
I've created radio buttons as a means of selecting/filter the analysis type.
I've given them the appearance of regular buttons, in the object properties.
When a certain radio buttons is clicked, I want to give them a specific color, depending on the type of analysis.
The same color will be used to mark the samples that are to be weighed in the visual grid.
When I test the program, the radio buttons do what they need to do (meaning: getting the correct info from Oracle DB), but I can't seem to manage the backcolor change of the radio buttons.
The backcolor change of the regular buttons (on visual grid) is working correctly.
I trigger the function behind all this, by Function Rbanalysistype (sender as object, e as EventArgs) Handles Rbanalysistype1.Click, Rbanalysistype2.Click, ...
Public Function RbAnaTypeClick(sender As Object, e As EventArgs) Handles RbAnaTypeAcIn.CheckedChanged
Dim SenderName As String = ""
Dim TitrType As String = ""
SenderName = CType(sender, RadioButton).Name
Select Case SenderName
Case Is = "RbAnaTypeAcIn"
TitrType = "AcIn"
'put correct Radiobuttion in GbAnaType in color
If RbAnaTypeAcIn.Checked = True Then
RbAnaTypeAcIn.BackColor = Color.Orange
End If
End Select
End Function
You're handling the wrong event for a start. Here's the sort of thing you should be doing:
Private Sub RadioButtons_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged,
RadioButton2.CheckedChanged
Dim rb = DirectCast(sender, RadioButton)
If rb.Checked Then
rb.BackColor = Color.Red
Else
rb.BackColor = Color.Green
End If
End Sub

Determine which textbox on winform has cursor in it

I am working on a Windows Form via VB.net. The form contains two textboxes and an onscreen keyboard that I have programmed myself.
How can I determine which textbox the cursor is currently in, so that the onscreen keyboard will type in the correct textbox?
The application is meant to be entirely touch based.
I'm going to guess that your onscreen keyboard is a series a buttons representing a keyboard.
Since you have two text boxes, you have to have a flag for both text boxes to indicate that they have focus, and when one has it the other doesn't.
Private Sub TextBox1_GotFocus(sender as Object, e as EventArgs) _
Handles TextBox1.GotFocus
// These flags are class level variables
textbox1HasFocus = true;
textbox2HasFocus = false;
End Sub
Private Sub TextBox2_GotFocus(sender as Object, e as EventArgs) _
Handles TextBox2.GotFocus
// These flags are class level variables
textbox2HasFocus = true;
textbox1HasFocus = false;
End Sub
For your keyboard events
Private Sub KeyBoard_Click(sender As Object, e As EventArgs) Handles ButtonA.Click, ButtonB.Click, ButtonC.Click, etc...
' I don't know how your using your keyboard, but buttonClickLetter could be determined in multiple ways. I would use either the Name of the control or even the Tag property for retrieving which letter to use
String buttonClickLetter = DirectCast(sender, Button).Tag.ToString()
If textbox1HasFocus Then
TextBox1.Text += buttonClickLetter
ElseIf textbox2HadFocus
TextBox2.Text += buttonClickLetter
End If
End Sub