• Panel容器


    Panel容器的作用,可以呈放其他控件的容器

    有滚动条,当显示内容太多可以显示滚动条

    属性:Anchor,Dock,AutoScroll

    知识点1:

    Anchor和Dock都可以实现容器或控件的停靠

    区别在于,使用Dock时Panel与父容器之间没有边距,而使用Anchor可以设置Panel与父容器之间的边距。

    知识点2:

    AutoScroll,当控件内容大于可见区域时,是否自动显示滚动条。

    设置AutoScroll=true,效果如下:

    知识点2:

    实现如下功能,学习动态添加控件和遍历控件的方法。

     代码:

    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace ControlsTest
    {
        public partial class FormPanel : Form
        {
            public FormPanel()
            {
                InitializeComponent();
                this.Load += FormPanel_Load;
            }
    
            private void FormPanel_Load(object sender, EventArgs e)
            {
                panel4.Controls.Clear();
                for (int i = 0; i < 7; i++)
                {
                    CheckBox cb = new CheckBox();
                    cb.Text = "选项" + i.ToString();
                    cb.Size = new Size(60, 20);//见注意
                    cb.Location = new Point(60 * i + 5, 5);
                    panel4.Controls.Add(cb);
                }
    
                for (int i = 0; i < 7; i++)
                {
                    RadioButton rb = new RadioButton();
                    rb.Text = "单选" + i;
                    rb.Size = new Size(60, 20);
                    rb.Location = new Point(60 * i + 5, 30);
                    panel4.Controls.Add(rb);
                }
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                List<string> list = new List<string>();
                string str = "";
                foreach (var item in panel4.Controls)
                {
                    if (item is CheckBox)
                    {
                        CheckBox cb = item as CheckBox;
                        if (cb.Checked)
                        {
                            list.Add(cb.Text);
                        }
                    }
                    if (item is RadioButton)
                    {
                        RadioButton rb = item as RadioButton;
                        if (rb.Checked)
                        {
                            list.Add((string)rb.Text);
                        }
                    }
                }
                str = String.Join(";", list);
                MessageBox.Show(str);
            }
        }
    }

    注意,动态添加控件时需要设置控件的大小,防止控件遮挡影响显示。

  • 相关阅读:
    大学的最后时光---我的毕设日记(二)
    大学的最后时光---我的毕设日记(一)
    记公司同事的一次集体活动
    ssh框架基本原理
    plsql基本操作记录
    eclipse快捷键。
    面向对象的特性:封装继承多态
    设置label中的对齐方式
    pixmap和label设置图片自适应大小
    类的继承
  • 原文地址:https://www.cnblogs.com/hanzq/p/16808744.html
Copyright © 2020-2023  润新知