Set back to default radio button - vb.net

I am working with VB.net and am trying to create a Clear button.
I have everything worked out with my labels, checkboxes, and textboxes. The problem I have is working with the 3 radio buttons in a group box. I want it to default back to the default of radButton1 when pressing the Clear button. I thought that the code would just be:
radButton1.Checked = True
but that is not working. Any thoughts?
**************Adding more info***************
Here is the code that I am working with right now for the clear button.
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmWMMainForm
Dim intWMTotal As Integer
Private Sub btnWMExit_Click(sender As Object, e As EventArgs) Handles btnWMExit.Click
'closes the program
Me.Close()
End Sub
Private Sub btnWMClear_Click(sender As Object, e As EventArgs) Handles btnWMClear.Click
'Clears all information
txtWMFee.Text = String.Empty
lblWMAdditional.Text = String.Empty
lblWMDiscount.Text = String.Empty
lblWMDues.Text = String.Empty
chkWMGolf.Checked = False
chkWMRaquetball.Checked = False
chkWMTennis.Checked = False
radWMRegular.Checked = True
End Sub
Private Sub btnWMCalc_Click(sender As Object, e As EventArgs) Handles btnWMCalc.Click
Dim intWMFee As Integer
Dim intWMGolf As Integer = 25
Dim intWMTennis As Integer = 30
Dim intWMRacquetball As Integer = 20
Dim intWMMilitary As Integer = -10
Dim intWMSenior As Integer = -5
Integer.TryParse(txtWMFee.Text, intWMFee)
End Sub
Private Sub radWMMilitary_CheckedChanged(sender As Object, e As EventArgs) Handles radWMMilitary.CheckedChanged
If radWMMilitary.Checked = True Then
lblWMThankyou.Visible = True
Else
lblWMThankyou.Visible = False
End If
End Sub
Private Sub CancelKeys(sender As Object, e As KeyPressEventArgs) Handles txtWMFee.KeyPress
' allows the text box to accept only numbers and
' the Backspace key
If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso
e.KeyChar <> ControlChars.Back Then
' cancel the key
e.Handled = True
End If
End Sub
End Class

Related

The input String is not in correct format even though my variable is an integer

Private Sub shirtnum_KeyPress(sender As Object, e As KeyPressEventArgs) Handles shirtnum.KeyPress ("this is suppose to type in any number using into the textbox using your keyboard")
Dim Ch As Char = e.KeyChar
If Not Char.IsDigit(Ch) AndAlso Asc(Ch) <> 8 Then
e.Handled = True
End If
End Sub
Private Sub addshbtn_Click(sender As Object, e As EventArgs) Handles addshbtn.Click
Dim t As Integer = Integer.Parse(shirtnum.Text) ("this is to add 1 number to the textbox")
t += 1
shirtnum.Text = t
addshbtn.Enabled = True
pricetxt_TextChanged(sender, e)
End Sub
Private Sub minusshbtn_Click(sender As Object, e As EventArgs) Handles minusshbtn.Click ("this is to minus 1 number from the textbox")
Dim t As Integer = Integer.Parse(shirtnum.Text)
t += -1
If t <= 0 Then
t = 0
End If
shirtnum.Text = t
pricetxt_TextChanged(sender, e)
End Sub
Private Sub shirtnum_TextChanged(sender As Object, e As EventArgs) Handles shirtnum.TextChanged
End Sub
Private Sub pricetxt_TextChanged(sender As Object, e As EventArgs) Handles pricetxt.TextChanged
"Dim t As Integer = Integer.Parse(shirtnum.Text) -- this is where the error appears **NOTE** this this code works without this sub"
t = 1.0
pricetxt.Text = ("$" & t * 15 & ".00")
pricetxt.BorderStyle = 0
pricetxt.BackColor = Me.BackColor
End Sub
Got an issue with my assignment is that the code appears to say that the input string is not in the correct format, even though my integer is a number.
If someone can help me out with this, this would be a big help, thanks!

