Prevent raising Click event on RadioButton while moving from one to another - vb.net

I have a Form with a Panel containig 5 RadioButton
I've only handled the Click event of all RadioButtons.
When I use arrow keys to move from a RadioButton to another, the Click event is raised at each move.
Is it possible to only Select the RadioButton and Click it using SpaceBar?

I would you use the Form.KeyDown event of the parent form and do something like this:
Private Sub Form1_KeyPress(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
REM Check if space is the button pressed
If e.KeyCode = Keys.Space Then
REM See if a Radio Button has focus to know which was selected
If RadioButton1.ContainsFocus Then
RunMyCode(RadioButton1)
ElseIf RadioButton2.ContainsFocus Then
RunMyCode(RadioButton2)
End If
End If
End Sub
''' <summary>
''' Method which executes the code you want run when a radio button is selected
''' </summary>
''' <param name="rdButtona"></param>
Private Sub RunMyCode(ByRef rdButtona As RadioButton)
REM Check which radio button was selected and execute the apprpriate code
If rdButtona.Equals(RadioButton1) Then
MsgBox("Radio Button 1")
ElseIf rdButtona.Equals(RadioButton2) Then
MsgBox("Radio Button 2")
End If
End Sub
To prevent the RadioButton from being checked, handle the RadioButton.Click event and uncheck the RadioButton using radioButton1.checked = false. If you handle the RadioButton.CheckChanged event, radioButton1.checked = false will raise another CheckChanged event which we don't need. I would also do something there to show which radio button has been selected. In My example I change the text color but you can do whatever you want.
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
RadioButton2.Checked = False
ResetColors()
RadioButton2.ForeColor = Color.DarkRed
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
RadioButton3.Checked = False
ResetColors()
RadioButton3.ForeColor = Color.DarkRed
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
RadioButton1.Checked = False
ResetColors()
RadioButton1.ForeColor = Color.DarkRed
End Sub
Private Sub ResetColors()
RadioButton2.ForeColor = Color.Black
RadioButton1.ForeColor = Color.Black
RadioButton3.ForeColor = Color.Black
End Sub
Of course the code could be simplified, but this is the general idea.

The following code runs RadioButtons Click Event only on mouse click (or on spacebar press) and not on arrow keys press.
It also gives focus to RadioButton selected with Arrow Key
Dim HandleRBtnClick As Boolean = True
Private Sub RadioButtons_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click, RadioButton4.Click
If Not HandleRBtnClick Then
With CType(sender, RadioButton)
.Checked = False
.Focus()
End With
Exit Sub
End If
'Code for RadioButton Checked (on mouse click or spacebar pressed)
Me.TextBox1.Text = CType(sender, RadioButton).Name
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Left OrElse
keyData = Keys.Right OrElse
keyData = Keys.Up OrElse
keyData = Keys.Down Then
HandleRBtnClick = False
Else
HandleRBtnClick = True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function

Related

vb.net can't use button.PerformClick after control Validated

in the form, i need check the textbox detail,so i use the Validated method.i also have a button to save form detail.and the button bind F10.
the text box Validated method
Private Sub txtReportCD_Validated(sender As Object, e As EventArgs) Handles txtReportCD.Validated
If txtReportCD.Text <> String.Empty Then
cboReportName.Value = txtReportCD.Text.Trim
Else
CommonMsg.showMsg("the CD not be empty")
End If
End Sub
the form keydown
Private Sub form_KeyDown(sender As Object, e As KeyEventArgs) Handles MyBase.KeyDown
If e.Alt AndAlso e.Control AndAlso e.Shift Then
Exit Sub
End If
Select Case e.KeyCode
'save data
Case Keys.F10
ClickButton(Me.btnSaveData, e)
e.Handled = True
End Select
End Sub
the ClickButton method
Private Sub ClickButton(ByVal btn As ButtonKdcCtrl, e As KeyEventArgs)
e.Handled = True
If e.Alt OrElse e.Control OrElse e.Shift Then
Exit Sub
End If
If btn.Enabled Then
btn.PerformClick()
End If
End Sub
the btnSaveData method
Private Sub btnSaveData_Click(sender As Object, e As EventArgs) Handles btnSaveData.Click
CommonMsg.showMsg("begin save data")
'do save form data
End Sub
when focus in txtReportCD,and i press F10 key. the form first do [btn.PerformClick], then do [txtReportCD_Validated] twice,last do [btnSaveData_Click].
but when focus in txtReportCD,and i click btnSavaData button.
the form first do [txtReportCD_Validated] once and not do [btnSaveData_Click].
how can i do,make when i press F10,the form do [txtReportCD_Validated] once
and the [btnSaveData_Click] not be call
PS: the txtReportCD text always empty.

Can't get KeyDown event on Tabpage

I have a TabControl with several TabPages, each containing a series of PictureBox. I want to be able to navigate with the arrow keys inside those TabPages. I tried to give .Focus() to the TabPage when the user selects it, and then trap the PreviewKeyDown and KeyDown events, but it does not work. I've put a breakpoint at the PreviewKeyDown event handler, and it is not event reached.
Here is the activation of the TabPage :
Sub TilesetsTab_Selected(ByVal sender As Object, ByVal e As TabControlEventArgs) Handles TilesetsTab.Selected
Dim myTab As TabControl = CType(sender, TabControl)
If myTab.TabCount = 0 Then Exit Sub
Active.Tileset = Project.Tileset(CInt(myTab.SelectedTab.Tag))
Active.Tileset.Tab.Focus()
End Sub
And here is the Key event handlers
Sub Tab_KeyPreviewDown(ByVal sender As Object, ByVal e As PreviewKeyDownEventArgs) Handles Tab.PreviewKeyDown
If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Or e.KeyCode = Keys.Up Or e.KeyCode = Keys.Down Then e.IsInputKey = True
End Sub
Sub Tab_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles Tab.KeyDown 'BUG
Dim TilesWidth As Integer = Me.Tab.Width \ Me.ScreenSize
Select Case e.KeyCode
Case Keys.Left
If Active.Tile.Index = 0 Then Exit Sub
Me.Tile(Active.Tile.Index - 1).Selected()
Case Keys.Right
If Active.Tile.Index = Project.Tileset.Count - 1 Then Exit Sub
Me.Tile(Active.Tile.Index + 1).Selected()
Case Keys.Up
If Active.Tile.Index < TilesWidth Then Exit Sub
Me.Tile(Active.Tile.Index - TilesWidth).Selected()
Case Keys.Down
If Active.Tile.Index + TilesWidth > Active.Tileset.Count - 1 Then Exit Sub
Me.Tile(Active.Tile.Index + TilesWidth).Selected()
End Select
End Sub
Why can't I fire a KeyDown event on the TabPage ?
I don't know why the answer posted yesterday by someone (and the comments I made) were deleted, but it was said that TabPages could not have focus, and that it is restricted to TabControl. Following that advice, I shifted the PreviewKeyDown and KeyDown event handlers to TabControl events and it worked.
I'm losing some of the arrow navigation between TabPages, but it suits me.

Toggle button color by calling function in vb.net

I'm new to vb.net. I have 20 buttons in one form. When I click any of one button, it color should be changed.
I can code for all button like following. But I need a function, when i call that function, the color should be changed. Please help me and give me full coding
Private Sub btnR1X1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnR1X1.Click
If (btnR1X1.BackColor = Color.White) Then
btnR1X1.BackColor = Color.Gray
ElseIf (btnR1X1.BackColor = Color.Gray) Then
btnR1X1.BackColor = Color.White
End If
End Sub
I have assumed that you are using VB.Net. Assuming that is the case, you should edit your question to remove the vb6 tag.
You can write a function that will toggle the BackColor of any control.
Private Sub ToggleColor(ctrl As Control)
If ctrl.BackColor = Color.White Then ctrl.BackColor = Color.Gray Else ctrl.BackColor = Color.White
End Sub
You can call that function from a Button's click handler like this
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ToggleColor(CType(sender, Control))
End Sub
However, if all you want to do when any of the buttons is clicked is to toggle the BackColor, you can use a single event handler for the click event of every button.
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click 'etc
Dim ctrl as Control = CType(sender, Control)
If ctrl.BackColor = Color.White Then ctrl.BackColor = Color.Gray Else ctrl.BackColor = Color.White
End Sub

How to change the Location (x,y) of the object by using coding in VB.net?

I Want to say sorry if there was a same question like i had. I have tried to Search but i couldn't find it, so.. i hope there is no other question like this..
To the point, i need your help to tell me how to change the Location of an object in a form
What i want to do is making the Button1 move to the left when i press the left key on the keyboard. But i have a problem with how to set the location (x,y) of the object
Private Sub Button1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Button1.KeyPress
If Asc(e.KeyChar) = Keys.Left Then
End If
End Sub
Thanks ...
By default, the arrow keys are not captured by the control's KeyPress, KeyDown or KeyUp events. You can make them be captured by KeyDown and KeyUp by setting e.IsInputKey to True in the PreviewKeyDown event. Then you can move the button sideways by changing its Left property. The following assumes the button has focus.
Private Sub Button1_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) _
Handles Button1.PreviewKeyDown
If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Then e.IsInputKey = True
End Sub
Private Sub Button1_KeyDown(sender As Object, e As KeyEventArgs) _
Handles Button1.KeyDown
Dim myButton As Button = CType(sender, Button)
If e.KeyCode = Keys.Left Then myButton.Left -= 1
If e.KeyCode = Keys.Right Then myButton.Left += 1
End Sub
Updated Solution
Here's a way to accomplish it so that it moves while holding down the key. Create a project with a Form and Button.
Right-click on the Project in the Solution Explorer and add two .NET References:
PresentationCore
WindowsBase
Here's the code. A timer will capture key events and move the button:
Imports System.Windows.Input
Public Class Form1
Private timex As New Timer
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
AddHandler timex.Tick, AddressOf myTickEvent
timex.Enabled = True
timex.Interval = 100
End Sub
Private Sub myTickEvent(sender As Object, e As EventArgs)
If isKeyPressed(Key.Left) Then
Me.Button1.Left -= 10
End If
If isKeyPressed(Key.Right) Then
Me.Button1.Left += 10
End If
If isKeyPressed(Key.Up) Then
Me.Button1.Top -= 10
End If
If isKeyPressed(Key.Down) Then
Me.Button1.Top += 10
End If
End Sub
Private Function isKeyPressed(ByRef keys As System.Windows.Input.Key)
If (Windows.Input.Keyboard.GetKeyStates(keys) And Windows.Input.KeyStates.Down) > 0 Then
Return True
Else
Return False
End If
End Function
End Class

