-1

Is there any code for the PageUp and PageDown keys to navigate to the previous and next textboxes in addition to the Up and Down arrow keys in c sharp , .net framework .

private void Control_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.PageUp || e.KeyCode == Keys.PageDown)
    {
        if (sender is TextBox currentTextBox)
        {
            // Get the index of the current textbox in the parent container's control collection
            int currentIndex = currentTextBox.Parent.Controls.IndexOf(currentTextBox);

            // Calculate the index of the previous or next textbox based on the key pressed
            int newIndex = e.KeyCode == Keys.PageUp ? currentIndex - 1 : currentIndex + 1;

            // Check if the new index is within the valid range of textboxes
            if (newIndex >= 0 && newIndex < currentTextBox.Parent.Controls.Count)
            {
                TextBox newTextBox = (TextBox)currentTextBox.Parent.Controls[newIndex];

                // Set focus to the previous or next textbox
                newTextBox.Focus();
            }
        }
    }
}
Siya
  • 1
  • 4
  • 1
    The normal way is to use `Tab` and `Shift-Tab` along with the `TabIndex` of the control. – Poul Bak Jun 20 '23 at 07:00
  • The `IndexOf` the control in the controls collection is meaningless. You have to look through all the controls and look for the one with the next [TabIndex](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.tabindex?view=windowsdesktop-7.0) (or possibly [Location](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.location?view=windowsdesktop-7.0)). – John Wu Jun 20 '23 at 07:18
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jun 20 '23 at 07:27
  • The tab order is specified by the `TabIndex` values, not the order in which the controls were added to the form. As the duplicate shows, the next control in the tab order is returned by [GetNextControl](https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.getnextcontrol?view=windowsdesktop-7.0) – Panagiotis Kanavos Jun 20 '23 at 07:56
  • The order in the Controls collection is the order controls are created and added in `InitializeComponents`. That in turn is the order the *developer* added controls while designing the form. Each time a control is added, another section is added in `InitializeComponents`. There's no attempt to order things visually - that doesn't even have meaning with layout controls. If you add a textbox in the third row of a grid, then another in the first, the "third" textbox will appear first in the `Controls` list. – Panagiotis Kanavos Jun 20 '23 at 07:59
  • To ensure users can move from one textbox to the next, applications since the 1990s control the tab order. In this case, by setting the TabIndex. The label of a textbox is important and should be included in the tab order. Labels include the shortcut of their controls, so hitting the shortcut would move focus to the label and then automatically move to the control. – Panagiotis Kanavos Jun 20 '23 at 08:02

1 Answers1

-1

I hope this could help you.

private void Control_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.PageUp)
    {
        SelectNextTextBox(-1);
        e.Handled = true;
    }
    else if (e.KeyCode == Keys.PageDown)
    {
        SelectNextTextBox(1);
        e.Handled = true;
    }
}
private void SelectNextTextBox(int direction)
{
    var textboxes = GetAllTextBoxes(this); // Replace 'this' with your container control if necessary
    var currentTextBox = textboxes.FirstOrDefault(t => t.Focused);

    if (currentTextBox != null)
    {
        var currentIndex = textboxes.IndexOf(currentTextBox);
        var nextIndex = (currentIndex + direction + textboxes.Count) % textboxes.Count;
        textboxes[nextIndex].Focus();
    }
}
private List<TextBox> GetAllTextBoxes(Control control)
{
    var textboxes = new List<TextBox>();

    foreach (Control childControl in control.Controls)
    {
        if (childControl is TextBox textBox)
            textboxes.Add(textBox);
        else
            textboxes.AddRange(GetAllTextBoxes(childControl));
    }

    return textboxes;
}
  • 1
    Would you be able to explain what this code does, why it works (it doesn't) or how it's even related to the question? This sounds like a ChatGPT-generated non-answer that doesn't understand how desktop applications work. – Panagiotis Kanavos Jun 20 '23 at 07:53