张亮的博客园

联系方式:131280660812 微信号:131280660812 邮箱:1796969389@qq.com qq号:1796969389
黑马程序员+Winform基础(上)

黑马程序员+Winform基础

 

  1. 控件:Button 按钮,TextBox 文本框, Label 标签,RadioButton 单选按钮,CheckButton 复选框 ,ListBox 列表框  ,PictureBox 图片框,ComboBox 下拉列表框
  2. 属性:Text:文本框中输入的值 ,Name:通过name来操作对象,Form.Text:窗体的标题,ForeColor:前景色,TextAlign:文本对齐,Image 图片,PasswordChar:密码文本 ,MultiLine 多行文本,Items 数据项,DropDownStyle:控制组合框的外观和功能,SelectItem 选中的项,SelectIndex 选中项的索引
  3. string.Format(“{0},你好”,textBox1.Text); //设置输出格式
  4. 方法:Form.Hide():对象隐藏,TextBox. AppendText()附加文本,
  5. 事件:Click:点击, SelectedIndexChanged 选择项发生改变

6.练习:求和
/// <summary>

        /// 求和

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnAdd_Click(object sender, EventArgs e)

        {        

   string num1 = txtnum1.Text;

            string num2 = txtnum2.Text;

            int i1,i2;

            if (!int.TryParse(num1,out i1))//转换成int

            {

                MessageBox.Show("第一个数不合法!");

                txtnum1.Text = "";//数字不合法,清空文本框

            }

            else if (int.TryParse(num2, out i2) == false)

            {

                MessageBox.Show("第二个数不合法!");

                txtnum2.Text = "";

            }

            else

            {//求和输出

                txtsum.Text = (i1 + i2).ToString();

            }

        }

7. 练习:核对Email,分析出用户名和域名
/// <summary>

        ///       核对Email,分析出用户名和域名

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button1_Click(object sender, EventArgs e)

        {

            string email = txtEmail.Text;

            string[] str = email.Split('@');

            if (str.Length!=2)

            {

                MessageBox.Show("Email不合法");

                txtEmail.Text = "";

            }

            else

            {

                MessageBox.Show(string.Format("用户名:{0} , 域名:{1}",str[0],str[1]));

            }

        }

8. 练习:两个数的累加
          /// <summary>

        /// 累加

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button2_Click(object sender, EventArgs e)

        {

            string num1 = textBox1.Text;

            string num2 = textBox2.Text;

            int i1, i2,sum=0;

            if (!int.TryParse(num1,out i1))

            {

                MessageBox.Show("第一个数非法");

                textBox1.Text = "";

            }

            else if (!int.TryParse(num2,out i2))

            {

                MessageBox.Show("第二个数非法");

                textBox1.Text = "";

            }

            else

            { //判断

                if (i1>=i2)

                {

                    MessageBox.Show("第一个数必须小于第二个数");

                    textBox1.Text = "";

                    textBox2.Text = "";

                }

                else

                {//累加

                    for (int i = i1; i <= i2; i++)

                    {

                        sum += i;

                    }

                    MessageBox.Show(string.Format("{0}累加到{1}和为{2}", i1, i2, sum));

                }               

            }         

           

        }

 

9 .练习:

          /// <summary>

        /// 查看图片(满18岁可查看)

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button5_Click(object sender, EventArgs e)

        {

            string id = textBox4.Text;

            //验证输入的身份证是否合法

            if (id.Length!=18)

            {

                MessageBox.Show("身份证输入不合法");

                textBox4.Text = "";

            }

            else 

            {//判断是否成年

                if (DateTime.Now.Year-Convert.ToInt32(id.Substring(6,4))>18)

                {

                    pictureBox1.Visible = true;

                }

                else

                {

pictureBox1.Visible = false;

                    MessageBox.Show("年龄太小!");

                }

            }

        }

 

 

10.练习:
        /// <summary>

        /// 向左移动

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button3_Click(object sender, EventArgs e)

        {

            string str = textBox3.Text;

            this.textBox3.Text = str.Substring(1)+str.Substring(0,1);

        }

        /// <summary>

        /// 向右移动

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button4_Click(object sender, EventArgs e)

        {

            string str = textBox3.Text;

            this.textBox3.Text = str.Substring(str.Length-1)+str.Substring(0, str.Length - 1);

        }

11.+=与appendText的区别
        /// <summary>

        /// 附加文本

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void button6_Click(object sender, EventArgs e)

        {

            //textBox5.Text += DateTime.Now+"\r\n";//用于数据量小

            textBox5.AppendText(DateTime.Now.ToString()+"\n");//数据量大

        }

12.练习:登录错三次不让再登录
              /// <summary>

            /// 登录,失败超过3次后退出

            /// </summary>

            /// <param name="sender"></param>

            /// <param name="e"></param>

      private  int errorTime = 0;

        private void btnsubmit_Click(object sender, EventArgs e)

        {

            string userName = txtname.Text.Trim();

            string pwd = txtpwd.Text.Trim();

          

            if (userName.Equals("admin", StringComparison.OrdinalIgnoreCase)&&pwd.Equals("888888"))

            {

                MessageBox.Show("登录成功!");

            }

            else

            {

                errorTime++;//不能用局部变量,用类字段才可达到计数的功能

                if (errorTime>=3)

                {

                    MessageBox.Show("错误三次,不允许再登录!");

                    //this.Close();

                    Application.Exit();//退出程序

                }

                else

                {

                    MessageBox.Show("登录失败!");

                }

            }

        }

posted on 2013-11-03 19:35  张亮13128600812  阅读(123)  评论(0编辑  收藏  举报