Click here to Skip to main content
15,886,091 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all:

who can help me solve this issue, my issue is how to transfer focus to the previous control
Posted

For System.Windows.Forms.Control, use the method GetNextControl. For the obtained control, call Focus. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.getnextcontrol.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.focus.aspx[^].

Don't blame me if I answered a wrong question. In this case, this is your problem: you did not tag what application type or UI library do you use: WPF? Forms? Silverlight? ASP.NET? What?

Next time, always tag such thing in your question. Moreover, you could find the answer in MSDN much sooner then asking this question and waiting for the answer.

[EDIT]

For WPF, you can use System.Windows.FrameworkElement.PredictFocus in nearly the same way. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.predictfocus.aspx[^].

—SA
 
Share this answer
 
v2
Comments
Maciej Los 8-May-12 15:03pm    
Good answer, my 5!
Sergey Alexandrovich Kryukov 8-May-12 15:13pm    
Thank you.
--SA
killerqx1 9-May-12 21:45pm    
i'm sorry! i using WPF
killerqx1 9-May-12 21:47pm    
my issue is, curent i am focus on textbox, so after pressed F1 Function key then move focus to the previous control, i try "Me.SelectNextControl(Me.ActiveControl, False, True, True, True)" but it does not work
Sergey Alexandrovich Kryukov 10-May-12 19:25pm    
This should be a right solution, but "it does not work" is not informative. Run it under the debugger, observe if this method is called at all or not, see what happens on related event (don't use F1, it is reserved for help).

Again, by not tagging your question you made me to waste some time. This is not very nice. Well, tag it now. Please tag it properly next time.
--SA
hi you can define a bookmark variable as control
and in lostfocus event of every control set it to that control
VB
    Dim Bkm, bkm1 As Control
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'focus to previous control
        bkm1.Focus()
    End Sub
    Private Sub TextBox3_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox3.LostFocus
        bkm1 = Bkm
        Bkm = TextBox3
    End Sub
    Private Sub TextBox4_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox4.LostFocus
        bkm1 = Bkm
        Bkm = TextBox4
    End Sub
    Private Sub TextBox5_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox5.LostFocus
        bkm1 = Bkm
        Bkm = TextBox5
    End Sub
    Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
        bkm1 = Bkm
        Bkm = TextBox1
    End Sub
    Private Sub TextBox2_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox2.LostFocus
        bkm1 = Bkm
        Bkm = TextBox2
    End Sub

when you need to focus the previous control press button3
good luck
 
Share this answer
 
Comments
Maciej Los 8-May-12 15:07pm    
Can i ask you why you do this for each control (TextBox 1-5)? Why in this way? Can't you use TabIndex property?
Sergey Alexandrovich Kryukov 8-May-12 15:14pm    
This is quite a bright demonstration on the topic: "how not to do programming, ever". Voted 1.
--SA
Maciej Los 8-May-12 17:35pm    
I agree with with you, SA.
killerqx1 10-May-12 4:47am    
yep, i tried it but that only move to next control, but i only want when i pressed one function key example F1 :) then move focus to the previous control not next control
Rishi Dahikar 13-Jan-14 6:31am    
you write the code in Form_Keydown event right so your all textbox having tab index 1,2,3 etc if ur focus on 3 textbox so pressing F1 , focus goes to textbox having tab index 2
just copy and add this code to your form

Protected Overrides Function ProcessTabKey(ByVal forward As Boolean) As Boolean
End Function
and try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900