I have a form containes some controls (buttons) how i can make the program autoscan these controls one by one and whenever a button higlited I can bress enter to press this button. Using Visual Basic 2008
You don't have to autoscan the controls, pressing TAB cycles through the controls on the form. Just press TAB until you have the right control selected and then use the KeyDown method for the control:
Private Sub Button1_KeyDown(byVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Button1.KeyDown
If e.KeyCode = Windows.Forms.Keys.Enter Then
' Do whatever
End If
End Sub
In the If Statement, I recommend you reference a custom Sub that you'll also reference in the Button1_Click event. This will allow you to do make sure that hitting Enter on the selected control and clicking on it do the same thing.
The only downside to this is that you have to write Event Handlers as shown above for every control on your form if that's what you want.
HTH
Related
I was working on a project and for no reason all Handles disappeared from the form
For example:
Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click
has become
Private Sub Button17_Click(sender As Object, e As EventArgs)
I have a lot of tools in the form
Is there a shortcut to retrieve Handles?
This happens if a control is being cut and pasted back on the form or if it's deleted and recreated with the same name.
To recreate the Handles you can click behind the closing bracket and press SPACE - TAB - SPACE and fill the rest up with the help of IntelliSense.
There's nothing to retrieve. They are just regular methods now. You need to attach them to the appropriate events like you would any other method you wrote yourself. Open the Properties window in the designer and click the Events button. Select a control on the form, click the drop-down for the event of interest and select the correct method.
I've come across a peculiar focusing issue. I have created the following "search" program:
It runs in the background.
When you double-tap the Ctrl key it becomes visible.
You can type in the textbox because the form has focus.
If the form loses focus (I click on my desktop, for example), it disappears after 3 seconds.
I double-tap the Ctrl key again, and again it becomes visible.
But this time, no matter what I try, the form is not focused and I cannot type in the textbox without first manually clicking on the form.
What's particularly interesting is that when I run this program in debug mode from Visual Studios, the program regains focus upon double-tapping Ctrl key and becoming visible, and I can immediately start typing in the text box. However, when I build this program and run it alone, the program appears but does not regain focus upon double-tapping Ctrl key, and therefore I cannot type in the text box until I manually click the form.
After Me.Show() I have tried:
Me.Focus()
Me.Validate()
Me.Select()
Textbox1.Select()
Textbox1.Focus()
The form is topmost and normally running in administrator, but the same problem arises regardless.
The issue can be recreated in a more simple manner. Create a form with
Button ("Button1")
TextBox
Two timers ("hideForm", "showForm") both with intervals of 1000
Code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
hideForm.Start()
sender.Enabled = False
End Sub
Private Sub hideForm_Tick(sender As Object, e As EventArgs) Handles hideForm.Tick
Me.Hide()
hideForm.Stop()
showForm.Start()
End Sub
Private Sub showForm_Tick(sender As Object, e As EventArgs) Handles showForm.Tick
showForm.Stop()
Me.Show()
Me.Activate()
End Sub
End Class
Click the button, and immediately click on a different window (so the form loses focus). Wait until the form is hidden and shown again. The textbox should have focus. Try typing.
If the program is run in debugging mode in Visual Studios, it works as expected. If you build the program and run it outside of VS, the form will reappear without focus, and you cannot type in the textbox without manually selecting the form.
Sorry for the long-winded explanation. It's a difficult issue to properly describe.
Try the form event handler Activate. Inside that method, you can use setFocus to gain focus for that particular Text Box. I know this answer is too late. But hope this helps someone.
Private Sub Form_Activate()
TextBox1.SetFocus
End Sub
Try an event handler for Form_Activate, and within that handler pass the focus to your textbox.
Instead of Focus, you can also try TextBox1.Select. This SO link provides some additional information and something about the difference between Focus and Select.
Select the Textbox you want to assign a focus to in the Design View Window.
Under the Properties window, set the TabIndex to 0 (zero).
I didn't even have to use the TextBox1.Focus() command. It still bothers me that the TabIndex overrides the Focus command.
What I tried (and worked for me), was to set the Focus() of the Textbox in the event handler Shown() [VB]:
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles MyBase.Shown
Me.Textbox1.Focus()
End Sub
Note: the Select() method just didn't do the job. I hope this helps anyone else that comes with this same issue.
I am developing a POS in vb.net and want to use Enter key as tab. this works well in controls other than button. e.g when the focus is on textbox or combobox etc then it works good but if the focused control is button then it does not move to next control. Help me by fixing this problem, I have searched alot but could not find any solution regarding this.
thanks
Regards
Muhammad Irfan Sumra
Enter key for button have default behavior which raise Click event.
You can handle Click event for moving focus to next control, and use MouseClick event for your click logic
Private Sub MyButton_Click() Handles MyButton.Click
SendKeys.Send("{TAB}")
End Sub
Private Sub MyButton_MouseClick() Handles MyButton.MouseClick
' Do your click logic
End Sub
anybody have an idea how to write a code if the user clicks outside windows form, the form will automatically close? I.e., I had two forms, when i show form2 and then i click outside of it, form 2 will closed.
Does this help? Try the first two solutions, one of which should work for you.
Winforms: Close modal dialog when clicking outside the dialog
You can simply utilize the LostFocus built-in event of the Form like this
Private Sub Form1_LostFocus(ByVal sender As Object, ByVal e As EventArgs) _
Handles Me.LostFocus
Me.Close()
End Sub
I have a nested custom control that I need to set focus on. I have it all internally wired up to automatically set the focus when the form loads, but when it comes up on screen, the designated accept button for the parent form is in focus instead. Even when is disassociate the accept button it still does not set correctly. How can I ensure my desired control gets focus.
I think what you're asking is that you would like to make a textbox (or button, etc) on a user control have focus when the form loads. Try setting the ActiveControl of the user control to the textbox (or which ever control) and then calling focus on it. For example:
Private Sub myUserControl_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ActiveControl = myTextbox
myTextbox.Focus()
End Sub
Set the tab order. If your custom control is on the same level as buttons (has the same parent), make sure your control and any containter it is in has tab index 0.