Why is my basic default .acceptbutton is not working? - vb.net

What I have:
I have two group boxes each with a text box inside. A third text box is placed outside both of the group boxes.
Button 1 is the default accept button on form load.
What I need:
When button 1 is clicked (or enter key is pressed), I need button 2 to become the default accept button.
My problem:
Button 3 becomes the default accept button rather than button 2 in spite of my code.
My code:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
GroupBox1.Enabled = True
GroupBox2.Enabled = False
Me.AcceptButton = Button1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("Button 1 pressed!")
GroupBox1.Enabled = False
GroupBox2.Enabled = True
Me.AcceptButton = Button2
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
MessageBox.Show("Button 2 pressed!")
GroupBox1.Enabled = True
GroupBox2.Enabled = False
Me.AcceptButton = Button1
End Sub
End Class

the problem is after you press button 1 button 3 gets focus.
you could fix it by adding code to focus to the button you need in the button 1 click event. "Button2.Focus()" etc..
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("Button 1 pressed!")
GroupBox1.Enabled = False
GroupBox2.Enabled = True
Me.AcceptButton = Button2
Button2.Focus()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
MessageBox.Show("Button 2 pressed!")
GroupBox1.Enabled = True
GroupBox2.Enabled = False
Me.AcceptButton = Button1
Button1.Focus()
End Sub

Related

How can I use the VB.NET Key press

I created a mouse position program that can be used to save your mouse position {X, Y}
I realised that this is not going to be effective unless I implement a method where for example pressing "5" will save that position
The only way i can save the position is by pressing the button, although that does work, there is no way to save the position without clicking the btn.
Can anyone help? I would be very grateful
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub XYbtn_Click(sender As Object, e As EventArgs) Handles XYbtn.Click
Dim mousep As Point = MousePosition
MouseXY.Text = mousep.ToString()
TimeCo.Start()
End Sub
Private Sub clearBtn_Click(sender As Object, e As EventArgs) Handles clearBtn.Click
LabelX.Text = "X"
LabelY.Text = "Y"
X2.Text = "X2"
Y2.Text = "Y2"
End Sub
Private Sub TimeCo_Tick(sender As Object, e As EventArgs) Handles TimeCo.Tick
Dim mousep As Point = MousePosition
MouseXY.Text = mousep.ToString()
End Sub
Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click
LabelX.Text = Cursor.Position.X
LabelY.Text = Cursor.Position.Y
End Sub
Private Sub save2_Click(sender As Object, e As EventArgs) Handles save2.Click
X2.Text = Cursor.Position.X
Y2.Text = Cursor.Position.Y
End Sub
Private Sub startBtn_Click(sender As Object, e As EventArgs) Handles startBtn.Click
End Sub
End Class
If your form will have focus, you can set the AcceptButton property of the FORM to saveBtn. This will make it so that when you press ENTER on the keyboard while your form has focus then that button will be pressed.
If you'd rather use the key approach then set the KeyPreview property of the Form to True and handle the KeyPress event:
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MyBase.KeyPress
If e.KeyChar = "5" Then
Console.WriteLine("5")
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

Vb. net button second click

On first click I'd like to see listbox and some buttons disabled.
On second click I'd like to see that listbox and buttons enabled again.
My current code is:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Button2.Click
lstbx_1.Enabled = False
lstbx_2.Enabled = False
outbtn.Enabled = False
Button2.Text = "remove approval"
End Sub
Try this code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
lstbx_1.Enabled = Not (lstbx_1.Enabled)
lstbx_2.Enabled = Not (lstbx_2.Enabled)
outbtn.Enabled = Not (outbtn.Enabled)
If lstbx_1.Enabled = False Then
Button2.Text = "remove approval"
Else
Button2.Text = "Your Text"
End If
End Sub

GetAsyncKeyState not working with spacebar

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?

Prevent display of MDI child title bar after a second MDI child is closed

In a Win Form MDI app, I want to have MDI child forms appear maximized within the parent container, without their own title bars. Everything works fine until I open a second child form from the first child form, and then close the second child form -- the first child form's title bar is then displayed.
I created a small test project containing three forms - frmParent, frmChild1 and frmChild2. frmParent contains a MenuStrip with options to open frmChild1, open frmChild2 and exit. FrmChild1 contains two buttons - Open frmChild2, and Close. frmChild2 contains a single button Close.
frmParent code:
Private Sub mnuChild1_Click(sender As Object, e As EventArgs) Handles mnuChild1.Click
Dim lfrmChild1 As New frmChild1
lfrmChild1.MdiParent = Me
lfrmChild1.WindowState = FormWindowState.Maximized
lfrmChild1.Show()
End Sub
Private Sub mnuChild2_Click(sender As Object, e As EventArgs) Handles mnuChild2.Click
Dim lfrmChild2 As New frmChild2
lfrmChild2.MdiParent = Me
lfrmChild2.WindowState = FormWindowState.Maximized
lfrmChild2.Show()
End Sub
Private Sub mnuExit_Click(sender As Object, e As EventArgs) Handles mnuExit.Click
Me.Close()
End Sub
frmChild1 code:
Private Sub btnCloseChild1_Click(sender As Object, e As EventArgs) Handles btnCloseChild1.Click
Me.Close()
End Sub
Private Sub btnOpenChild2_Click(sender As Object, e As EventArgs) Handles btnOpenChild2.Click
Dim lfrmChild2 As New frmChild2
lfrmChild2.MdiParent = Me.MdiParent
lfrmChild2.WindowState = FormWindowState.Maximized
lfrmChild2.Show()
End Sub
Private Sub frmChild1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.StartPosition = FormStartPosition.Manual
Me.WindowState = FormWindowState.Maximized
Me.MaximizeBox = False
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
End Sub
frmChild2 code:
Private Sub btnCloseChild2_Click(sender As Object, e As EventArgs) Handles btnCloseChild2.Click
Me.Close()
End Sub
Private Sub frmChild2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.StartPosition = FormStartPosition.Manual
Me.WindowState = FormWindowState.Maximized
Me.MaximizeBox = False
End Sub
How can I prevent frmChild1's title bar from being displayed after frmChild2 is closed, having been opened from frmChild1?