Results 1 to 6 of 6

Thread: [RESOLVED] Sendkey

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2008
    Location
    Philippines
    Posts
    218

    Resolved [RESOLVED] Sendkey

    Hi guys,

    In VB6 I used Sendkeys "{Tab}" to place the cursor on the next control.

    How will I do this in VB.NET ?

    Code:
    Private Sub txtUserID_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUserID.KeyDown
    
            If e.KeyCode = Keys.Enter Then
                  ' what will I add here ? 
            End If
    
        End Sub
    Thank you very much.

  2. #2
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: Sendkey

    you would want to use the SendKeys class similar to vb6. On a side note, to prevent the notorious ding you here when pressing enter, place the code in the KeyPress event and set e.Handled = true like so
    Code:
        Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
            If e.KeyChar = Chr(Keys.Enter) Then
                e.Handled = True
                SendKeys.Send("{TAB}")
            End If
        End Sub
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Sendkey

    Don't use SendKeys.Send for this. Call SelectNextControl.
    vb.net Code:
    1. Private Sub SelectNextControlOnEnterKey(ByVal sender As Object, _
    2.                                         ByVal e As KeyPressEventArgs) Handles TextBox1.KeyPress, _
    3.                                                                               TextBox2.KeyPress, _
    4.                                                                               TextBox3.KeyPress
    5.     If e.KeyChar = ControlChars.Cr Then
    6.         Me.SelectNextControl(DirectCast(sender,  _
    7.                                         Control), _
    8.                              True, _
    9.                              True, _
    10.                              True, _
    11.                              True)
    12.         e.Handled = True
    13.     End If
    14. End Sub
    You could use the KeyDown event and set e.SuppressKeyPress to True but the advantage of using the KeyPress event is that if you keep the Enter key depressed focus will continue to move from control to control, which is the same behaviour as the Tab key.

    Note also the use of ControlChars.Cr, which is the first character of a Windows line break.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [RESOLVED] Sendkey

    Good info, I never knew about the ControlChars enum.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Sendkey

    Quote Originally Posted by bmahler
    Good info, I never knew about the ControlChars enum.
    It's not an enum. It's a class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [RESOLVED] Sendkey

    look at that I learned even another thing
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

Posting Permissions

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



Click Here to Expand Forum to Full Width