vb.net DataGridView how to stay in current cell when I press enter or tab key?

Now I'm making code that after I change a text in a cell and then, when I press enter key or tab key, then the currente cell stay in the same cell which I modified. I searched in here about that (Link)
But it dosen' work for me. What did I make a mistake in that?
Public row1, col1 As Integer
Public selectedPart1 As String
Private Sub DataGridView1_KeyDown(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyDown
row1 = DataGridView1.CurrentCell.RowIndex
col1 = DataGridView1.CurrentCell.ColumnIndex
selectedPart1 = DataGridView1(0, row1).Value.ToString
If e.KeyCode = Keys.Enter Or e.KeyCode = Keys.Tab Then
MsgBox("ok")
DataGridView1.CurrentCell = DataGridView1(col1, row1)
e.SuppressKeyPress = True
End If
End Sub
to disable Tab Key you can set StandardTab property to True:
DataGridView1.StandardTab = True
to disable Enter key after edit use events CellEndEdit and SelectionChanged
Private currentRow, currentCell As Integer
Private resetRow As Boolean = False
Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
If resetRow Then
resetRow = False
DataGridView1.CurrentCell = DataGridView1.Rows(currentRow).Cells(currentCell)
End If
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
resetRow = True
currentRow = e.RowIndex
currentCell = e.ColumnIndex
End Sub

VB.NET How to avoid first key press when captured it as hotkey?

I managed to make my app capture any key to use it as a hotkey, but when I press the key the hotkey function is activated. This is what I have so far:
Private Sub tmrFunc_Tick(sender As Object, e As EventArgs) Handles tmrFunc.Tick
'Function
End Sub
Private Sub tmrKey_Tick(sender As Object, e As EventArgs) Handles tmrKey.Tick
'Uses the hotkey to start and stop tmrF
End Sub
Private Sub lblCapKey_Click(sender As Object, e As EventArgs) Handles lblCapKey.Click
tmrKey.Enabled = False
txtbStartFunc.Enabled = True
txtbStartFunc.Text = "Press any key"
txtbStartFunc.Focus()
End Sub
Private Sub txtbStartFunc_KeyDown(sender As Object, e As KeyEventArgs) Handles txtbStartFunc.KeyDown
If e.KeyCode = Keys.F10 Then
txtbStartFunc.Text = "F10"
End If
tmrKey.Enabled = True
txtbStartFunc.Enabled = False
End Sub
The current code perfectly captures the key and uses it as a hotkey, the problem is that the hotkey is activated on this first key pressed and that causes it to be activated unexpectedly at the wrong time.
My current goal is that the function is not activated with the first keystroke, but is activated with the following keystrokes.
I am expecting something like this:
Private Sub txtbStartFunc_KeyDown(sender As Object, e As KeyEventArgs) Handles txtbStartFunc.KeyDown
If e.KeyCode = Keys.F10 Then
txtbStartFunc.Text = "F10"
End If
tmrKey.Enabled = True
'=========================
tmrFunc.Enabled = False
'=========================
txtbStartFunc.Enabled = False
End Sub
Here's a solution that finally worked for me. It allows you to set the hotkeys between F1 and F10, but won't let you select the same key for both of them.
This is pretty different than what you had, so take your time studying it and ask as many questions as need be:
Public Class Form1
Private Const KeyDownBit As Integer = &H8000
Private Enum HotKeyType
StartClicker
StopClicker
End Enum
Private _StartHotkey As Keys
Private _StopHotKey As Keys
Private SelectingHotKey As HotKeyType
Private HotKeySelected As Boolean = False
Private _SettingHotKey As Boolean = False
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKeys As Integer) As Short
Private Property StartHotKey As Keys
Get
Return _StartHotkey
End Get
Set(value As Keys)
If value <> StopHotKey Then
_StartHotkey = value
TextBox1.Text = StartHotKey.ToString
End If
End Set
End Property
Private Property StopHotKey As Keys
Get
Return _StopHotKey
End Get
Set(value As Keys)
If value <> StartHotKey Then
_StopHotKey = value
TextBox2.Text = StopHotKey.ToString
End If
End Set
End Property
Private Property SettingHotKey As Boolean
Get
Return _SettingHotKey
End Get
Set(value As Boolean)
If value Then
HotKeySelected = False
Timer2.Stop()
Else
Timer2.Start()
End If
_SettingHotKey = value
End Set
End Property
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
Timer1.Enabled = False
Timer2.Enabled = False
Timer1.Interval = 50
Timer2.Interval = 50
TextBox1.ReadOnly = True ' they just stay this way all the time
TextBox2.ReadOnly = True ' they just stay this way all the time
StartHotKey = Keys.F1
StopHotKey = Keys.F2
Timer2.Start()
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Dim startKeyDown, stopKeyDown As Boolean
GetAsyncKeyState(StartHotKey) ' disregard first call to "flush it"
startKeyDown = (GetAsyncKeyState(StartHotKey) And KeyDownBit) = KeyDownBit ' see if it's down
GetAsyncKeyState(StopHotKey) ' disregard first call to "flush it"
stopKeyDown = (GetAsyncKeyState(StopHotKey) And KeyDownBit) = KeyDownBit ' see if it's down
TextBox1.BackColor = If(startKeyDown, Color.Green, Control.DefaultBackColor)
TextBox2.BackColor = If(stopKeyDown, Color.Green, Control.DefaultBackColor)
If startKeyDown Then
Timer1.Start()
End If
If stopKeyDown Then ' if you hold both down, then it'll stop
Timer1.Stop()
Label1.BackColor = Control.DefaultBackColor
End If
End Sub
Private Sub BothButtons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
SelectingHotKey = If(sender Is Button1, HotKeyType.StartClicker, HotKeyType.StopClicker)
SettingHotKey = True ' automatically turns off Timer2
Dim tb As TextBox = If(sender Is Button1, TextBox1, TextBox2)
tb.Text = "Press F1 to F10"
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
Select Case keyData
Case Keys.F1 To Keys.F10
If SettingHotKey Then
If SelectingHotKey = HotKeyType.StartClicker Then
If keyData <> StopHotKey Then
StartHotKey = keyData
HotKeySelected = True
Return True
End If
Else
If keyData <> StartHotKey Then
StopHotKey = keyData
HotKeySelected = True
Return True
End If
End If
End If
End Select
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Private Sub Form1_KeyUp(sender As Object, e As KeyEventArgs) Handles Me.KeyUp
If SettingHotKey AndAlso HotKeySelected Then
SettingHotKey = False ' restarts Timer2 once selected key has been released
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.BackColor = Color.Green
Label1.Text = DateTime.Now.ToString("HH:mm:ss.ffff") ' just to show it's "clicking"
End Sub
End Class
Okay, here's a little example of my program, which shows my problem.
This is the interface:
enter image description here
When I press F1, the counter starts to add 1 to itself, when I press F2, the counter stops. The Select button changes F1 to F9 or F2 to F10, but when I change the key the function is triggered, that is, if the counter is stopped, and the key is at F1, when I click Select to change from F1 to F9, the counter is activated.
Here is an example of the code, so you can see the problem first hand:
Public Class Form1
Declare Function GetAsyncKeyState Lib "user32" (ByVal vKeys As Integer) As Short
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text += 1
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Dim a, b As Boolean
If TextBox1.Text = "F1" Then
a = GetAsyncKeyState(Keys.F1)
ElseIf TextBox1.Text = "F9" Then
a = GetAsyncKeyState(Keys.F9)
End If
If TextBox2.Text = "F2" Then
b = GetAsyncKeyState(Keys.F2)
ElseIf TextBox2.Text = "F10" Then
b = GetAsyncKeyState(Keys.F10)
End If
If a = True Then
Timer1.Start()
ElseIf b = True Then
Timer1.Stop()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Timer2.Enabled = False
TextBox1.ReadOnly = False
TextBox1.Text = "Press any key"
TextBox1.Focus()
End Sub
Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
e.SuppressKeyPress = True
If TextBox1.ReadOnly = False Then
If e.KeyCode = Keys.F9 Then
TextBox1.Text = "F9"
Else
TextBox1.Text = "F1"
End If
End If
Timer2.Enabled = True
TextBox1.ReadOnly = True
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer2.Enabled = False
TextBox2.ReadOnly = False
TextBox2.Text = "Press any key"
TextBox2.Focus()
End Sub
Private Sub TextBox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyDown
e.SuppressKeyPress = True
If TextBox2.ReadOnly = False Then
If e.KeyCode = Keys.F10 Then
TextBox2.Text = "F10"
Else
TextBox2.Text = "F2"
End If
End If
Timer2.Enabled = True
TextBox2.ReadOnly = True
End Sub
End Class
#Idle_Mind

