当前位置:   article > 正文

【WinForm】WinForm中的TableLayoutPanel控件、PropertyGrid控件、PictureBox控件、ListView控件、DataGridView控件的使用_winform tablelayoutpanel

winform tablelayoutpanel


前言


一、TableLayoutPanel控件

可以起到一个随着界面大小变化的效果。

1、效果

在这里插入图片描述

2、界面设计

界面上没有拖动窗体控件,而是在代码中new系统控件。
在这里插入图片描述

3、代码

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 TableLayoutPanel控件
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TableLayoutPanel tlp = new TableLayoutPanel();          //生成TableLayoutPanel控件
            tlp.Dock = DockStyle.Fill;
            tlp.RowCount = 4;
            tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 25));
            tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 25));
            tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 25));
            tlp.RowStyles.Add(new RowStyle(SizeType.Percent, 25));
            tlp.ColumnCount = 4;
            tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));
            tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));
            tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));
            tlp.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 25));
            tlp.CellBorderStyle = TableLayoutPanelCellBorderStyle.OutsetDouble;

            this.Controls.Add(tlp);

            TextBox tb1 = new TextBox();
            tb1.Text = "TextBox1";
            tb1.Multiline = true;
            tb1.Dock = DockStyle.Fill;

            tlp.Controls.Add(tb1);
            tlp.SetCellPosition(tb1,new TableLayoutPanelCellPosition(0,0));
            tlp.SetColumnSpan(tb1,2);

            TextBox tb2 = new TextBox();
            tb2.Text = "TextBox2";
            tb2.Multiline = true;
            tb2.Dock = DockStyle.Fill;

            tlp.Controls.Add(tb2);
            tlp.SetCellPosition(tb2, new TableLayoutPanelCellPosition(2, 1));
            tlp.SetRowSpan(tb2, 2);
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

二、PropertyGrid控件

1、效果

类似于VS中控件的属性栏。
在这里插入图片描述

2、界面设计

在这里插入图片描述

3、代码

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 PropertyGrid
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.propertyGrid1.SelectedObject = new Emplyee();
        }
    }

    public class Emplyee
    {
        private string _name = "Name1";
        private string _sex = "1";

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Sex
        {
            get { return _sex; }
            set { _sex = value; }
        }

    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

三、PictureBox

1、效果

使用PictureBox加载了一张动图。
在这里插入图片描述

2、界面设计

在这里插入图片描述
在这里插入图片描述

3、代码

就看加了图的效果,没啥代码。

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 PictureBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

四、ListView

1、效果

在这里插入图片描述

2、界面设计

在这里插入图片描述
在这里插入图片描述

3、代码

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 ListView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            /// <summary>
            /// 代码生成
            /// </summary>
            //ListViewItem List1 = new ListViewItem("List1");                                     
            //List1.SubItems.AddRange(new string[] { "1", "11", "111" });
            //ListViewItem List2 = new ListViewItem("List2");
            //List2.SubItems.AddRange(new string[] { "2", "22", "222" });

            //listView1.Items.AddRange(new ListViewItem[] { List1, List2 });
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

五、DataGridView

1、效果

在这里插入图片描述

2、界面设计

在这里插入图片描述

3、代码

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 DataGridView控件增强性
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“北风贸易DataSet.自行车产品”中。您可以根据需要移动或删除它。
            this.自行车产品TableAdapter.Fill(this.北风贸易DataSet.自行车产品);
            // TODO: 这行代码将数据加载到表“北风贸易DataSet.自行车产品次分类”中。您可以根据需要移动或删除它。
            this.自行车产品次分类TableAdapter.Fill(this.北风贸易DataSet.自行车产品次分类);
            // TODO: 这行代码将数据加载到表“北风贸易DataSet.自行车产品分类”中。您可以根据需要移动或删除它。
            this.自行车产品分类TableAdapter.Fill(this.北风贸易DataSet.自行车产品分类);

        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

总结

对这些控件的再一次熟悉。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/419035
推荐阅读
相关标签
  

闽ICP备14008679号