• 公共控件


    控件的赋值,取值,改值,事件

    (1)Button 按钮   AutoSize属性,选择True根据输入的Text的长度自动改变长度

           Enable属性:选取该控件是否可用,true表示可用,False表示不可用

             Visiable属性:该控件是否可见,true表示可见,False表示隐藏

                      事件:默认Click事件

    (2)CheckBox 复选框   Checked属性,选择true表示以选中,false表示未选中

          点击Button1 按钮,实现CheckBox1的选中和取消功能,代码如下:

    private void button1_Click(object sender, EventArgs e)
            {
                if (checkBox1.Checked)//若checkBox1为选中,改为未选中(true未选中,false为未选中)
                    checkBox1.Checked = false;
                else//若checkBox1为未选中,改为选中
                    checkBox1.Checked = true;
    
            }

      将启动时设置CheckBox1为默认选中,在构造函数中编写代码,代码如下:

    public Form1()
            {
                InitializeComponent();
                checkBox1.Checked = true;//默认选中
            }

    (3)CheckListBox  一组CheckBox

      

    赋值:点击Button1将数据库中的民族选项导入:

    封装实体类:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace WindowsFormsApplication1.App_Code
    {
        public class Nation
        {
            /// <summary>
            /// 民族编号
            /// </summary>
            public string nationcode { get; set; }
            /*private string _nationcode;
    
            public string Nationcode
            {
                get { return _nationcode; }
                set { _nationcode = value; }
            }*/
            /// <summary>
            /// 民族名称
            /// </summary>
            public string nationname { get; set; }
            /*
            private string _nationname;
    
            public string Nationname
            {
                get { return _nationname; }
                set { _nationname = value; }
            }
             */
        }
    }
    View Code

    数据访问类:

    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 comm = null;
            public NationData()
            {
                conn = new SqlConnection("server=.;database=Data1128;user=sa;pwd=123");
                comm = conn.CreateCommand();
            }
            public List<Nation> Select()
            {
                List<Nation> nlist = new List<Nation>();
                comm.CommandText=("select *from Nation");
                conn.Open();
                SqlDataReader dr=comm.ExecuteReader();
                if(dr.HasRows)
                {
                    while (dr.Read())
                    {
                        Nation na = new Nation();
                        na.nationcode = dr[0].ToString();
                        na.nationname = dr[1].ToString();
                        nlist.Add(na);
                    }
                }
                conn.Close();
                return nlist;
            }
        }
    }
    View Code

    业务逻辑:

    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 NationData().Select();
                foreach(Nation nn in nlist)
                {
                    checkedListBox1.Items.Add(nn.nationname);
                }
            }
        }
    }

    取值:点击Button2,将选中项的值取出来

     private void button2_Click(object sender, EventArgs e)
            {
                string end = "";//中间变量
                int count = 0;
                //遍历选中的集合
                foreach (object ob in checkedListBox1.CheckedItems)
                {
                    if (count > 0)
                        end += ",";
    
                    end += ob.ToString();
                    count++;
                }
                MessageBox.Show(end);
            }

     扩展:容器控件中的FlowLayoutPanel   流式布局对空间进行集合分组,相当于一个小div

    赋值:点击Button1将数据库中的民族选项导入:

    private void button1_Click(object sender, EventArgs e)
            {
                List<Nation> nlist = new NationData().Select();
                foreach (Nation nn in nlist)
                {
                    CheckBox cb=new CheckBox();//实例化,将checkbox放到这个容器中
                    cb.Text = nn.nationname;
                    flowLayoutPanel1.Controls.Add(cb);
                }
            }

    取值:点击Button2,将选中项的值取出来

    private void button2_Click(object sender, EventArgs e)
            {
                foreach(Control ct in flowLayoutPanel1.Controls)
                {
                    CheckBox cb = ct as CheckBox;
                    if (cb.Checked)
                    {
                        MessageBox.Show(cb.Text);
                    }
                }
         }

    点击Button3,自动选择藏族

     private void button3_Click(object sender, EventArgs e)
            {
                foreach (Control ct in flowLayoutPanel1.Controls)
                {
                    CheckBox cb = ct as CheckBox;
                    if(cb.Text=="藏族")
                    {
                        cb.Checked=true;
                    }
                }
            }        

    (4)ComoBox  下拉选框

      赋值:点击Button1将数据库中的民族选项导入:

       实体类,数据访问类同上,

      业务逻辑:

    private void button1_Click(object sender, EventArgs e)
            {
                List<Nation> nlist = new NationData().Select();
                comboBox1.DataSource = nlist;//获取数据源
                comboBox1.DisplayMember = "nationname";//设置要显示的属性
           comboBox1.SelectedIndex = nlist.Count - 1;//设置默认选择为最后一项
    }

      取值:点击Button2,将选中项的值取出来

    private void button2_Click(object sender, EventArgs e)
            {
                Nation na = new Nation();
                na = comboBox1.SelectedItem as Nation;//将选中项转为Nation对象
    
                MessageBox.Show(na.nationname);//获取编号:MessageBox.Show(na.nationcode);
            }

     事件:SelectedIndexChanged   选择的索引发生改变时,执行括号内的语句(弹出改变的信息)

     private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                Nation na = new Nation();
                na = comboBox1.SelectedItem as Nation;//将选中项转为Nation对象
    
                MessageBox.Show(na.nationname);//获取编号:MessageBox.Show(na.nationcode);
            }

    (5)DatetimePicker   时间日期的选择

    取值:

    点击Button3,弹出字符串样式的时间:

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

     点击Button3,弹出DataTime样式的时间:

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

     改值:

    点击Button3,将时间改为2000-1-1

    private void button3_Click(object sender, EventArgs e)
            {
                dateTimePicker1.Value = Convert.ToDateTime("2000-1-1");//value既能取值也能赋值
            }

    (6)Lable

    (7)LinkLable  超链接样式,和Lable都有Click和DoubleClick点击事件

    (8)ListBox  列表框,相当于把下拉列表展开,具有多选功能

          SelectionMode  选择列表框时单选:One、多选 :MultiSimple和MultiExtented、不选:None

    (9)MaxedTextBox  格式文本框,设置掩码,选择哪种掩码,输入时只能输入该掩码的数据格式

      

    (10)MonthCalender  日历  属性:MaxDate :设置 最大选择日期;    MaxSelectCount:设置最大选取天数

     获取开始日期和结束日期:

    private void button3_Click(object sender, EventArgs e)
            {       
           MessageBox.Show(monthCalendar1.SelectionStart.ToString());
    //ToString()可设置日期显示样式 MessageBox.Show(monthCalendar1.SelectionEnd.ToString()); }

     (11)NotifyIcon  运行时在任务栏的右侧通知区域显示图标,窗口不显示任何东西

      Visible  选择true表示可见,False表示隐藏

      Icon   选择要显示的图标

      Text   鼠标悬停在图标上时要现实的文本

    (12)NumericUpDown  

      Increment   没单击一下按钮增加或减少的值

      Maximum    可输入的最大值

      Minimum    可输入的最小值

      Value    空间的当前值,取值时使用,如下:

    点击Button1,获取NumericUpDown1的值

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

    (13)PictureBox   图片,有Click事件,使用时和Button没有区别

    (14)ProgressBar   操作进度条  ,Value  输入值,进度按百分比,如取50时,

      点击Button1,把NumericUpDown1的值在ProgressBar1上显示进度:

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

      Style  用户设置进度条的样式,样式一:Blocks 和样式二:Continuous 看上去完全一样为正常进度条样式

         样式三:Marquee   跑马灯  与MarqueeAnimationSpeed   (字母动画的速度以毫秒为单位)配合使用

    点击Button1,把NumericUpDown1的值赋在ProgressBar1的MarqueeAnimationSpeed上

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

    (15)RadioButton  单选按钮,属性和CheckedBox复选框基本一致,上查第(2)条;

    (16)TextBox   文本框,用户可输入任何内容,不限长度。

         属性:MultiLine 选择true时可以换行,选择false只会是一行

          WordWrap  选择true自动换行,选择False不会自动换行

          ScrollBars   进度条选择,None:无进度条;Horizaontal:横向进度条,只有在WordWrap选择false时不自动换行才会出现横向进度条;

                 Vertical :  纵向进度条;Both:双向进度条

          MaxLength 设置可输入的最大字符数(长度);

          PasswordChar 设置一个字符,显示时代替你输入的字符显示在文本框中;

          UseSystemPasswordChar  选择true时表示使用系统自带的PasswordChar;

          ReadOnly  选择true时表示只读,用户不能输入,可以复制里面的内容;

          Enabled   是否使用该控件,选择true表示使用,false表示禁用,用户既不能输入,也不能复制

    (17)RichTextBox   默认多行文本框,常用于设置输入多行文本

             和TextBox的区别:

          1.RichTextBox输入的最大字符数(MaxLength)多于TextBox

          2.RichTextBox执行起来更加细致,如下

    public Form2()
            {
                InitializeComponent();
                string aaa=("第一行
    第二行
    第三行");
                textBox1.Text = aaa;
                richTextBox1.Text = aaa;
            }
    RichTextBox      
    TextBox

    (18)ToolTip 给每个控件增加一个ToolTip属性,输入文字后,鼠标移过相关联控件时显示输入的文字信息

    (19)TreeView  树状列表,唯一一个用递归加载的控件。

      

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

    如:更改选定内容后弹出更改后的内容

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

    (20)WebBrowser 允许用户在窗体内浏览网页

    窗体网页设置为百度:

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

    设置一个TextBox,一个Button,在TextBox中输入网址,点击Button,WebBrowser跳转到该网页

    private void button1_Click(object sender, EventArgs e)
            {
                Uri u = new Uri(textBox1.Text);
                webBrowser1.Url = u;
            }
  • 相关阅读:
    摄像头标定GML Camera Calibration
    joda.money.Money
    aop the less note 1
    good notes
    yingyu biji 1
    flowable note1
    activiti note1
    CET4/6
    Web应用界面好帮手!DevExtreme React和Vue组件全新功能上线
    Web UI开发推荐!Kendo UI for jQuery自定义小部件——处理事件
  • 原文地址:https://www.cnblogs.com/maxin991025-/p/6133421.html
Copyright © 2020-2023  润新知