Hi,

I am developing a VB net application with lots of TextBox. I need to call a module everytime any of the textbox gets focus. I dont want to write code in every textbox gotfocus event, but a generic code which will call the module everytime any textbox gets focus.

Please help.

Pankaj

Recommended Answers

All 6 Replies

something like this should work:

Public Class Form1

	Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
		RecurseContainers(Me)
	End Sub

	Private Sub RecurseContainers(container As Control)
		For Each ctrl In container.Controls
			If TypeOf (ctrl) Is TextBox Then
				SetHandler(CType(ctrl, TextBox)) 'add handler to textbox
				Continue For
			End If
			If TypeOf (ctrl) Is Control Then 'panel, groupBox or any other container
				RecurseContainers(CType(ctrl, Control))
			End If
		Next
	End Sub

	Private Sub SetHandler(ctrl As TextBox)
		AddHandler ctrl.GotFocus, AddressOf TextBox_Focused
	End Sub

	Private Sub TextBox_Focused(sender As Object, e As EventArgs)
		Debug.WriteLine(CType(sender, TextBox).Name)
	End Sub

End Class

Dear GeekByChoiCe,

Thanks for the response. At outset it seemed the perfect solution, but when i went on to implement this I am having trouble.

Anything that I write in Textbox_Focused sub, gets into infinite loop and am unable to proceed.

I have been trying to figure this out for the whole day but have not succeeded. I have just moved from VB6 to VB net so might sound naive, your further help is highly solicited.

Pankaj

Could you please provide the code that you have added into the "Textbox_Focused" Sub?

I got the problem. I had added a simple message box in the sub, and as soon as i would press "ok" on message box, the testbox again gets the focus and i see the message box again.

So i have now understood the problem, but have no Idea how should I call a form without returning focus to my textbox.

Actually I want to show a NumericPad when the textbox gets focus and hide it after numbers have been entered, but again the focus shifts to textbox and i get numeric Pad. This gets into infinite loop.

Please Help.

Pankaj

this does the trick:

Private Sub Form1_Loaded(sender As Object, e As System.EventArgs) Handles Me.Load
		RecurseContainers(Me)
	End Sub

	Private Sub RecurseContainers(container As Control)
		For Each ctrl In container.Controls
			If TypeOf (ctrl) Is TextBox Then
				SetHandler(CType(ctrl, TextBox)) 'add handler to textbox
				Continue For
			End If
			If TypeOf (ctrl) Is Control Then 'panel, groupBox or any other container
				RecurseContainers(CType(ctrl, Control))
			End If
		Next
	End Sub

	Private Sub SetHandler(ctrl As TextBox)
		AddHandler ctrl.GotFocus, AddressOf TextBox_Focused
	End Sub

	Private Sub TextBox_Focused(sender As Object, e As EventArgs)
		RemoveHandler CType(sender, TextBox).GotFocus, AddressOf TextBox_Focused
		'option #1 - a new form
		Dim opt As New OptionsForm()
		opt.ShowDialog()
		'option #2 - a messagebox
		MsgBox(String.Format("You clicked {0}", CType(sender, TextBox).Name))
		'set focus back to the textbox BEFORE adding the handler back
		CType(sender, TextBox).Focus()
		'add handler
		SetHandler(CType(sender, TextBox))
	End Sub
commented: Quick and onspot +3

Dear GeekByChoiCe,

This did the trick :)

Thanks alot for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.