DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Feb 2008
    Posts
    61

    Moving from UserControl To A Form Control

    I am creating a user control that has 6 textboxes in it. When the cursor sits in a textbox and the Enter key is pressed the cursor moves to the next textbox. When it gets to the last textbox and the Enter key is pressed, I would like for it to jump out of the user control and move to whatever the control is on the form with the next TabIndex (like if the Tab key was pressed). I’ve tried using the SendKey method, but it doesn’t like the “SendKey” once typed in. Can somebody please help me? Thanks.

  2. #2
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    In the LostFocus event of the last TextBox, put the following line:

    Code:
    GetNextControl(Me, True).Select
    Me represents the object you are in (your UserControl). When GetNextControl is called on Me, it will fetch the next control at the UserControl level, thus on the form.

    True, gets the next control. False would have been the previous control.

    Select selects the control retrieved by GetNextControl.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  3. #3
    Join Date
    Feb 2008
    Posts
    61
    Thanks for your reply. The solution works to a degree, but after it goes through the LostFocus it requires another key stroke other than the Enter key to get it to move on to the next control. Even pressing the Enter key more than once doesn’t work….it has to be another key. Besides that I also have other keys that can make the last textbox loose focus, but I don’t want them to make it leave the UserControl. So, I moved the GetNextControl to the TextBox6_KeyDown procedure where I get pretty much the same results. Here’s my code for that.


    If e.KeyCode = Windows.Forms.Keys.Enter Then
    Kount = TextBox6.Text
    TextBox6.Text = Format(Kount, "00")
    Call ErrorCheck()
    If xError = "N" Then GetNextControl(Me, True).Select()
    End If

    Any Ideas on what is going on and why it doesn’t move on without multiple key strokes? Thanks for your help, it much appreciated.

  4. #4
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    Hard to say with that limited view of the code.

    TextBox6.Text = Format(Kount, "00") could trigger another event that changes the way things behave.

    And what happens inside of the call to ErrorCheck.

    My intuition is that somewhere, the value of xError is changed to something else than "N" while you are expecting it to be "N". And somewhere, a multiple keystroke sets it to the "N" you are awaiting.

    You seem to rely a lot on class level or public variables (Kount and xError, where are they declared?), that can be changed anywhere in the code, which make debugging a lot harder than using local variables. Ideally, ErrorCheck should be a function that returns a boolean value instead of changing the xError variable as I suppose it is doing.

    The way the code is written, xError could be changed by something else, making the error very hard to pinpoint.

    This is a very bad programming practice that was the only way to go in the 80's, but has changed since (unfortunately, many programming teachers or self proclaimed gurus still program the same way they did 30 years ago... and I am not complaining about them being old, I was personnally programming that way 40 year ago... you can be old and program in a modern way, or be young and program in an old way). Nowadays we tend, as much as possible, to use local variables and functions instead of defining variables that can be changed from anywhere in the code.

    Once again, only intuition, because I see only a tiny little part of what is happening. But look everywhere in your code where the variable xError is changed. One of those is probably the culprit.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  5. #5
    Join Date
    Feb 2008
    Posts
    61
    I solved the problem of the multi-key strokes to get it to move on to the next control. I forgot that I had it in a panel to create a certain look and it was going to it.

    Once I removed the panel though it doesn’t matter how I do the coding the next TabIndex that it goes to is within the UserControl which in this case goes back to the first TextBox. It never leaves the UserControl. Any Ideas?

    I plan to spend the day playing with it and if anything changes I’ll let you know.

  6. #6
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    Put a breakpoint on the line of code and see if it is hit.

    If not, check that the Handles at the end of the event procedure declaration is still there. The Form Designer might have remove it if you moved the TextBox out of the User Control by mistake.

    It the Handles is still there and the breakpoint is not hit, then something is grabbing the Enter key before it goes into your event. Could the KeyPreview property be True on the form? In such a case the Form receives the key before the control.

    If the breakpoint is hit, then make sure that your UserControl is not the last in the Tab Order sequence of the form.

    You were using the Panel to create a "certain look", and this is not the role of a Panel. Because the Panel is a container, it changes the way thing behave in the form, specially the Tab Order. Controls are not put there for look, but for their use. So I wonder, do you really need a UserControl? If you do not use it in other forms, then it is useless and you would be better off putting the TextBoxes right on the Form, that would solve your problem.

    If none of this answers your situation, you could try to move the focus directly to the control into which you want it to go:

    Code:
    If TypeOf Me.Parent Is Form1 Then
        DirectCast(Me.Parent, Form1).Button1.Select()
    Else
        'Repeat other forms on which you want to use the UserControl
    End If
    Simply replace Form1 and Button1 by the name of your form and the name of the control you want to go to.

    This is not as interesting because it needs to be adjusted for all the forms you are using the control with, but it would temporarily solve your problem while waiting for another solution.

    You told us about trying to use the SendKey in your first post. What did not work with that? How and where were you using the Sendkey? Usually, a TAB in the last control lets you out of the UserControl (in Visual Studio 2010 at least). So using SendKey with a "{TAB}" should work. However SendKey should always be used only as a last resort, because you are never sure of where the key will end up.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  7. #7
    Join Date
    Feb 2008
    Posts
    61
    I have played with this for hours now with no results and here’s what I finally did. I created a new UserControl with just two TextBoxes and added the following code. (This is the complete code for the UserControl)



    ===============================================================
    Public Class UserControl1

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
    If e.KeyCode = Windows.Forms.Keys.Enter Then
    TextBox2.Focus()
    End If
    End Sub

    Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
    If e.KeyCode = Windows.Forms.Keys.Enter Then
    GetNextControl(Me, True).Select()
    End If
    End Sub


    End Class
    ===============================================================


    I then created a new Windows application and added this UserControl along with one extra TextBox. What I was expecting to happen was for the cursor to move through the two TextBoxes in the UserControl when I pressed the Enter key and then finally work its way down to the TextBox that I added to the Windows application. When I tried testing this, the cursor never left the UserControl just as it’s doing with my original UserControl that I’m trying to get to work. All it did was alternate between the two TextBoxes within the UserControl.

    What I have concluded is that the GetNextControl will only work within the UserControl unless someone can tell me differently. One thing that does come to mind is that there could be a property that I need to set to get it to finally work that I don’t know about.

    Have you got an answer to what I have given here? I’m very much stumped over this and would like to know what I’m doing wrong if anything. If I could get the above to work, I could then get my original project going also.

    Thanks for your help.

  8. #8
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    Try this

    Code:
    Private Sub TextBox2_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox2.KeyDown
        If e.KeyCode = Windows.Forms.Keys.Enter Then
            e.Handled = True
            SendKeys.Send("{TAB}")
        End If
    End Sub
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  9. #9
    Join Date
    Feb 2008
    Posts
    61
    I have tried using SendKeys in the beginning (as stated above) and the program editor doesn’t like it. I get squiggly lines under the word “SendKeys”. The closest thing that comes to it is “Sender” and “SendToBack”.

  10. #10
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    When you get a wiggly (the name given by Microsoft to those "squiggly lines"), simply put the cursor over it and you will get a tooltip telling you why you have the error. In some instances, you will also get a small red square at the end of the line. Moving over it will call a Smart Tag that often propose a solution and will even apply it for you.

    The two most frequent reasons to get an error on Sendkeys are the following.

    In VB6, SendKeys was a method, in .NET it is now and object with a Send method. Many older VB programmers do not detect that difference and try to call it the old way : SendKeys("{TAB}"). That does not work, you need to call Send on the object: SendKeys.Send("{TAB}").

    This is not your situation, since you are in a Windows Application project, butSendkeys, as well as a few other common objects such as MessageBox, is part of the System.Windows.Forms namespace, which is not referenced by default when creating a new class library (dll). You need to add the reference manually to have access to those objects.
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

  11. #11
    Join Date
    Feb 2008
    Posts
    61
    IT WORKS! I never thought that I would have to “Import” the “System.Windows.Forms” to the UserControl. Live and learn. Thanks for all your help, I really appreciate it.

Similar Threads

  1. A form passing a parameter to another form
    By Bill Gaddam in forum VB Classic
    Replies: 10
    Last Post: 11-06-2007, 11:15 AM
  2. Replies: 7
    Last Post: 08-22-2002, 12:24 PM
  3. Replies: 0
    Last Post: 04-11-2002, 06:48 PM
  4. Replies: 0
    Last Post: 02-04-2001, 09:56 AM
  5. Losing control on SetParented form.
    By Mark Alexander Bertenshaw in forum VB Classic
    Replies: 0
    Last Post: 03-16-2000, 12:14 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links