Window shapes: scrolling programmatically

I am developing a Windows Forms Application. And I have the following problem, there is a panel in the form, and in that panel, I have several controls (just a label with a textbox, the number is determined at runtime). This panel is sized less than the sum of all dynamically added controls. So, I need a scroll. Well, the idea is this: when the user opens the form: the first of the controls should be focused, the user enters text and presses the enter key, the next control should be focused, and so before completion.

Well, it is very likely that not all controls fit into the panel, so I want that when a control inside the panel receives focus, the panel scrolls to allow the user to see the control and let him see what he is entering into the panel's textbox.

I hope to be clear.

here is some code, this code is used to create controls and added to the panel:

    List<String> titles = this.BancaService.ValuesTitle();
    int position = 0;
    foreach (String title in titles)
    {
         BancaInputControl control = new BancaInputControl(title);
         control.OnInputGotFocus = (c) => {
                 //pnBancaInputContainer.VerticalScroll.Value = 40;
                 //pnBancaInputContainer.AutoScrollOffset = new Point(0, c.Top);
                 // HERE, WHAT CAN I DO?
                 };
         control.Top = position;
         this.pnBancaInputContainer.Controls.Add(control);
         position += 10 + control.Height;
    }

      

+3


source to share


1 answer


If you set AutoScroll to true, this will be taken care of automatically. As for the idea that Enter should give focus to the next field, the best solution would be to execute the input as if it were the TAB key in BancaInputControl :

protected override void OnKeyDown(KeyEventArgs e)
{
    base.OnKeyDown(e);

    if (e.KeyCode == Keys.Enter)
    {
        e.Handled = true;
        //  Move focus to next control in parent container
        Parent.SelectNextControl(this, true, true, false, false);
    }
}

      



If BancaInputControl is a composite control (a UserControl that contains other controls), each child control must hook up a KeyDown event to that handler. It tries to move focus to the next control in the BancaInputControl ; if it fails, moves focus to the parent container with the next control.

private void textBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        e.Handled = true;
        if (!SelectNextControl((Control)sender, true, true, false, false))
        {
            Parent.SelectNextControl(this, true, true, false, false);
        }
    }
}

      

+3


source







All Articles