Why I can set same Tabindex for many control in C# WPF

Mojtaba_Hakim 321 Reputation points
2022-06-06T13:11:00.387+00:00

I'm using C# WPF in visual Studio

Why I can set same Tabindex for many control in my projects and then even works !

XAML:

<Grid>
<TextBox x:Name="Textbox0" TabIndex="0" HorizontalAlignment="Left" Height="23" Margin="145,65,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="505"/>
<TextBox x:Name="Textbox1" TabIndex="1" HorizontalAlignment="Left" Height="23" Margin="145,132,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="505"/>
<TextBox x:Name="Textbox2" TabIndex="2" HorizontalAlignment="Left" Height="23" Margin="145,195,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="505"/>
<Button x:Name="Textbox3" TabIndex="3" Content="Button" HorizontalAlignment="Left" Height="40" Margin="145,250,0,0" VerticalAlignment="Top" Width="505"/>
</Grid>

And is there an easy way to manage Tabindexes like Windows Forms that were done in a simple, Viusal way?

please help me to Understand it

Developer technologies | Windows Forms
Developer technologies | Windows Presentation Foundation
Developer technologies | XAML
Developer technologies | C#
{count} votes

3 answers

Sort by: Most helpful
  1. Michael Taylor 60,326 Reputation points
    2022-06-06T15:04:31.1+00:00

    Tab index controls the order in which controls are focused when tabbing. If 2 controls have the same index then which would Windows choose? You can have duplicate indice BTW but you shouldn't have dups in the same parent control. For example in a tab control each tab page will have its own tab index ordering and they can overlap but all the controls on a single page should have separate indice.

    The Tab Layout feature you are talking about for Winforms has actually been broken for well over a year. When they started the migration to the new designer it was disabled so we had to do things the hard way. It was recently fixed in VS 2022 back to what it was before. AFAIK WPF has never had this feature nor does it exist now. In general WPF will figure out the tab order. If you want a specific tab order then you'll have to set it yourself manually.

    1 person found this answer helpful.

  2. Hui Liu-MSFT 48,681 Reputation points Microsoft External Staff
    2022-06-07T06:23:42.4+00:00

    If you want to visually set the TabIndex order of a control, you can right-click the control and choose Properties.

    Set the TabIndex for the control on the Properties window:
    208916-image.png

    Update:

    Reference documentation: TabIndex

    A tab index can consist of any valid integer greater than or equal to zero, lower numbers being earlier in the tab order. If more than one control on the same parent control has the same tab index, the z-order of the controls determines the order to cycle through the controls.

    Xaml:

    <StackPanel>  
         <TextBox x:Name="Textbox0" TabIndex="0"  Height="23" Margin="5,5,0,0" TextWrapping="Wrap" Text="TextBox" Width="505"/>  
                <TextBox x:Name="Textbox1" TabIndex="4"  Height="23" Margin="5,5,0,0" TextWrapping="Wrap" Text="TextBox"  Width="505"/>  
                <TextBox x:Name="Textbox2" TabIndex="1"  Height="23" Margin="5,5,0,0" TextWrapping="Wrap" Text="TextBox"  Width="505"/>  
                <Button x:Name="Textbox3" TabIndex="3" Content="Button" Height="40" Margin="5" Width="505"/>  
                <Button x:Name="Textbox5" TabIndex="2" Content="Button" Height="40" Margin="5"  Width="505"/>  
                <Button x:Name="Textbox6" TabIndex="2" Content="Button"  Height="40" Margin="5" Width="505"/>  
    </StackPanel>  
    

    The result:
    Focus on the 2 controls with a tabindex value of 2, first focus on Textbox 5, and then focus on Textbox 6.
    209408-4.gif


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  3. Mojtaba_Hakim 321 Reputation points
    2022-06-09T05:00:29.853+00:00

    thanks all ,

    is this helpful ? :

     public static void SettingTabindexha(params FrameworkElement[] FRELM)  
            {  
                bool onTimedone = false;  
                for (int i = -2147483647; i <= 2147483647; i++)  
                {  
                    if (!onTimedone)  
                    {  
                        foreach (var ElementITM in FRELM)  
                        {  
                            if (ElementITM is TextBox) ((TextBox)ElementITM).TabIndex = i;  
                            if (ElementITM is DataGrid) ((DataGrid)ElementITM).TabIndex = i;  
                            if (ElementITM is ComboBox) ((ComboBox)ElementITM).TabIndex = i;  
                            if (ElementITM is CheckBox) ((CheckBox)ElementITM).TabIndex = i;  
                            if (ElementITM is Button) ((Button)ElementITM).TabIndex = i;  
                            if (ElementITM is RadioButton) ((RadioButton)ElementITM).TabIndex = i;  
                            i++;  
                        }  
                        onTimedone = true;  
                    }  
                    else { break; }  
                }  
            }  
      
    //Usage :  
    SettingTabindexha(  
    TextBox1,  
    TextBox2,  
    TextBox3  
           );  
      
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.