PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
VS 2010 Enter Key on User Control-VBForums
Results 1 to 9 of 9

Thread: Enter Key on User Control

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Enter Key on User Control

    I have an User Control which contains an textbox and listbox. In the textchange event of the TextBox, the listbox is populated from the DB. In the KeyDown event of the the textbox i am handling the Enter key, if there is an selection in the listbox the textbox text is set to that value.. as follows

    vb Code:
    1. Private Sub txtField_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtField.KeyDown
    2.         If e.KeyCode = Keys.Down Then
    3.             e.Handled = True
    4.             e.SuppressKeyPress = True
    5.             If Me.ListBox1.Items.Count > 0 AndAlso Me.ListBox1.SelectedIndex < Me.ListBox1.Items.Count - 1 Then
    6.                 Me.ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex + 1
    7.             End If
    8.         ElseIf e.KeyCode = Keys.Up Then
    9.             e.Handled = True
    10.             e.SuppressKeyPress = True
    11.             If Me.ListBox1.Items.Count > 0 AndAlso Me.ListBox1.SelectedIndex > 0 Then
    12.                 Me.ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex - 1
    13.             End If
    14.         ElseIf e.KeyCode = Keys.Enter Then
    15.             If Me.ListBox1.SelectedIndex > -1 Then
    16.                 Me.Raise_Events = False
    17.                 Me.txtField.Text = Me.ListBox1.Text
    18.                 Me.Raise_Events = True
    19.                 Me.ListBox1.Visible = False
    20.             End If
    21.             e.Handled = True
    22.             e.SuppressKeyPress = True
    23.         End If
    24.     End Sub

    The form on which i am using this UC has KeyPreview set to True, and i am handling the Keydown event of the form , and on Enter key the focus is set to the next control using Me.SelectNextControl.

    When the focus is on UC and Enter key is pressed the Keydown event of the UC textbox is not fired.

    Is it possible to fire both the Keydown events i.e of UC textbox and UC on the form when Enter key is pressed?

    TIA

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,912

    Re: Enter Key on User Control

    There is also a PreviewKeyDown event so the UC would be raising that before the form raises its KeyDown event.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Re: Enter Key on User Control

    Strangely the user control is not firing the PreviewKeyDown event.

    I have even tried on a new UC but same result.

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Re: Enter Key on User Control

    Strangely the user control is not firing the PreviewKeyDown event.

    I have even tried on a new UC but same result.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,912

    Re: Enter Key on User Control

    I probably mislead you a little there. I didn't actually mean the UC would raise that event but rather the TextBox whose KeyDown event you're already handling.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Re: Enter Key on User Control

    Thank you for you Help..Thats works perfectly...

    But..
    The UserControl is not firing KeyDown and KeyUp Events on the form. Is this an related issue?

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,912

    Re: Enter Key on User Control

    It's not an issue at all. Only the control that has focus will raise the event. If you want the UC to raise the event too then you'd have to handle the child control's event and then call the OnKeyDown method of the UC.

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,133

    Re: Enter Key on User Control

    There's an TextBox in the UC, in the KeyDown Event of the TextBox i am calling "Me.OnKeyDown(e)"

    Is this the correct way to do? Its working though.

    Regards.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,912

    Re: Enter Key on User Control

    Yes that is correct. I'd just double-check the object referred to by `e` though, just to make sure that it's not more appropriate to create a new object of that type rather than passing the existing one on. It may well be OK but I haven't checked what it contains so I don't know whether there's anything specific to the child control in there.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width