C#窗体高度工具滚动条 [英] C# Form Height implement Scroll Bar

查看:98
本文介绍了C#窗体高度工具滚动条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一些对象作为图片框,用户可以决定是否启动程序
应该生成多少个对象.

现在,如果用户说要超过7,就会出现问题,
图片框脱离表单.

我想实现的方法是,如果添加了7个以上的对象,那么滚动条应该是可见的,并且可以滚动到最后一个对象.

谢谢

问候Niko



i have some objects as pictureboxes, and the user can decide on programm start
how many object should be generated.

Now there is the problem if the user says he wants more than 7, the
pictureboxes get out of the form.

I want to implement like if you add more than 7 objects, a scrollbar should be visible, and you can scroll until the last object.

Thank you

Greets Niko

推荐答案

您可以使用System.Windows.Forms.ScrollableControl的功能.该类是System.Windows.Forms.ContainerControl的基类,而该类是System.Windows.Forms.Form的基类,因此可以使用表单来实现.

但是更好的是,创建一些从System.Windows.Forms.ContainerControl派生的控件类,并在其中使用自动滚动.将您提到的所有对象作为此控件的子级.并以您的形式插入此控件.

请参阅:
http://msdn.microsoft.com/en-us/library/system. windows.forms.scrollablecontrol.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system. windows.forms.containercontrol.aspx [ ^ ].

本文非常清楚地说明了如何使用自动滚动:
http://www.bobpowell.net/understanding_autoscroll.htm [
You can use the features of the System.Windows.Forms.ScrollableControl. The class is a base class for System.Windows.Forms.ContainerControl, and this one is the base class for System.Windows.Forms.Form, so you can do it with your form.

But better yet, create some control class derived from System.Windows.Forms.ContainerControl and use auto-scrolling in it; put all your objects you''ve mentioned as a children of this control. And insert this control in your form.

Please see:
http://msdn.microsoft.com/en-us/library/system.windows.forms.scrollablecontrol.aspx[^],
http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol.aspx[^].

This article explains how to use auto-scroll very clearly:
http://www.bobpowell.net/understanding_autoscroll.htm[^].

—SA


使用面板.然后将您的pictureBoxes添加到面板中.确保设置AutoScroll = true.

Use a panel. Then add your pictureBoxes to the panel. Make sure you set AutoScroll = true.

public class Form1 : Form
    { 
        private System.ComponentModel.IContainer components = null;
        private System.Windows.Forms.Panel panel1;
        private string _dir = @"C:\img";
        private string _pat = "*.bmp";
        public Form1()
        {
            InitializeComponent();
            LoadPictureBoxes();
        }
        private void LoadPictureBoxes()
        {
            int count = 0;
            foreach(string path in Directory.GetFiles(_dir,_pat))
            {            
                PictureBox pb = new PictureBox();
                pb.Image = new Bitmap(path);
                pb.Height = 100;
                pb.Width = panel1.Width - 5;
                pb.Left = 0;
                pb.Top = count * 100;
                panel1.Controls.Add(pb);
                count++;
            }
        }
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(505, 659);
            this.panel1.TabIndex = 0;
            this.panel1.VerticalScroll.Enabled = true;
            this.panel1.AutoScroll = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(505, 659);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
    }


这篇关于C#窗体高度工具滚动条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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