zoukankan      html  css  js  c++  java
  • winform基础控件-例子学习

    1、如图实现整数计算器

    ComboBox控件:

    Items属性:添加集合中的项。

    this.comoper.Items.AddRange(new object[] {
                "+",
                "-",
                "x",
                "/"});

    TextBox:

    对TextBox的输入文本有限制:

    1)只能输入数值型(整数和小数);

    2)小数点不能开头,小数只能输入一位;

    3)不满足要求的输入统一不接受。

    实现方法

    使用TextBox的KeyPress事件:在控件有焦点的情况下按下键时发生。

    KeyChar属性获取或设置与按下的键对应的字符。

    KeyPress 事件不能由非字符键引发;但是非字符键能够引发 KeyDownKeyUp 事件。

    使用 KeyChar 属性采样运行时的键击,以及使用或修改常用键击的一个子集。

    若要仅在窗体级别处理键盘事件而不允许其他控件接收键盘事件,请将窗体的 KeyPress 事件处理方法中的 KeyPressEventArgs. Handled 属性设置为true。

    //处理键盘按键盘事件,当前时间焦点在TextBox控件级别
            private void textnum1_KeyPress(object sender, KeyPressEventArgs e)
            {
                //通过sender得到触发该事件的控件
                TextBox currtextbox = sender as TextBox;
    
                if (e.KeyChar<'0' || e.KeyChar>'9')
                {
                   //用户输入的不是数字
                    e.Handled = true;
                }
    //用户是否输入了退格键
    if (e.KeyChar==8) { e.Handled = false; } //让用户输入小数点,判断是不是小数点 if(e.KeyChar==46) { //只让用户输入一个小数点,检测当前文本框是否有小数点
    //报告指定 Unicode 字符或字符串在此实例中的第一个匹配项的从零开始的索引。 如果未在此实例中找到该字符或字符串,则此方法返回 -1。
    if (currtextbox.Text.IndexOf(".") == -1) {
      //小数点不能在第一位,注意字符是单个单个输入的
                        //SelectionStart获取或设置文本框中当前输入的文本起始点。
    if(currtextbox.SelectionStart>0) { e.Handled = false; } } } }

    using System;
    using System.Windows.Forms;
    
    public class Form1: Form
    {
        public Form1()
        {
            // Create a TextBox control.
            TextBox tb = new TextBox();
            this.Controls.Add(tb);
            tb.KeyPress += new KeyPressEventHandler(keypressed);
        }
    
        private void keypressed(Object o, KeyPressEventArgs e)
        {
            // The keypressed method uses the KeyChar property to check 
            // whether the ENTER key is pressed. 
    
            // If the ENTER key is pressed, the Handled property is set to true, 
            // to indicate the event is handled.
            if (e.KeyChar == (char)Keys.Return)
            {
                e.Handled = true;
            }
        }
    
        public static void Main()
        {
            Application.Run(new Form1());
        }
    }

    2、图片查看器

    身份证号码的校正:

          private bool CheckCardId(string id)
            {
                //校验位的权值或编码值
                int[] wQuan = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
                string checkWei = "10X98765432";
                string number17 = id.Substring(0, 17);
                string number18 = id.Substring(17);
                int sum=0;
                for(int i=0;i<17;i++)
                {
                    sum+=Convert.ToInt32(number17[i].ToString())* wQuan[i];
                    //char类型不能直接转换成整形(实际转换成对应的ascii),先tostring在转换
    
                }
                int mod = sum % 11;
                string result = checkWei[mod].ToString();
                if(number18.Equals(result,StringComparison.OrdinalIgnoreCase))
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
    
            private void btnView_Click(object sender, EventArgs e)
            {
                //身份证可能是15位或者18位
                //15位身份证组号:省(20)市(2)县(2)年(2)月(2)日(2)+3序列号(奇男偶女)
                //18位身份证:第一:出生年前加上19 第二点:第18位校验位,从前17位计算而来
                string id = pidid.Text;
                int age = 0;
                int year = 0;
                if (id.Length == 15)
                {
                    year = Convert.ToInt32(id.Substring(6, 2)) + 1900;
                }
                else if (id.Length == 18)
                {
                    if(!this.CheckCardId(id))
                    {
                        MessageBox.Show("身份证输入有误,请检查");
                            return;
                    }
                    year = Convert.ToInt32(id.Substring(6, 4));
                }
                else
                {
                    MessageBox.Show("身份证长度输入有误,请重新输入");
                    return;
                }
                age = DateTime.Now.Year - year;
                if (age >= 18)
                {
                    pic.Visible = true;
                }
                else
                {
                    MessageBox.Show("你太小了,回家看动画片吧");
                }
            }

    3、访问网址

    WebBrowser类:使用户可以在窗体内导航网页。

    private void button1_Click(object sender, EventArgs e)
            {
                webBrowser1.Navigate(button1.Text);
            }

    4.树形控件增删该查

    TreeView:

    与上图对应的方法:

       private void button1_Click(object sender, EventArgs e)
            {
                treeView1.HideSelection = false;
                MessageBox.Show(treeView1.SelectedNode.Text);
                
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                //MessageBox.Show("根节点节点的数量{0}", treeView1.Nodes.Count.ToString());
                //treeView1.Nodes.Add("add");根节点添加
                //treeView1.Nodes[0].Nodes[0].Nodes.Add("王晓东");//添加到一级节点下
                if(treeView1.SelectedNode!=null)
                {
                    treeView1.SelectedNode.Nodes.Add(textBox1.Text);
                }
                if(treeView1.SelectedNode==null)
                {
                    treeView1.Nodes.Add(textBox1.Text);
                }
    
            }
    
            private void TreeView_Load(object sender, EventArgs e)
            {
                treeView1.ExpandAll();//展开节点
                treeView1.Nodes[0].ImageIndex = 3;
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                if(treeView1.SelectedNode!=null)
                {
                    //treeView1.SelectedNode.ExpandAll();//展开当前节点下的所有节点
                    treeView1.SelectedNode.Expand();//展开当前节点下的所子节点
                }
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                if (treeView1.SelectedNode != null)
                {
                    treeView1.SelectedNode.Collapse();//关闭当前节点下的所有节点
                }
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                treeView1.SelectedNode.Remove();
            }
    
            private void button6_Click(object sender, EventArgs e)
            {
                string str="选中的语言";
                foreach(TreeNode tn in treeView1.Nodes[0].Nodes[0].Nodes)
                {
                    if(tn.Checked==true)
                    {
                        str = str + tn.Text;
                    }
                }
                MessageBox.Show(str);
            }
    
            private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
            {
                foreach (TreeNode tn in e.Node.Nodes)
                {
                    tn.Checked = e.Node.Checked;
                }
            }
    View Code
  • 相关阅读:
    不能在注册表中识别出来python时的解决方法
    Biopython
    Biopython
    生信相关网站
    linux下的查找命令
    class
    不同版本的Eclipse安装SVN插件
    【如何快速的开发一个完整的iOS直播app】(原理篇)
    iOS 直播
    Ios-视频直播-知识点汇总-持续更新
  • 原文地址:https://www.cnblogs.com/janghe/p/7460751.html
Copyright © 2011-2022 走看看