Get selected value from DateTimePicker

I would like to get selected value from DateTimePicker in VB (If I select only day value then I would like to get only selected day value.)
In this image I have selected (blue marked) year value from this DateTimePicker. So I need only this year value.
In the case of TextBox I can get selected value using
TextEndTime.SelectedText
Is there any syntax or approach to get selected value from DateTimePicker?
As the DateTimePicker-control can be manipulated by using the arrow keys, you can use SendKeys to change the current selected value.
The following example gets the current DateTime-value of the DateTimePicker and, after sending the ↑ key, compares the value to the new value. At last it resets the DateTimePicker to the original value.
So the variable currSelected will contain the last Selection.
Dim currVal As DateTime
Dim newVal As DateTime
Dim valCheck As Boolean
Dim currSelected As Selection = Selection.None
Public Enum Selection
None = 0
Year = 1
Month = 2
Day = 3
End Enum
Private Sub CheckDTPSelection(dtp As DateTimePicker)
valCheck = True
currVal = dtp.Value
SendKeys.Send("{UP}")
End Sub
Sub RefreshSelection(dtp As DateTimePicker)
If valCheck Then
newVal = dtp.Value
If currVal.Year <> newVal.Year Then
currSelected = Selection.Year
ElseIf currVal.Month <> newVal.Month Then
currSelected = Selection.Month
ElseIf currVal.Day <> newVal.Day Then
currSelected = Selection.Day
End If
dtp.Value = currVal
valCheck = False
End If
End Sub
Private Sub MyDateTimePicker_DropDown(sender As Object, e As EventArgs) Handles MyDateTimePicker.DropDown
RemoveHandler MyDateTimePicker.MouseUp, AddressOf MyDateTimePicker_MouseUp
End Sub
Private Sub MyDateTimePicker_CloseUp(sender As Object, e As EventArgs) Handles MyDateTimePicker.CloseUp
AddHandler MyDateTimePicker.MouseUp, AddressOf MyDateTimePicker_MouseUp
CheckDTPSelection(MyDateTimePicker)
End Sub
Private Sub MyDateTimePicker_KeyUp(sender As Object, e As KeyEventArgs) Handles MyDateTimePicker.KeyUp
If e.KeyValue = Keys.Left OrElse e.KeyValue = Keys.Right Then
CheckDTPSelection(MyDateTimePicker)
End If
End Sub
Private Sub MyDateTimePicker_MouseUp(sender As Object, e As MouseEventArgs) Handles MyDateTimePicker.MouseUp
CheckDTPSelection(MyDateTimePicker)
End Sub
Private Sub MyDateTimePicker_ValueChanged(sender As Object, e As EventArgs) Handles MyDateTimePicker.ValueChanged
Dim dtp As DateTimePicker = DirectCast(sender, DateTimePicker)
RefreshSelection(dtp)
End Sub
Private Sub Btn_WhatsSelected_Click(sender As Object, e As EventArgs) Handles Btn_WhatsSelected.Click
'Show the current selected value in a MessageBox
MessageBox.Show(currSelected.ToString())
End Sub
Hi Everyone and thanks for your tips that the DateTimePicker-control can be manipulated.
I had the selection problem with DateTimePicker , the currently selected item value could not send to a text box, as DTP works only valuechanged event. I spend 4 hours time to find the solution and wrote the following code:
Public MyEventCounter As Integer = 0
Private Sub DTPAcquDt_DropDown(sender As Object, e As EventArgs) Handles DTPAcquDt.DropDown
RemoveHandler DTPAcquDt.MouseUp, AddressOf dtpacqudt_closeup
End Sub
Private Sub dtpacqudt_closeup(sender As Object, e As EventArgs) Handles DTPAcquDt.CloseUp
AddHandler DTPAcquDt.MouseUp, AddressOf dtpacqudt_closeup
'Check the Mouse/Keys event counter
If MyEventCounter > 0 Then
TxtDtAcqu.Text = DTPAcquDt.Value
'RESET The Counter
MyEventCounter = 0
End If
End Sub
Private Sub DTPAcquDt_KeyUp(sender As Object, e As KeyEventArgs) Handles DTPAcquDt.KeyUp
If e.KeyValue = Keys.Left OrElse e.KeyValue = Keys.Right Then
MyEventCounter = MyEventCounter + 1
End If
End Sub
Private Sub DTPAcquDt_MouseUp(sender As Object, e As MouseEventArgs) Handles DTPAcquDt.MouseUp
MyEventCounter = MyEventCounter + 1
End Sub
Private Sub DTPAcquDt_ValueChanged(sender As Object, e As EventArgs) Handles DTPAcquDt.ValueChanged
TxtDtAcqu.Text = DTPAcquDt.Value
'RESET The Counter
MyEventCounter = 0
End Sub

