Como posso garantir que as barras de rolagem não se sobreponham ao conteúdo?

StackOverflow https://stackoverflow.com/questions/26721

  •  09-06-2019
  •  | 
  •  

Pergunta

Ao criar controles de usuário roláveis ​​com .NET e WinForms, encontrei repetidamente situações em que, por exemplo, uma barra de rolagem vertical aparece, sobrepondo o conteúdo do controle, fazendo com que uma barra de rolagem horizontal também seja necessária.Idealmente, o conteúdo diminuiria um pouco para abrir espaço para a barra de rolagem vertical.

Minha solução atual tem sido apenas manter meus controles fora dos 40 pixels da extrema direita ou de forma que a barra de rolagem vertical ocupe o lugar.Como este ainda é efetivamente o espaço do cliente para o controle, a barra de rolagem horizontal ainda aparece quando é coberta pela barra de rolagem vertical, mesmo que nenhum controle esteja oculto.Mas pelo menos o usuário não precisa realmente usar a barra de rolagem horizontal que aparece.

Existe uma maneira melhor de fazer tudo isso funcionar?Alguma maneira de evitar que as barras de rolagem desnecessárias e indesejadas apareçam?

Foi útil?

Solução

Você precisará redimensionar ligeiramente os controles para acomodar a largura da barra de rolagem vertical.Uma maneira de conseguir isso é através do acoplamento.Em vez de apenas colocar controles no formulário, você terá que brincar um pouco com painéis, preenchimento, dimensionamento mínimo/máximo e encaixe.

Aqui está um exemplo de código que você pode colocar atrás de um novo Form1 em branco.Redimensione o formulário, no designer ou em tempo de execução, e você verá que a barra de rolagem horizontal não é mostrada e os campos não ficam sobrepostos.Também dei aos campos uma largura máxima para garantir:

#region Windows Form Designer generated code

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent() {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.label1 = new System.Windows.Forms.Label();
        this.panel1 = new System.Windows.Forms.Panel();
        this.panel2 = new System.Windows.Forms.Panel();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.label2 = new System.Windows.Forms.Label();
        this.panel1.SuspendLayout();
        this.panel2.SuspendLayout();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
        this.textBox1.Location = new System.Drawing.Point(32, 0);
        this.textBox1.MaximumSize = new System.Drawing.Size(250, 0);
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(250, 20);
        this.textBox1.TabIndex = 0;
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Dock = System.Windows.Forms.DockStyle.Left;
        this.label1.Location = new System.Drawing.Point(0, 0);
        this.label1.Name = "label1";
        this.label1.Padding = new System.Windows.Forms.Padding(0, 3, 0, 0);
        this.label1.Size = new System.Drawing.Size(32, 16);
        this.label1.TabIndex = 0;
        this.label1.Text = "Field:";
        // 
        // panel1
        // 
        this.panel1.Controls.Add(this.textBox1);
        this.panel1.Controls.Add(this.label1);
        this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
        this.panel1.Location = new System.Drawing.Point(0, 0);
        this.panel1.Name = "panel1";
        this.panel1.Size = new System.Drawing.Size(392, 37);
        this.panel1.TabIndex = 2;
        // 
        // panel2
        // 
        this.panel2.Controls.Add(this.textBox2);
        this.panel2.Controls.Add(this.label2);
        this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
        this.panel2.Location = new System.Drawing.Point(0, 37);
        this.panel2.Name = "panel2";
        this.panel2.Size = new System.Drawing.Size(392, 37);
        this.panel2.TabIndex = 3;
        // 
        // textBox2
        // 
        this.textBox2.Dock = System.Windows.Forms.DockStyle.Top;
        this.textBox2.Location = new System.Drawing.Point(32, 0);
        this.textBox2.MaximumSize = new System.Drawing.Size(250, 0);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(250, 20);
        this.textBox2.TabIndex = 0;
        // 
        // label2
        // 
        this.label2.AutoSize = true;
        this.label2.Dock = System.Windows.Forms.DockStyle.Left;
        this.label2.Location = new System.Drawing.Point(0, 0);
        this.label2.Name = "label2";
        this.label2.Padding = new System.Windows.Forms.Padding(0, 3, 0, 0);
        this.label2.Size = new System.Drawing.Size(32, 16);
        this.label2.TabIndex = 0;
        this.label2.Text = "Field:";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoScroll = true;
        this.ClientSize = new System.Drawing.Size(392, 116);
        this.Controls.Add(this.panel2);
        this.Controls.Add(this.panel1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.panel1.ResumeLayout(false);
        this.panel1.PerformLayout();
        this.panel2.ResumeLayout(false);
        this.panel2.PerformLayout();
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Panel panel1;
    private System.Windows.Forms.Panel panel2;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Label label2;

Outras dicas

Se seus controles estiverem dentro de um painel, tente definir a propriedade AutoScroll do Painel como False.Isso ocultará as barras de rolagem.Espero que isso aponte você na direção certa.

myPanel.AutoScroll = False
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top