Active Control Marker

Today, while working on my project, I recalled that some of the data entry pages in my project have a lot of textboxes, which may confuse the end-user once he starts entering information. My idea was to make the textbox that has focus visually different from the rest, so I came up with the following code snippet, that does the actual work:

Public Sub ShowActiveTextbox(ByVal txt As TextBox)

    txt.Attributes.Add("onfocus", "this.className='class1'")
    txt.Attributes.Add("onblur", "this.className='class2'")

End Sub

This code would work just fine if I had only one or two Textboxes on my page. However, some of the pages may contain a dozen of those textboxes, so I came up with another useful method:

Public Sub ShowActiveTextbox(ByVal col As ControlCollection)

    For Each Ctl As Control In col

       If Ctl.GetType().Name = "Textbox" Then

           Call ShowActiveTextbox(Ctl)

        Else

            'Handle TextBoxes contained in container controls, such as panel
            If
ctl.Controls.Count > 0 Then

               Call ShowActiveTextBox(ctl.Controls)

            End If

       End If

    Next
 End Sub

 Now we have a method that will handle any number of controls on a specific page and will apply the onFocus and onBlur event handlers to all Textboxes on that page.

No Comments