0

I have a panel and on this panel, I create a number of TextBoxes, one under the other. The number of TextBoxes is greater than the vertical size of the panel can accommodate.
I have two problems:
1. The first TextBox appears at a large vertical distance from the top of the panel, even though its vertical coordinate is 5.
2. I want to automatically scroll to the bottom of the panel so that the last TextBoxes get visible, but I am unable to do that.
The first TextBoxes remain visible.
Here is the code I use:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestPanelScroll    
{
    public partial class Form1 : Form
    {
        private const int _verticalSpacing = 1;

    public Form1()
    {
        InitializeComponent();

        this.StartPosition = FormStartPosition.CenterScreen;
        this.BackColor = Color.BurlyWood;
        this.ClientSize = new Size(800, 800);

        this.SuspendLayout();
        panelLeftForNodeCreationAndNodeMembersEditingMode = new Panel();
        panelLeftForNodeCreationAndNodeMembersEditingMode.SuspendLayout();
        panelLeftForNodeCreationAndNodeMembersEditingMode.Location = new Point(3, 56);
        panelLeftForNodeCreationAndNodeMembersEditingMode.Size = new Size(461, 187);
        panelLeftForNodeCreationAndNodeMembersEditingMode.Name = "panelLeftForNodeCreationAndNodeMembersEditingMode";
        panelLeftForNodeCreationAndNodeMembersEditingMode.BorderStyle = BorderStyle.Fixed3D;
        panelLeftForNodeCreationAndNodeMembersEditingMode.Visible = true;
        panelLeftForNodeCreationAndNodeMembersEditingMode.CausesValidation = false;
        panelLeftForNodeCreationAndNodeMembersEditingMode.TabStop = false;
        panelLeftForNodeCreationAndNodeMembersEditingMode.TabIndex = 0;
        panelLeftForNodeCreationAndNodeMembersEditingMode.BackColor = Color.LightGray;
        panelLeftForNodeCreationAndNodeMembersEditingMode.SetAutoScrollMargin(0, 40);
        panelLeftForNodeCreationAndNodeMembersEditingMode.HorizontalScroll.Maximum = 0;
        panelLeftForNodeCreationAndNodeMembersEditingMode.AutoScroll = false;
        panelLeftForNodeCreationAndNodeMembersEditingMode.HorizontalScroll.Visible = false;
        panelLeftForNodeCreationAndNodeMembersEditingMode.AutoScroll = true;
        panelLeftForNodeCreationAndNodeMembersEditingMode.Parent = this;
        this.Controls.Add(panelLeftForNodeCreationAndNodeMembersEditingMode);
        panelLeftForNodeCreationAndNodeMembersEditingMode.SendToBack();
        panelLeftForNodeCreationAndNodeMembersEditingMode.ResumeLayout(false);
        panelLeftForNodeCreationAndNodeMembersEditingMode.PerformLayout();

        TextBox currentTextBox = null;
        int xCoordinate = 30;
        for (int i = 1; i <= 37; i++)
        {
            currentTextBox = new TextBox();
            currentTextBox.Name = i.ToString();
            currentTextBox.Size = new Size(85, 20);
            currentTextBox.Tag = i.ToString();
            currentTextBox.TabIndex = i - 1;
            currentTextBox.TextAlign = HorizontalAlignment.Center;
            currentTextBox.Text = currentTextBox.Name;
            currentTextBox.Enabled = true;
            currentTextBox.ReadOnly = true;
            currentTextBox.CausesValidation = true;
            currentTextBox.TabStop = true;
            currentTextBox.BackColor = Color.DarkGray;
            currentTextBox.Parent = panelLeftForNodeCreationAndNodeMembersEditingMode;
            int yCoordinate = yCoordinate = 0 + (i - 1) * _verticalSpacing * 33 + _verticalSpacing * 5;
            currentTextBox.Location = new Point(xCoordinate, yCoordinate);
            textBox1.AppendText("TextBox " + currentTextBox.Name + " verticalCoordinate = " + yCoordinate.ToString() + Environment.NewLine);
            int value = (currentTextBox.Location.Y + currentTextBox.Height) * 2 + _verticalSpacing * 40;
            panelLeftForNodeCreationAndNodeMembersEditingMode.AutoScrollPosition = new Point(0, value);
            panelLeftForNodeCreationAndNodeMembersEditingMode.VerticalScroll.Maximum = 300;
            int minimum = panelLeftForNodeCreationAndNodeMembersEditingMode.VerticalScroll.Minimum;
            int maximum = panelLeftForNodeCreationAndNodeMembersEditingMode.VerticalScroll.Maximum;
            //panelLeftForNodeCreationAndNodeMembersEditingMode.VerticalScroll.Value = value;
            //panelLeftForNodeCreationAndNodeMembersEditingMode.VerticalScroll.Value = value;
            panelLeftForNodeCreationAndNodeMembersEditingMode.SetAutoScrollMargin(0, 40);
            panelLeftForNodeCreationAndNodeMembersEditingMode.ScrollControlIntoView(currentTextBox);
            panelLeftForNodeCreationAndNodeMembersEditingMode.PerformLayout();
        }
        this.ResumeLayout();
    }

    private Panel panelLeftForNodeCreationAndNodeMembersEditingMode;

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

}
I use Visual Studio 2010.
Any idea would be greatly appreciated.
Thank you in advance. My intention is to understand what I am doing wrong and how to correct my code and not to find a workaround (a different way to program it).

user2102327
  • 59
  • 2
  • 6
  • 19
  • Create the Textboxes in the Load event of the form – Jonathan Applebaum Dec 25 '22 at 06:11
  • The solution is: 1. Create the Textboxes in the Load event of the form (as Jonathan Applebaum suggested). 2. place these 2 statements in the Form.Shown event handler: panelLeft.ScrollControlIntoView(_lastCreatedTextBox); panelLeft.PerformLayout(); – user2102327 Dec 26 '22 at 06:27

0 Answers0