Shorten code vb.net

Is it possible to shorten these codes? If yes, how? Thanks for your answer guys.
Private Sub txtFirstName_GotFocus(sender As Object, e As EventArgs) Handles txtFirstName.GotFocus
lblFirstName.Visible = True
End Sub
Private Sub txtLastName_GotFocus(sender As Object, e As EventArgs) Handles txtLastName.GotFocus
lblLastName.Visible = True
End Sub
Private Sub txtMiddleName_GotFocus(sender As Object, e As EventArgs) Handles txtMiddleName.GotFocus
lblMiddleName.Visible = True
End Sub
Private Sub txtAddress_GotFocus(sender As Object, e As EventArgs) Handles txtAddress.GotFocus
lblAddress.Visible = True
End Sub
Private Sub txtContact_GotFocus(sender As Object, e As EventArgs) Handles txtContact.GotFocus
lblContact.Visible = True
End Sub
Since your labels and text boxes essentially have the same name (it's only the prefix that's different) you can:
Bind all GetFocus events to a single event handler.
Get the sender's name (sender is the control that raised the event), remove the txt prefix and replace it with lbl.
Look for a control by the new name (lbl...).
If found, make it visible.
In code it'd look like this:
Private Sub TextBoxes_GotFocus(sender As Object, e As EventArgs) Handles txtFirstName.GotFocus, txtLastName.GotFocus, txtMiddleName.GotFocus, txtAddress.GotFocus, txtContact.GotFocus
Const NamePrefix As String = "txt"
Const NewPrefix As String = "lbl"
Dim ctrl As Control = TryCast(sender, Control)
If ctrl IsNot Nothing AndAlso ctrl.Name.StartsWith(NamePrefix) Then 'Check if the sender's name starts with our prefix.
Dim NewName As String = NewPrefix & ctrl.Name.Remove(0, NamePrefix.Length) 'Remove the old prefix and replace it with the new one.
Dim Controls As Control() = Me.Controls.Find(NewName, True) 'Look for the control of our new name.
If Controls.Length > 0 Then 'Did we find one?
Controls(0).Visible = True 'Make it visible.
End If
End If
End Sub