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 Example22
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            byte bt_One, bt_Two; //定义两个 byte 变量
            if (byte.TryParse(textBox1.Text, out bt_One) && byte.TryParse(textBox2.Text, out bt_Two)) //为两个 byte变量赋值
            {
                try
                {
                    checked { bt_One += bt_Two; } //使用 checked 关键字判断是否溢出
                    textBox3.Text = bt_One.ToString();  //输出 相加后的结果
                }
                catch (OverflowException ex)
                {
                    MessageBox.Show(ex.Message, "出错!");
                }
            }
            else
            {
                MessageBox.Show("请输入 255 以内的数字!");
            }
        }
        private void Form1_Shown(object sender, EventArgs e)
        {
            this.Text = "使用 checked 关键字处理 溢出 错误信息";
        }
    }
}
posted on 2011-10-17 01:25  C#_初学者  阅读(297)  评论(0编辑  收藏  举报