GetAsyncKeyState not working with spacebar - vb.net

So I have a program set up so if checkbox 1 is checked, it starts me.keypreview and starts timer3. Then I have timer 3 checking if Spacebar is held down and if it is, it starts Timer1. For some reason, the code doesnt pick up that spacebar is held down and it detects left click instead, When I tried clicking left click it started timer1.
Here is my code:
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
hotkey = GetAsyncKeyState(Keys.Space)
If CBool(hotkey) = True Then
Timer1.Start()
Else
Timer1.Stop()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If CheckBox1.Checked = True Then
Me.KeyPreview = True
Timer3.Start()
Else
Me.KeyPreview = False
Timer3.Stop()
End If
End Sub
End Class
Can anybody help?

Related

Timer not stopping VB.NET

I'm making a simple Windows Forms App with Visual Studio 2019. I want to use a timer and a progress bar but i can't make the timer stop.
I've tried Timer1.Stop() and Timer1.Enabled = False but neither of them have worked. The timer waits 1 second.
Here's the full code:
(it has changed a lot but I still have the problem)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Button1.Text = "Stop" Then
ProgressBar1.Value = 0
Timer1.Enabled = False
End If
Button1.Text = "Stop"
Timer1.Enabled = True
Cursor = Cursors.AppStarting
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If Timer1.Enabled = True Then
ProgressBar1.Increment(1)
Else
ProgressBar1.Value = 0
End If
If ProgressBar1.Value = 100 Then
Timer1.Stop()
Button1.Text = "Done!"
End If
End Sub
I strongly suggest that you use a CheckBox rather than a Button. If you set the Appearance to Button then it will look just like a regular Button but you can use the Checked property to represent state. The control will appear depressed when Checked is True. You can then use code like this:
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
Timer1.Enabled = CheckBox1.Checked
CheckBox1.Text = If(CheckBox1.Checked, "Stop", "Start")
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.PerformStep()
If ProgressBar1.Value = ProgressBar1.Maximum Then
CheckBox1.Checked = False
End If
End Sub

If mouse is clicked then don't execute mouse leave event

Hi I want to show a Label for hint so if mouse hover then show Label and mouse leave then hide Label.
But if mouse click then show label and don't execute leave event, because leave event means hide mouse. So how can I perform it? My code is here.
Click Event
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
control("set")
End Sub
Hover Event
Private Sub Label2_MouseHover(sender As Object, e As EventArgs) Handles Label2.MouseHover
control("show")
End Sub
Leave Event
Private Sub Label2_MouseLeave(sender As Object, e As EventArgs) Handles Label2.MouseLeave
control("remove")
End Sub
Control Sub
Public Sub control(ByVal c As String)
If c = "set" Then
Label3.Visible = True
ElseIf c = "show" Then
Label3.Visible = True
ElseIf c = "remove" Then
Label3.Visible = False
End If
End Sub
You can remove the EventHandler when Label2 is clicked:
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
RemoveHandler Label2.MouseLeave, AddressOf Label2_MouseLeave
End Sub
Not sure what exaclty is the purpose of the control-method...but the code could be reduced to this:
Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click
RemoveHandler Label2.MouseLeave, AddressOf Label2_MouseLeave
End Sub
Private Sub Label2_MouseHover(sender As Object, e As EventArgs) Handles Label2.MouseHover
Label3.Visible = True
End Sub
Private Sub Label2_MouseLeave(sender As Object, e As EventArgs) Handles Label2.MouseLeave
Label3.Visible = False
End Sub

How to generate multiple picture boxes and control them using a timer?

My uni asked us to make a game using VB, and I really don't know much about the language.
I'm trying to make a game where balloons go up to them top of the screen and must be popped before getting there.
Public Class Form1
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If PictureBox1.Top = 0 Then
PictureBox1.Visible = False
Timer1.Enabled = False
End If
PictureBox1.Top = PictureBox1.Top - 1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 1
Timer1.Enabled = True
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
PictureBox1.Visible = False
End Sub
End Class
This is my code so far, when I click the button, the balloon starts to go up, if I click the balloon, it disappears, it also disappears if it reaches the top and the timer stops.
How can I generate more balloons and control them using that timer?
Now all you have let to do is add the functionality of adding more PictureBoxes, maybe a second timer and when you create them use an Addhandler statement to point them the the pbs_Click event that I made and add them to the List I made as well.
Public Class Form1
Private PBs As New List(Of PictureBox)
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For Each pb As PictureBox In PBs
If pb.Top = 0 Then
pb.Visible = False
Timer1.Enabled = False
Else
pb.Top = pb.Top - 1
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer1.Interval = 1000'ms
Timer1.Enabled = True
End Sub
Private Sub pbs_Click(sender As Object, e As EventArgs)
Dim pb As PictureBox = DirectCast(sender, PictureBox)
PBs.Remove(pb)
End Sub
Private Sub makeNewPB()
Dim pb As New PictureBox
Addhandler pb.Click, AddressOf pbs_Click
'don't forget to make them the size you need
PBs.Add(pb)
End Sub
End Class

