Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
Hi,
 
I am having 100's of controls in a form. I want to run some code, when focus changed from one control to another control in vb.net form. Is there any event?
Posted 20-Dec-12 0:54am
Edited 20-Dec-12 22:42pm
v2
Comments
Krunal Rohit at 20-Dec-12 6:55am
   
What you are up to ?
Sachin Gargava at 20-Dec-12 7:08am
   
you have to make any action to change focus.how would like to change focus to change please give us to hint for that
kgmmurugesh at 20-Dec-12 23:47pm
   
when we use three textbox in a form, user enters input in first textbox after he goes to second textbox using mouse or tab key, now focus changed from textbox1 to textbox2.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Yes, see, for instance: Control.LostFocus Event at MSDN[^]
  Permalink  
Comments
kgmmurugesh at 21-Dec-12 2:27am
   
I am having 100's of controls in a form. I want to trace it in one common event.
CPallini at 21-Dec-12 3:20am
   
in fact, you can. You may add the same event handler to every control.
kgmmurugesh at 21-Dec-12 4:40am
   
It is very tedious job. I need single event( form_focuschanged ).
CPallini at 21-Dec-12 4:49am
   
No, it isn't. You may automate it.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Just Handle LostFocus Event All Control Using One Function (or Sub) as Displayed Below
Private Sub LostFocus(ByVal sender as System.Object, e As EventArgs) Handles _
Control1.LostFocus, Control2.LostFocus, Control3.LostFocus, Control4.LostFocus, _
Control5.LostFocus, Control6.LostFocus, Control7.LostFocus, Control8.LostFocus, _
Control9.LostFocus, Control10.LostFocus
 
'Now Use sender Parameter to Identify Which Control Lost it's Focus
Select Case sender.Name
case Control1.Name
    ' Write Code
case Control2.Name
    ' Write Code
case Control3.Name
    ' Write Code
case Control4.Name
    ' Write Code
case Control5.Name
    ' Write Code
case Control6.Name
    ' Write Code
case Control7.Name
    ' Write Code
case Control8.Name
    ' Write Code
case Control9.Name
    ' Write Code
case Control10.Name
    ' Write Code
case Else
    ' Write Code Here if Necessary

End Select
 
End Sub
  Permalink  
Comments
kgmmurugesh at 21-Dec-12 4:41am
   
It is very tedious job. I am having 100's of controls in a form. I need single event( form_focuschanged ).
Ashok19r91d at 21-Dec-12 4:43am
   
Mumm, may be, For the same I've gave example above, My Code handle 10 Controls, So you just make it to work for 100 controls
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

For Each ctrl As Control In Me.Controls
AddHandler ctrl.Enter, AddressOf ControlFocusChanged
Next
 

i write my code in controlfocuschanged event.
 

Thanks for your suggestions.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Depending on the type of control that is GAINING or LOSING control there are many events within the event cycles.
 
One place you can read more is here
http://www.thevbprogrammer.com/VBNET_08/08-00_VB2K_ControlBasics.htm[^]
 

 
Another place is here...
http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol.activecontrol(v=vs.90).aspx[^]
 

 
The second example is much better as it uses the ACTIVECONTROL method to return the currently active control.
 
Taking a step back an looking at the larger picture allows us modify the sample in the second link...
 
such as for example...
 
Public Class Form1
 
    Private mPreviousControl As Control = Nothing
    Private mActiveControl As Control = Nothing
 
    Public Sub MyActiveControl(sender As System.Object, e As System.EventArgs)
        If (sender Is Nothing) = False Then
            mPreviousControl = mActiveControl
            mActiveControl = sender
            ListBox1.Items.Add("Active control is: " & mActiveControl.Name & Chr(13) & "Previous control was: " & mPreviousControl.Name)
        End If
    End Sub
 
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        MyActiveControl(sender, e)
    End Sub
 
    Private Sub Button1_GotFocus(sender As System.Object, e As System.EventArgs) Handles Button1.GotFocus
        MyActiveControl(sender, e)
    End Sub
 
    Private Sub CheckBox1_GotFocus(sender As System.Object, e As System.EventArgs) Handles CheckBox1.GotFocus
        MyActiveControl(sender, e)
    End Sub
 
    Private Sub ComboBox1_GotFocus(sender As System.Object, e As System.EventArgs) Handles ComboBox1.GotFocus
        MyActiveControl(sender, e)
    End Sub
 
    Private Sub ListBox1_GotFocus(sender As System.Object, e As System.EventArgs) Handles ListBox1.GotFocus
        MyActiveControl(sender, e)
    End Sub
 
    Private Sub Label1_GotFocus(sender As System.Object, e As System.EventArgs) Handles Label1.GotFocus
        MyActiveControl(sender, e)
    End Sub
 
    Private Sub RadioButton1_GotFocus(sender As System.Object, e As System.EventArgs) Handles RadioButton1.GotFocus
        MyActiveControl(sender, e)
    End Sub
 
    Private Sub TextBox1_GotFocus(sender As System.Object, e As System.EventArgs) Handles TextBox1.GotFocus
        MyActiveControl(sender, e)
    End Sub
 
    Private Sub PictureBox1_GotFocus(sender As System.Object, e As System.EventArgs) Handles PictureBox1.GotFocus
        MyActiveControl(sender, e)
    End Sub
 
End Class
 

Using the sample above you can simply drop one each of the following controls
Button
Checkbox
Listbox
Combobox
Label
Radiobutton
Textbox
Picturebox
 
and then as you tab through the controls on the form the information is added to the listbox to show you which control is CURRENTLY active and which control was PREVIOSULY active.
 
The downside to this is off course that you MUST call the MyActiveSub procedure Immediately in your controls so that its gaining focus can be catalogued.
 
Ideally though, you really should process the event actions within the procedure for the relevant control as that is the preferred text-book method and standard procedure, after all, thats why the methods exist!
 

Additionally
Off course you dont need ANY of the above and can simply interogate the the Forms ACTIVECONTROL property to findout which control on the form is currently active such as....
 
 
 dim some_variable as string
 dim some_control as control
 
 some_variable = me.activecontrol.name
 some_control = me.activecontrol
 
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

you can do this with set tab order.
go to /view/tab order in visual studio and set tab order
 
or see the link to guide http://www.devasp.net/net/articles/display/831.html[^]
 
Hope will solve your problem...
  Permalink  
v3
Comments
kgmmurugesh at 21-Dec-12 2:29am
   
I know tab order very well. I think you are not understand my question.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Shai Vashdi 440
1 Sergey Alexandrovich Kryukov 221
2 OriginalGriff 183
3 Tadit Dash 160
4 Guruprasad.K.Basavaraju 125
0 Sergey Alexandrovich Kryukov 9,280
1 OriginalGriff 5,473
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 28 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid