C#源代码—在windows窗体中添加一个名为lblShow的Lable控件

来源:互联网 发布:淘宝促销手段有哪些 编辑:程序博客网 时间:2024/06/06 00:33

C#编程

1.在windows窗体中添加一个名为lblShow的Lable控件然后在源代码视图中编辑如下代码using System;using System.Windows.Forms;namespace TestEnum{    public partial class TestEnum : Form   //声明一个窗体类    {       //Visual Studio .Net自动生成的构造函数,后文示例将全部省略        public TestEnum()         {            InitializeComponent();        }  enum MyEnum { a = 101, b, c, d = 201, e, f };  //声明枚举型        private void TestEnum_Load(object sender, EventArgs e)        {            MyEnum x = MyEnum.f;                 //使用枚举型定义枚举数x            MyEnum y = (MyEnum)202;            string result ="枚举数x的值为";            result += (int)x;                   //将x转换为整数            result += "\n枚举数y代表枚举元素" + y;            lblShow.Text = result;        }    }}程序运行结果为显示:枚举数x的值为203枚举数y代表枚举元素e2.在windows窗体中添加一个名为lblShow的Lable控件然后在源代码视图中编辑如下代码using System;using System.Windows.Forms;namespace TestStru{    public partial class TestStru : Form    {        struct Student                //声明结构型        {            //声明结构型的数据成员            public int no;            public string name;            public char sex;            public int score;//声明结构型的方法成员            public string Answer()            {                string result="该学生的信息如下:";                result += "\n学号:" + no;        //"\n"为换行符                result += "\n姓名:"+ name;                result += "\n性别:"+ sex;                result += "\n成绩:"+ score;                return result;                   //返回结果            }        }; private void TestStru_Load(object sender, EventArgs e)        {            Student s;                           //使用结构型            s.no = 101;            s.name = "黄海";            s.sex = '男';            s.score = 540;            lblShow.Text = s.Answer();    //调用s的Answer方法显示该生信息                lblShow.Text += "\n\n"+DateTime.Now; //显示当前时间        }    }}程序运行结果为显示:该学生的信息如下:学号:101姓名:黄海性别:男成绩:5402016-1-10 16:32:07


0 0
原创粉丝点击