面板中的面板,自动滚动 [英] Panel in a panel, autoscroll

查看:207
本文介绍了面板中的面板,自动滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用WinForms遇到我的真实程序的问题后,我正在做一个极简测试应用程序.我将一个小面板(孩子)放在一个较大的面板(父母)中.较大的面板将AutoScroll设置为true.子面板的默认锚点设置为顶部"和左侧".子面板未固定.

I'm doing a minimalist test app after encountering an issue with my real program, using WinForms. I put a small panel (child) inside a bigger panel (parent). The bigger panel has AutoScroll set to true. The child panel has the default Anchors set to Top and Left. The child panel is not docked.

我想要的行为是,每当较小面板的位置偏移太远(顶部,底部,左侧或右侧)时,滚动条就会出现.问题在于,仅当它在正确的位置或在底部的位置太远时才起作用.当滚动条的顶部过多或左侧过多时,都不会出现滚动条.

The behavior I want is for scrollbars to appear whenever the smaller panel's location is too offset, either top, bottom, left or right. The problem is that it only works when it's too far right, or too far in the bottom. No scrollbars appear when it's too much in the top or too much in the left directions.

我使用两个简单的按钮来强制子面板的位置向左200像素,或向右200像素,以快速轻松地修改其位置.

I use two simple buttons to force the child panel's location 200 pixels to the left, or 200 pixels to the right to have a quick way of easily modifying its position.

这是我的Form1()代码:

Here's my Form1() code:

        private void button1_Click(object sender, EventArgs e)
    {
        childPanel.Location = new Point(childPanel.Location.X - 200, childPanel.Location.Y);
        hostPanel.Invalidate();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        childPanel.Location = new Point(childPanel.Location.X + 200, childPanel.Location.Y);
        hostPanel.Invalidate();
    }

这是设计师代码:

       private void InitializeComponent()
    {
        this.hostPanel = new System.Windows.Forms.Panel();
        this.childPanel = new System.Windows.Forms.Panel();
        this.moveChildLeft = new System.Windows.Forms.Button();
        this.moveChildRight = new System.Windows.Forms.Button();
        this.hostPanel.SuspendLayout();
        this.SuspendLayout();
        // 
        // hostPanel
        // 
        this.hostPanel.AutoScroll = true;
        this.hostPanel.BackColor = System.Drawing.SystemColors.AppWorkspace;
        this.hostPanel.Controls.Add(this.childPanel);
        this.hostPanel.Location = new System.Drawing.Point(239, 48);
        this.hostPanel.Name = "hostPanel";
        this.hostPanel.Size = new System.Drawing.Size(400, 400);
        this.hostPanel.TabIndex = 0;
        // 
        // childPanel
        // 
        this.childPanel.BackColor = System.Drawing.SystemColors.ButtonHighlight;
        this.childPanel.Location = new System.Drawing.Point(29, 62);
        this.childPanel.Name = "childPanel";
        this.childPanel.Size = new System.Drawing.Size(342, 259);
        this.childPanel.TabIndex = 0;
        // 
        // moveChildLeft
        // 
        this.moveChildLeft.Location = new System.Drawing.Point(61, 81);
        this.moveChildLeft.Name = "moveChildLeft";
        this.moveChildLeft.Size = new System.Drawing.Size(75, 23);
        this.moveChildLeft.TabIndex = 1;
        this.moveChildLeft.Text = "Left 200";
        this.moveChildLeft.UseVisualStyleBackColor = true;
        this.moveChildLeft.Click += new System.EventHandler(this.button1_Click);
        // 
        // moveChildRight
        // 
        this.moveChildRight.Location = new System.Drawing.Point(61, 111);
        this.moveChildRight.Name = "moveChildRight";
        this.moveChildRight.Size = new System.Drawing.Size(75, 23);
        this.moveChildRight.TabIndex = 2;
        this.moveChildRight.Text = "Right 200";
        this.moveChildRight.UseVisualStyleBackColor = true;
        this.moveChildRight.Click += new System.EventHandler(this.button2_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(1018, 549);
        this.Controls.Add(this.moveChildRight);
        this.Controls.Add(this.moveChildLeft);
        this.Controls.Add(this.hostPanel);
        this.Name = "Form1";
        this.Text = "Form1";
        this.hostPanel.ResumeLayout(false);
        this.ResumeLayout(false);
    }

推荐答案

最终,您可能希望将您的应用程序转换为WPF.从那以后,Winform被判死刑.

Eventually, you might want to convert your app to WPF. Since, Winform is condemned to a small death.

这篇关于面板中的面板,自动滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