I have a registration sort of page in my application. It has lots of textboxes,dropdowns and many other controls on it. Out of them some are mandatory and others are not. Form will be submitted only if mandatory fields are filled. Mandatory fields are differentiated from others by using specific CSS classes for them. For eg. If a textbox is mandatory then its css is txtbox_req otherwise its css would be just txtbox.

Now the problem is...
I have dropdown on the top of the page which has two options like 'Mandatory' and 'All'. Now my requirement is,..when user selects all..the tab index must be normal and on pressing tab the focus must be changing normally as usual betwwen all the controls on the page...If the user selects 'mandatory'..then the tab index of all the controls must be changed..and on pressing tab..the focus must be changing only to mandatory textboxes or dropdowns or any other controls..So, can anyone please suggest how to change the tab index of all the controls on selection of options form a dropdown..??!!

Recommended Answers

All 3 Replies

Firstly, set ALL your controls on the form, including text boxes's TabStop Property to False.

'Gets or sets the character corresponding to the key pressed.

'Declaration

Public Property KeyChar As Char
'Usage

Dim instance As KeyPressEventArgs
Dim value As Char

value = instance.KeyChar

instance.KeyChar = value

Use the KeyChar property to sample keystrokes at run time and to modify keystrokes under special run-time circumstances. For example, you can use KeyChar to disable non-numeric keypresses when the user enters a ZIP code, change all alphabetical keypresses to uppercase in a data entry field, or monitor the keyboard or other key input device for specific key combinations.

You can get or set the following keys:

a-z, A-Z.

CTRL.

Punctuation marks.

Number keys, both across the top of the keyboard and on the numeric keypad.

ENTER.

You cannot get or set the following keys:

The TAB key.

INSERT and DELETE.

HOME.

END.

PAGE UP and PAGE DOWN.

F1-F2.

ALT.

Arrow keys.

Example -

'The following example creates a TextBox control. The keypressed method uses 'the KeyChar property to check whether the ENTER key pressed. If the ENTER key is 'pressed, the Handled property is set to true, which indicates the event is 'handled.

Imports System
Imports System.Windows.Forms

Public Class Form1
    Inherits Form

    Public Sub New()
        ' Create a TextBox control.
        Dim tb As New TextBox()
        Me.Controls.Add(tb)
        AddHandler tb.KeyPress, AddressOf keypressed
    End Sub 'New

    Private Sub keypressed(ByVal o As [Object], ByVal e As KeyPressEventArgs)
        ' The keypressed method uses the KeyChar property to check 
        ' whether the ENTER key is pressed. 

        ' If the ENTER key is pressed, the Handled property is set to true, 
        ' to indicate the event is handled.

        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            e.Handled = True
        End If
    End Sub 'keypressed

    Public Shared Sub Main()
        Application.Run(New Form1())
    End Sub 'Main
End Class 'Form1

Now use the e.Handled = True to set focus to the next text box.

To get the correct focus after selecting from the combo, use a if then statement within a sub to check the values. If only "Mandatory" was selected, disable the other text boxes. They can not receive focus whilst disabled, making sure that the next text box under Mandatory gets the focus.

See if this helps.

'//------- Prerequisites: 1 ComboBox, 3 TextBoxes. -------\\
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.Items.Add("Mandatory") : ComboBox1.Items.Add("All") '// add items.
        ComboBox1.SelectedIndex = 0 '// select first option.
        ComboBox1.TabIndex = 999 '// set index presumably to very last index.
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim ctl() As Control = {TextBox1, TextBox2, TextBox3} '// set arrays of controls.
        Select Case ComboBox1.SelectedIndex
            Case 0
                ctl(0).TabIndex = "0"
                ctl(2).TabIndex = "1"
            Case 1
                ctl(0).TabIndex = "0"
                ctl(1).TabIndex = "1"
                ctl(2).TabIndex = "2"
        End Select
    End Sub
End Class

the information u provided was helpful. But my concern is...the UI screen has been already designed using asp.net. Now I have to modify the tab property for all the controls. To use the method u have specified I need to redesign the entire screen again from code behind. So, can anyone suggest how to just modify the Tab Index property basing upon the css class of the controls..//!!

One more concern is how can we traverse all the controls in a page in page load function of the page from code behind. Is there any function or if loop so that we can traverse all the controls (irrespective of textboxes or comboboxes) in the page and their css classess also..???

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.