vb.net - enable/disable a button depending on user input

As I'm putting the finishing touches on my program I'm having some troubles.
Theres several user inputs and a submit button, once the inputs has been filled I wish to enable the submit button, else the button should be disabled. This is what I have:
Private Sub ButtonControl(sender As System.Object, e As System.EventArgs) Handles Input1.Validated
If Input1.Text = "" Then
ButtonSubmit.Enabled = False
ElseIf Input1.Text <> "" Then
ButtonSubmit.Enabled = True
End If
End Sub
The thing is it disables nomatter what and then it doesnt enable when my input is filed
Your code will work if you have another control that can receive the focus. Control Validation occurs on the loss of focus. If you need to have just one focusable item active you will need to use either KeyPress, KeyDown or Textchanged events to enable your button, also make sure that the CausesValidation property of your TextBox is true.
I would also make the method more generic so you could call it from multiple textbox's by using the sender object to access the textbox that raised the event. Also if you have a True/False condition you only need to do the comparison in the first if statement and then you just use an else not an elseif.
for example:
Private Sub ButtonControl(sender As System.Object, e As System.EventArgs) Handles Input1.Validated
If DirectCast(sender, TextBox).Text = "" Then
ButtonSubmit.Enabled = False
Else
ButtonSubmit.Enabled = True
End If
End Sub
You can also use the String.IsNullOrWhiteSpace Method to check if just spaces have been entered if you are using the 4.0 framework or above. Like this TextChanged EventHandler.
Private Sub ButtonControl(sender As Object, e As EventArgs) Handles Input1.TextChanged
If String.IsNullOrWhiteSpace(DirectCast(sender, TextBox).Text) Then
ButtonSubmit.Enabled = False
Else
ButtonSubmit.Enabled = True
End If
End Sub
You are going to need to use the TextBox "TextChanged" event, and be sure to set each Textbox AutoPostback="True". You can use an UpdatePanel to make the postbacks that occur on each Textbox you wish to validate less obnoxious to your end-user.
So, your textbox (if you have many, make sure they all have the OnTextChanged="ValidateForm":
<asp:TextBox ID="Input1" runat="server" OnTextChanged="Validate_TextChanged" />
Inside your textchanged ("ValidateForm") event (which each Textbox is attached to), one quick to implement route to do would just be
' Validation inside this event
Protected Sub Validate_TextChanged(sender As Object, e As EventArgs)
if Input1.text <> "" AndAlso Input2.text <> "" AndAlso ' ...etc.
End Sub
If you go the route of the UpdatePanel, you may find this useful.
This is the kind of thing I would do:
Private Sub TextBoxes_TextChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles _
TextBox1.TextChanged, _
TextBox2.TextChanged, _
TextBox3.TextChanged
Dim textBoxes = { TextBox1, TextBox2, TextBox3 }
Button1.Enabled = textBoxes.All(Function (tb) tb.Text <> "")
End Sub
You can then add as many text boxes in to the textBoxes array as you need to check. Just make sure that the text boxes to the Handles list.
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length > 0 Then
Me.Button1.Enabled = True
Else
Me.Button1.Enabled = False
End If
End Sub
Do that code in the Input1_TextChanged event
This is what I did and worked:
Dim CheckInput1 As Boolean
Private Sub Input1_TextChanged(sender As Object, e As EventArgs) Handles Input1.TextChanged, Input1.Enter
CheckInput1 = True
If Input1.Text = "" Then
CheckInput1 = False
End If
If CheckInput1 = False Then
ButtonSubmit.Enabled = False
ElseIf CheckInput1 = True Then
ButtonSubmit.Enabled = True
End If
End Sub
There must be a more efficient code that this but I think this solves your problem.