首页 > 代码库 > winform公共控件

winform公共控件

1.button:按钮 backcolor:背景颜色 backgroupimage:背景图片 click单击事件 image:显示图像

2.checkbox:复选框 checked:选中

用按钮控制复选框:点一下按钮复选框选中或删除

private void button1_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            { checkBox1.Checked = false; }
            else
            { checkBox1.Checked = true; }
        }

3.checklistbox:复选框列表  向右小箭头,编辑项可以添加 或数据,items选项也可添加

根据数据库数据,生成checklistbox列表

nation.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1.App_Code
{
    public class nation
    {
        private string _nationcode;

        public string Nationcode
        {
            get { return _nationcode; }
            set { _nationcode = value; }
        }

        private string _nationname;

        public string Nationname
        {
            get { return _nationname; }
            set { _nationname = value; }
        }
    }
}

nationdata.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace WindowsFormsApplication1.App_Code
{
    public class nationdata
    {
        SqlConnection conn = null;
        SqlCommand cmd = null;
        public nationdata()
        {
            conn = new SqlConnection("server=.;database=data0928;user=sa;pwd=123");
            cmd = conn.CreateCommand();        
        }
        public List<nation> select()
        {
            List<nation> nlist = new List<nation>();
            cmd.CommandText = "select*from nation";
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    nation n = new nation();
                    n.Nationcode = dr[0].ToString();
                    n.Nationname = dr[1].ToString();
                    nlist.Add(n);
                }
            }
            conn.Close();
            return nlist;
        }
    }
}

form1.cs

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;
using WindowsFormsApplication1.App_Code;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //根据数据库生成列表
        private void button1_Click(object sender, EventArgs e)
        {
            List<nation> nlist = new List<nation>();
            nationdata nd = new nationdata();
            nlist = nd.select();
            foreach (nation n in nlist)
            {
                checkedListBox1.Items.Add(n.Nationname);
            }
        }
        //根据列表选中项进行显示
        private void button2_Click(object sender, EventArgs e)
        {
            string end = "";
            int count = 0;
            foreach (object o in checkedListBox1.CheckedItems)
            {
                if (count > 0)
                { end = end + ","; }
                end += o.ToString();
                count++;
            }
            MessageBox.Show(end);
        }
    }
}

技术分享

容器flowlayoutpanel

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;
using WindowsFormsApplication1.App_Code;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //根据数据库生成列表
        private void button1_Click(object sender, EventArgs e)
        {
            List<nation> nlist = new List<nation>();
            nationdata nd = new nationdata();
            nlist = nd.select();
            foreach (nation n in nlist)
            {
                CheckBox cb = new CheckBox();
                cb.Text = n.Nationname;
                flowLayoutPanel1.Controls.Add(cb);
            }
        }
        //点击按钮2将民族苗族选项选中
        private void button2_Click(object sender, EventArgs e)
        {
            foreach (Control c in flowLayoutPanel1.Controls)
            {
                CheckBox cb = c as CheckBox;
                if (cb.Text=="苗族")
                { cb.Checked = true; }
            }
        }
    }
}

4.combobox:下拉列表  触发事件:selectedindexchanged选项索引改变触发

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;
using WindowsFormsApplication1.App_Code;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //根据数据库生成列表
        private void button1_Click(object sender, EventArgs e)
        {
            List<nation> nlist = new List<nation>();
            nationdata nd = new nationdata();
            nlist = nd.select();           
            comboBox1.DataSource = nlist;
            comboBox1.DisplayMember = "nationname";
            //默认选择最后一个
            comboBox1.SelectedIndex = nlist.Count - 1;
        }
        //点击按钮2将选中的项显示出来
        private void button2_Click(object sender, EventArgs e)
        {
            nation n = comboBox1.SelectedItem as nation;
            MessageBox.Show(n.Nationname);
        }
        //事件 选中的索引改变触发事件
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            nation n = comboBox1.SelectedItem as nation;
            MessageBox.Show(n.Nationcode);
        }
    }
}

技术分享

5.datetimepicker:时间日期选择
点击按钮3将选择的日期文本显示出来:

 private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(dateTimePicker1.Text);
        }

点击按钮3将日期以datetime类型显示出来

private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(dateTimePicker1.Value.ToString());
        }

对其赋值

private void button3_Click(object sender, EventArgs e)
        {
            dateTimePicker1.Value = Convert.ToDateTime("2000-1-1");
        }

6.label:输入显示文本

7.linklabel:点击变颜色,设置连接的文本

8.listbox:列表框 columnwidth:列宽  行为selectionmode:可变为多选multisimple,multiextended

9.maskedtextbox:格式文本框 可设置掩码

10.monthcalendar:日历

点击按钮显示日期:

private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(monthCalendar1.SelectionStart.ToString("yyyy年MM月dd日"));
        }

选择时间段,开始结束

 private void button3_Click(object sender, EventArgs e)
        {
            MessageBox.Show(monthCalendar1.SelectionStart.ToString());
            MessageBox.Show(monthCalendar1.SelectionEnd.ToString());
        }

时间段长度设置:行为maxselectioncount设置长度
11.notifyicon:右下角托盘工具  visible:是否可见  icon:图标  text:鼠标移上显示的文字

12.numericupdown:数字计数  数据increment:每次增加或减少的长度  maximum:最大值  minimum:最小值

取值:默认类型为decimal

private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(numericUpDown1.Value.ToString());
        }

13.picturebox:图片框  可导入图片,可设置图片布置样式(平铺,拉伸...)  可设置点击等事件  可设置鼠标事件(事件-鼠标)

14.progressbar:进度条      value:设置进度条的值

取计数器的值,点击按钮,显示长度

private void button1_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(numericUpDown1.Value);
            progressBar1.Value = a;
        }

行为style:marquee跑马灯  快慢设置marqueeanimationspeed更改数值调节速度

 private void button1_Click(object sender, EventArgs e)
        {
            int a = Convert.ToInt32(numericUpDown1.Value);
            progressBar1.MarqueeAnimationSpeed = a;
        }

15.radiobutton:单选按钮组  checked设置默认值

16.textbox:文本框  行为multiline:true多行false单行,自动换行  设置滚动条:外观scrollbars:horizontal水平滚动条,vertical竖直滚动条,both都有

要显示横向滚动条:行为wordwrap更改为false

设置最大长度:行为maxlength

设置密码型:行为passwordchar设置为一个*

使用系统默认的密码型:行为usesystempasswordchar设为true

行为readonly:不可以更改内容,但可以复制

行为enabled:不可用,鼠标焦点无法移入,不可选中复制

17.richtextbox:多行文本框,最大文本数比textbox多,拿来做多行内容用

18.tooltip:杂项中,注释其他工具

杂项initialdelay:鼠标停留多长时间显示,默认为半秒

19.treeview:树状列表 向右小箭头,编辑节点,添加根,添加子集

触发事件:afterselect更改选定内容后发生

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            MessageBox.Show(treeView1.SelectedNode.Text);
        }

20.webbrowser
设置页面:显示页面为百度

namespace WindowsFormsApplication2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
            Uri u = new Uri("http://www.baidu.com");
            webBrowser1.Url = u;
        }
    }
}

技术分享

 在文本框输入,点击按钮跳转网页

private void button1_Click(object sender, EventArgs e)
        {
            Uri u = new Uri(textBox1.Text);
            webBrowser1.Url = u;
        }

 

winform公共控件