הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Saturday, September 29, 2012 2:01 AM
Hi
I have a main form (IsMdiContainer and KeyPreview set to true) and some UserControl sub forms. each UserControl subform contains different type of controls such as TextBox, ComboBox, DGV and etc.
And what is the problem?
I want user can goes to next control on loaded UserControl forms with Enter KeyPress event generally in my program.
Please help me?
Thank you
All replies (8)
Saturday, September 29, 2012 7:26 PM ✅Answered | 1 vote
Maybe just send a tab key when Enter key is detected?
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
AddKeyDownEvent(Me)
End Sub
End Class
Public Class UserControl1
Private Sub UserControl1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Me.TabStop = True
AddKeyDownEvent(Me)
End Sub
End Class
Module Module1
Public Sub AddKeyDownEvent(ByVal obj As Control)
Dim ctl As Control = obj.GetNextControl(obj, True)
Do Until ctl Is Nothing
AddHandler ctl.KeyDown, AddressOf AllControls_KeyDown
ctl = obj.GetNextControl(ctl, True)
Loop
End Sub
Public Sub AllControls_KeyDown(sender As Object, e As KeyEventArgs)
If e.KeyCode = Keys.Enter Then
e.Handled = True
SendKeys.SendWait("{TAB}")
End If
End Sub
End Module
Saturday, September 29, 2012 2:38 AM
try this (in your mdichild _KeyDown event):
Private Sub Form2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
Me.SelectNextControl(Me.ActiveControl, True, True, False, True)
End If
End Sub
you need to set:
Me.KeyPreview = True
in Form_Load
thanks for any help
Saturday, September 29, 2012 2:53 AM
try this (in your mdichild _KeyDown event):
Private Sub Form2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown If e.KeyCode = Keys.Enter Then Me.SelectNextControl(Me.ActiveControl, True, True, False, True) End If End Sub
you need to set:
Me.KeyPreview = True
in Form_Load
thanks for any help
Thank you Paul for your reply but i checked this code before and it is just suitable for a form without UserControl subforms. wiht your code Me.ActiveControl returns only the name of loaded sub-form, not controls on the subforms.
Saturday, September 29, 2012 3:58 AM
What is a 'usercontrol subform'? A usercontrol is a control on a form, like any other control, and that code will work with it. A subform is a form - such as a child form in a MDI parent form. That code won't work with forms, but can be easily modified to do so. But I don't know what changes you would need to make for the code to work with a 'usercontrol subform'.
If you are getting the 'loaded subform' then that is because you have placed the code in the MDI parent form instead of in the child form.
Saturday, September 29, 2012 12:33 PM
What is a 'usercontrol subform'? A usercontrol is a control on a form, like any other control, and that code will work with it. A subform is a form - such as a child form in a MDI parent form. That code won't work with forms, but can be easily modified to do so. But I don't know what changes you would need to make for the code to work with a 'usercontrol subform'.
If you are getting the 'loaded subform' then that is because you have placed the code in the MDI parent form instead of in the child form.
Hi Acamar, Thanks for your attention. Sorry, maybe I used incorrect name for that control. My intent was the forms named "User Control" that can be added as a "New Item" to project in "Windows Form" category in VB.Net
I tested above code before on these sub-forms and it didn't works.
Saturday, September 29, 2012 12:54 PM
So I think you are saying that you have created a user control that is made up of a number of standard controls, and you are trying to make the focus move between those controls.
You will need to manage this from code within each control that makes up the user control. The user control does not intercept keystrokes in the same way that the form does when KeyPreview is True.
Private Sub CheckBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CheckBox1.KeyDown
If e.KeyCode = Keys.Enter Then
CheckBox2.Focus
End If
End Sub
and similar for the other controls.
Saturday, September 29, 2012 1:03 PM
So I think you are saying that you have created a user control that is made up of a number of standard controls, and you are trying to make the focus move between those controls.
You will need to manage this from code within each control that makes up the user control. The user control does not intercept keystrokes in the same way that the form does when KeyPreview is True.
Private Sub CheckBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles CheckBox1.KeyDown
If e.KeyCode = Keys.Enter Then
CheckBox2.Focus
End If
End Suband similar for the other controls.
Yes i can do that and i know that but i have a lot of TextBox, ComboBox, ListBox and etc on these sub-forms. I am searching for general way for moving to next tab-indexed control with Enter key.
Thanks
Saturday, September 29, 2012 8:58 PM
Are they sub-forms or are they user controls? If they are sub-forms then you can adapt the code previously provided. If they are user-controls then there is no simple way to do it, but of course once it is done for the user control it works for every instance you create.