How to break a loop with a single button click

I am using VB.NET and I have:
a Start button
a Stop button
a While loop
When the Start button is pressed, the While loop begins. I want to stop the loop when the Stop button is pressed.
I had tried to use Applications.DoEvents, but the loop stops when the Stop button is pressed twice.
Below is my code using Applications.DoEvents
Dim stopclick As Boolean = False
Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
btnForward.Enabled = False
btnStop.Enabled = True
While
' Perform the while statements
If stopclick = True Then
stopclick = False
Application.DoEvents()
Exit While
End If
End While
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
stopClick = True
End Sub
Have I used Application.DoEvents incorrectly?
What are the other options besides Application.DoEvents? How can I stop the loop with a single Stop button click?
You need to put the call to Application.DoEvents outside of your If statement, like this:
While True
Application.DoEvents()
' Perform the while statements
If stopclick Then
stopclick = False
Exit While
End If
End While
The Stop_Click won't have a chance to be processed until you call DoEvents, so, with the way you had it, stopClick would never get set to True.
However, the larger issue, here, is that calling DoEvents, at all, is very bad practice and can lead to some very difficult-to-fix bugs if you aren't very careful. It would be far better if the loop was performed in a background thread. Check out the BackgroundWorker component for an easy to implement threading mechanism for WinForm projects. You'll find it in the tool box of your form designer.
Here's an example of how to do it with the BackgroundWorker component:
Dim stopclick As Boolean = False
Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
btnForward.Enabled = False
btnStop.Enabled = True
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
stopClick = True
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
stopClick = False
While Not stopClick
' Perform the while statements
End While
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
btnForward.Enabled = True
btnStop.Enabled = False
End Sub
You can simplify your while loop significantly by using stopClick as your condition:
While Not stopClick
Application.DoEvents()
End While
I know this is an old thread, but I found a simple workaround. Add a CheckBox to the form (it can even be "off the side of the form," if that made sense).
Private Sub btnStopLoop_Click(sender As System.Object, e As System.EventArgs) Handles btnStopLoop.Click
chkEndLoop.CheckState = CheckState.Checked
End Sub
Then, in the Loop you wish to exit:
Do Until chkEndLoop.CheckState = CheckState.Checked
'Do Stuff Here
Loop
As long as the CheckBox.CheckState = CheckState.Unchecked, the Loop will continue, but when the CheckBox becomes Checked, the Loop will Exit
You have to use btnstop.focus() at the start of the loop. This will be sure to solve your problem.
Example:
Dim stopclick As Boolean = False
Private Sub btnPlay_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPlay.Click
btnForward.Enabled = False
btnStop.Enabled = True
btnStop.Focus() ' new code---------------------
While
' Perform the while statements
If stopclick = True Then
stopclick = False
Application.DoEvents()
Exit While
End If
End While
End Sub
Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
stopClick = True
End Sub

Enable and disable TextBox with a checkbox in visual basic

I've 6 TextBox and 6 CheckBox. Now I want to disable the TextBox1 with a CheckBox1 and reactivate it with the Same CheckBox.
How a can do it?
Edit1 15.55 14/02/2013
I have done so to solve my problem!
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = False
ElseIf CheckBox1.Checked = False Then
TextBox1.Enabled = True
End If
End Sub `
This will work, just add more for the other check boxes
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = True
Else
TextBox1.Enabled = False
End If
End Sub
What this does: if checkbox1 is check, the checked_changed event fires and the code inside is ran. The if statement looks to see if the checkbox is checked or not. If it is checked, then it sets the textbox1 to enabled, if not it sets it to disabled. Be sure to set the enabled property to either enabled or disabled when you create your program. If you want it to be enabled from the start, that is the default....otherwise set it to disabled in its properties view.
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
TextBox1.Enabled = CheckBox1.Checked
End Sub
Take a look at this tutorial below. After, look at the checkbox control's events and choose the most fitting one. The property you will be changing on the textbox is Enabled.
http://www.youtube.com/watch?v=4PbUryXqZ50
This works if you have a layer built in that you can send objects behind (therefore hide things). I use this as a way to make text boxes and other items appear and disappear depending on other selections.
Private Sub checkbox_Click()
If (checkbox = True) Then
ActiveSheet.Shapes("textbox").ZOrder msoSendToFront
ActiveSheet.Shapes("textbox").ZOrder msoSendToFront
Else
ActiveSheet.Shapes("textbox").ZOrder msoSendToBack
ActiveSheet.Shapes("textbox").ZOrder msoSendToBack
End If
End Sub
This worked for me:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Enabled = False
If Not TextBox1.Enabled Then
TextBox1.BackColor = Color.White
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
TextBox1.Enabled = True
Else
TextBox1.Enabled = False
End If
End Sub
End Class