Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Updated 20-Dec-12 22:42pm
v2
Comments
[no name] 20-Dec-12 6:55am    
What you are up to ?
Sachin Gargava 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 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.

Yes, see, for instance: Control.LostFocus Event at MSDN[^]
 
Share this answer
 
Comments
kgmmurugesh 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 21-Dec-12 3:20am    
in fact, you can. You may add the same event handler to every control.
kgmmurugesh 21-Dec-12 4:40am    
It is very tedious job. I need single event( form_focuschanged ).
CPallini 21-Dec-12 4:49am    
No, it isn't. You may automate it.
Just Handle LostFocus Event All Control Using One Function (or Sub) as Displayed Below
VB
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
 
Share this answer
 
Comments
kgmmurugesh 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 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
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.
 
Share this answer
 
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...

VB
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....

VB
dim some_variable as string
dim some_control as control

some_variable = me.activecontrol.name
some_control = me.activecontrol
 
Share this answer
 
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...
 
Share this answer
 
v3
Comments
kgmmurugesh 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)



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