Making a VB.net winform button look pressed from code behind - vb.net

I've got a basic VB.net 2.0 application together in VisualStudio 2005.
Within a form, I've tied [enter] in several text boxes to a button. What I'd like to do is "press" the button from getField_KeyDown() to give the user a visual indication of what's happening.
With the code below, the button click work is done but the button's look doesn't change and the user is left without feedback.
Private Sub getField_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtGetKey.KeyDown
If e.KeyCode = Keys.Enter Then
e.Handled = True
Call btnGet.PerformClick()
End If
End Sub
How can I make the button looked pressed while the btnGet.PerformClick() work is being done?

You can make a checkbox and set the appearance to a button.
chkCheck.Appearance = Appearance.Button
You can also set this in the design mode.
And it behaves just the same as a normal checkbox. So you can assign a key pressed to the state of the checkbox.

You want to set the FlatStyle of the button. So your code will look like this:
Private Sub getField_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtGetKey.KeyDown
If e.KeyCode = Keys.Enter Then
e.Handled = True
btnGet.FlatStyle = FlatStyle.Flat
End If
End Sub
This will change the button appearance - Don't forget to change it back to FlatStyle.Standard

Related

My Form is not registering Enter key presses

I am writing an educational program. I have a button which I want to be "clicked" when the user presses enter. I have set the form's accept button property to true and the KeyPreview property is also set to true. I have tried creating a custom Sub to replicate AcceptButton functionality, this does not work either, the sub runs on all keys other than when the enter key is pressed.
Private Sub TextBoxAnswer_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles TextBoxAnswer.KeyDown
If e.KeyCode = System.Windows.Forms.Keys.Enter Then
CheckQ()
End If
End Sub
I have spent the last 3 days searching for an answer and have tried everything on google I could find. I have also just tried setting the textbox MultiLine property to true and the enter key doesn't even create a new line, just to clarify I have set the Multiline property back to false now as it should be.
The AcceptButton property should be set = to the button you want "clicked" when enter is pressed, not a boolean value. Me.AcceptButton = buttonSubmit
Did you add a handler for the form's Keydown event to go along with setting the KeyPreview property?
Private Sub form1_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles form1.KeyDown
If e.KeyCode = System.Windows.Forms.Keys.Enter Then
CheckQ()
e.Handled = true
End If
End Sub
Your code as you have it written for the textbox keydown event should work as well. I tried it in a test app and didn't have any issues. Do you hit a breakpoint if you place it on the CheckQ() method call?
Any one of these methods should accomplish what you are trying to do.

Textbox Focus - Click versus Tab Using VB

The following code changes the properties of a textbox when it has focus and reverts the changes after losing focus. I'm having trouble with using the Enter, Leave, GotFocus, LostFocus events because they occur in different orders when clicking or tabbing to a textbox. The following code only acts as expected when tabbing between textboxes but not clicking. If I change txtBoxes_ReminderOffFocus to handle the Leave event instead of LostFocus, then it works as expected when clicking between textboxes but not tabbing.
Private Sub txtBoxes_ReminderOnFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHrsWkd.Enter, txtPayRate.Enter, txtFedTaxRate.Enter, txtStTaxRate.Enter
If sender.Text = "(Numeric Value)" Or sender.Text = "(Percentage)" Then
Debug.Print("New textbox focused onto.") 'Sets up textboxes for standard input, if they have initial reminder. Check control.enter event for more on focus orderings.
sender.Clear()
sender.TextAlign = HorizontalAlignment.Left
sender.ForeColor = Color.Black
End If
End Sub
Private Sub txtBoxes_ReminderOffFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHrsWkd.LostFocus, txtPayRate.LostFocus, txtFedTaxRate.LostFocus, txtStTaxRate.LostFocus
If sender.Text = "" Then
Debug.Print("A textbox has lost focus.")
sender.ForeColor = Color.Gray 'if textbox is empty, fills in initial "numeric value" or "percentage" reminder.
sender.TextAlign = HorizontalAlignment.Right
If sender Is txtHrsWkd Or sender Is txtPayRate Then
sender.Text = "(Numeric Value)"
Else
sender.Text = "(Percentage)"
End If
End If
End Sub
In 'txtBoxes_ReminderOffFocus'
.LostFocus event works as expected when tabbing between textboxes, not clicking.
.Leave event works as expected when clicking between textboxes, not tabbing.
Since .LostFocus works for tabbing and not clicking and .Leave works when clicking and not tabbing, can you try setting txtBoxes_ReminderOffFocus to handle both events?

Need to disable the enter key on a textedit box or at the form level

Can you show the coding needed to disable the enter key on a textedit box or at the form level?
This form is using multi-line textedit boxes and I would like to prevent the user from pressing the enter key from jumping to the next line in the textedit boxes.
You would add a handler to the text box's "KeyDown" event and use KeyEventArgs.SuppressKeyPress -- see :
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.suppresskeypress.aspx#Y0
Private Sub TextBox1_KeyDown(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles TextBox1.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
End If
End Sub
Text boxes also have a .ReadOnly property which can be set programatically if you need to temporarily (or permanently) prevent the user from changing the content of the box (ie: for display only).

how to focus a specific textbox in vb.net

I'm making a desktop application in vb.net. When I click the back button (placed by me) on any form it hides current form and shows previous form but when i go again to the form from which i had hit back button then the focus cue still remains on the back button but I want the focus to be on the first textbox on that form or any specific button on which I want.....How can i achieve this...
I have already used some code to shift focus from one textbox to another when i press enter key...but it doesn't work in the above mentioned case....I'm using vb.net in visual studio 2005...
This is the code i'm using for shifting focus among textboxes
Private Sub party_code_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles party_code.KeyDown
If e.KeyData = Keys.Return Then
party_name.Focus()
End If
End Sub
Can anyone help me on this????
You need to add a handler for either the form's Activated event:
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
party_name.Focus()
End Sub

Can't set focus on a Windows Forms textbox

I can't seem to get input focus on a textbox when a tab page first comes up (I'm using Windows Forms, VB.NET 3.5).
I have a textbox on a panel on a tab page, and I want the focus to be on the textbox when the tab page comes up. I want the user to be able to start typing immediately in the focused textbox without having to click on the textbox. I have tab stops set in the order I want and the textbox is the first tab stop. The tab stops work except that when the tab page comes up the focus is not on the textbox, i.e. the one that's first in the tab order.
In the Enter event handler of the tab page I call the Focus method of the text box, but it returns False and does nothing, no error messages. I know I can access the text box because
at the same point in the code I can set the text of the text box.
If it matters, the layout of the tab page is a little complicated:
frmFoo/TabControl1/TabPageX/Panel1/Panel2/TextBox1
I want to set the focus on TextBox1.
What's the best way to get the focus on the desired textbox?
If setting focus is the best way, why is the textbox.Focus() method failing?
I would assume you are attempting to set focus in the form load event handler? If so, you need to do a Me.Show() to actually create the onscreen controls before focus can be set. Something along the lines of:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Show()
Application.DoEvents()
TextBox1.Focus()
End Sub
If you don't do the Me.Show(), the form is NOT displayed until the load event is complete.
For the tab control, handle the _SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab.Name = "TabPage1" Then
TextBox2.Focus()
End If
If TabControl1.SelectedTab.Name = "TabPage2" Then
TextBox4.Focus()
End If
You will still want to set the initial focus in the load event as shown above if the first field selected is to be the textbox on the tab control.
Try either:
Me.ActiveControl = TextBox1
or
TextBox1.Select()
Do the control.Focus() in the OnShown event. You don't need any of the DoEvents logic which didn't work for me anyway.
You Should Use Selected Event of TabControl
Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
If e.TabPage.Name = "TabPage1" Then
TextBox1.Select()
End If
End Sub
As I have Checked in Both TabControl.Selected and TabPage.Enter Event can set Select TextBox. I think there is some other elements stealing focus. please varify
Any of the solutions I found online don't solve the problem when the control is on a tab page.
However, this works:
(1) set the TabIndex of the control to 0.
(2) In your code that handles the tabpage event, do the following:
SendKeys.Send("{TAB}")
If SendKeys doesn't seem to be a valid statment, make sure you have the following import at the top of your code file:
Imports System.Windows.Forms
I found that the TabControl gets the focus when the Selected event completes. To make this work I used the Paint event of the TabPage to set the focus of the desired object.
Private Sub TabChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tab1.Paint, Tab2.Paint, Tab3.Paint
Select Case sender.Name
Case "Tab1"
Textbox1.Focus()
Case "Tab2"
T3extbox2.Focus()
Case "Tab3"
Textbox3.Focus()
End Select
End Sub
Try the Activated event of the form like this:
Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
'SendKeys.Send("{TAB}") this line works too
TextBox1.Focus()
End Sub
That is guaranteed to work.
I once had the same problem but i solved it using the Me.activate() function.