How to: Enable Tabbing Between Shapes (Visual Studio)

Line and shape controls do not have TabStop or TabIndex properties, but you can still enable tabbing among them. In the following example, pressing both the CTRL and the TAB keys will tab between shapes; pressing only the TAB key will tab between the buttons.

Note

Your computer might show different names or locations for some of the Visual Studio user interface elements in the following instructions. The Visual Studio edition that you have and the settings that you use determine these elements. For more information, see Customizing Development Settings.

To enable tabbing among shapes

  1. Drag three RectangleShape controls and two Button controls from the Toolbox to a form.

  2. In the Code Editor, add an Imports or using statement at the top of the module:

    Imports Microsoft.VisualBasic.PowerPacks
    
    using Microsoft.VisualBasic.PowerPacks;
    
  3. Add the following code in an event procedure:

    Private Sub Shapes_PreviewKeyDown(
        ByVal sender As Object, 
        ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs
      ) Handles RectangleShape1.PreviewKeyDown, 
                RectangleShape2.PreviewKeyDown, 
                RectangleShape3.PreviewKeyDown
    
        Dim sh As Shape
        ' Check for the Control and Tab keys. 
        If e.KeyCode = Keys.Tab And e.Modifiers = Keys.Control Then 
            ' Find the next shape in the order.
            sh = ShapeContainer1.GetNextShape(sender, True)
            ' Select the next shape.
            ShapeContainer1.SelectNextShape(sender, False, True)
        End If 
    End Sub
    
    private void shapes_PreviewKeyDown(Shape sender, System.Windows.Forms.PreviewKeyDownEventArgs e)
    {
        Shape sh;
        // Check for the Control and Tab keys. 
        if (e.KeyCode == Keys.Tab && e.Modifiers == Keys.Control)
        // Find the next shape in the order.
        {
            sh = shapeContainer1.GetNextShape(sender, true);
            // Select the next shape.
            shapeContainer1.SelectNextShape(sender, false, true);
        }
    }
    
  4. Add the following code in the Button1_PreviewKeyDown event procedure:

    Private Sub Button1_PreviewKeyDown(
        ByVal sender As Object, 
        ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs
      ) Handles Button1.PreviewKeyDown
    
        ' Check for the Control and Tab keys. 
        If e.KeyCode = Keys.Tab And e.Modifiers = Keys.Control Then 
            ' Select the first shape.
            RectangleShape1.Select()
        End If 
    End Sub
    
    private void button1_PreviewKeyDown(object sender, System.Windows.Forms.PreviewKeyDownEventArgs e)
    {
        // Check for the Control and Tab keys. 
        if (e.KeyCode == Keys.Tab & e.Modifiers == Keys.Control)
        // Select the first shape.
        {
            rectangleShape1.Select();
        }
    }
    

See Also

Tasks

How to: Draw Shapes with the OvalShape and RectangleShape Controls (Visual Studio)

How to: Draw Lines with the LineShape Control (Visual Studio)

Concepts

Introduction to the Line and Shape Controls (Visual Studio)