C#开发之——MessageBox(12.5)

一 概述

消息框在Windows操作系统经常用到,例如在将某个文件或文件夹移动到回收站中时,系统会自动弹出如下图所示的消息框

<!--more-->

二 MessageBox介绍

  • 在Windows窗体应用程序中向用户提示操作时也是采用消息框弹出的形式

  • 消息框是通过MessageBox类实现的,在MessageBox类中仅定义了Show的多个重载方法,该方法的作用是弹出一个消息框

  • 由于Show方法时一个静态的方法,因此调用该方法只需要使用MessageBox.Show(参数)的形式即可弹出消息框

  • 消息框在显示时有不同的样式,例如标题、图标、按钮等

三 MessageBox常用方法

3.1 常用方法

方法说明
DialogResult Show(string text)指定消息框中显示的文本(Text)
DialogResult Show(string text,string caption)指定消息框中显示的文本(text)以及消息框的标题(caption)
DialogResult Show(string text,string caption,MessageBoxBUttons buttons)指定消息框中显示的文本(text)、消息框的标题(caption)以及消息框中显示的按钮(buttons)
DialogResult Show(string text,string caption,MessageBoxButtons buttons,MessageBoxIcon icon)指定消息框中显示的文本(text)、消息框的标题(caption)、消息框中显示的按钮(buttons)以及消息框中显示的图标(icon)

3.2 两个枚举类型

在上面所列出的方法的参数中还涉及两个枚举类型,一个是MessageBoxButtons,一个是MessageBoxIcon。下面介绍这两个枚举类型的具体值:

MessageBoxButtons枚举类型主要用于设置消息框中显示的按钮,具体的枚举值如下:

  • OK:在消息框中显示“确定”按钮

  • OKCancel:在消息框中显示“确定”和“取消”按钮

  • AbortRetryIgnore:在消息框中显示"终止"、“重试”和“忽略”按钮

  • YesNoCancel:在消息框中显示"是"、“否”和“取消”按钮

  • YesNo:在消息框中显示“是”和“否”按钮

  • RetryCancel:在消息框中显示“重试”和“取消”按钮

MessageBoxIcon 枚举类型主要用于设置消息框中显示的图标,具体的枚举值如下。

  • None:在消息框中不显示任何图标。

  • Hand、Stop、Error:在消息框中显示由一个红色背景的圆圈及其中的白色X组成 的图标

  • Question:在消息框中显示由圆圈和其中的一个问号组成的图标。

  • Exclamation、Warning:在消息框中显示由一个黄色背景的三角形及其中的一个感叹号组成的图标

  • Asterisk、Information:在消息框中显示由一个圆圈及其中的小写字母 i 组成的图标

调用 MessageBox 类中的 Show 方法将返回一个 DialogResult 类型的值。

DialogResult 也是一个枚举类型,是消息框的返回值,通过单击消息框中不同的按钮得到不同的消息框返回值

DialogResult 枚举类型的具体值如下

  • None:消息框没有返回值,表明有消息框继续运行

  • OK:消息框的返回值是 0K (通常从标签为“确定”的按钮发送)

  • Cancel:消息框的返回值是 Cancel (通常从标签为“取消”的按钮发送)

  • Abort:消息框的返回值是 Abort (通常从标签为“中止”的按钮发送)

  • Retry:消息框的返回值是 Retry (通常从标签为“重试”的按钮发送)

  • Ignore:消息框的返回值是 Ignore (通常从标签为“忽略“的按钮发送)

  • Yes:消息框的返回值是 Yes (通常从标签为“是“的按钮发送)

  • No:消息框的返回值是 No (通常从标签为“否“的按钮发送)

四 实例  创建一个窗体,单击该窗体弹出一个消息框提示“是否打开新窗口”,如果单击“是”按钮,则打开新窗口,如果单击“否”按钮,则关闭当前窗体 

4.1 创建所需的窗体

创建一个名为 Windows_3 的项目,并在该项目中添加两个窗体,分别命名为 MainForm、 MessageForm。

 

4.2 在 MainForm 窗体中添加事件

在 MainForm 窗体中添加鼠标单击事件,并在相应的事件中添加如下代码

<span style="color:#333333">public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
    }
    private void MainForm_MouseClick(object sender, MouseEventArgs e)
    {
        //弹出消息框,并获取消息框的返回值
        DialogResult dr = MessageBox.Show("是否打开新窗体?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        //如果消息框返回值是Yes,显示新窗体
        if (dr == DialogResult.Yes)
        {
            MessageForm messageForm = new MessageForm();
            messageForm.Show();
        }
        //如果消息框返回值是No,关闭当前窗体
        else if (dr == DialogResult.No)
        {
            //关闭当前窗体
            this.Close();
        }
    }
}</span>

4.3 设置项目的启动窗体

在 Program.cs 文件中将 MainForm 设置为启动窗体,代码如下

<span style="color:#333333">static class Program
{
    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }
}</span>

 

 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
学生信息管理系统是一个常见的应用系统,使用C#语言采用三层架构进行开发,可以提高系统的可维护性、可扩展性、可重用性和可测试性等方面的优势。 三层架构是一个比较经典的软件架构,它将应用程序划分为三个主要的部分:表示层、业务逻辑层和数据访问层。每一层都有不同的职责,但它们又协同工作,为应用程序提供了良好的结构和可维护性。 下面我来介绍一下如何使用C#语言来开发一个学生信息管理系统。 一、需求分析 首先,我们需要对学生信息管理系统进行需求分析,明确系统的功能和需求,例如: 1. 学生信息的录入、修改、删除和查询功能。 2. 学生信息的显示和打印功能。 3. 学生信息的统计和分析功能。 4. 具有管理员和普通用户两种角色,管理员可以对学生信息进行管理,而普通用户只能浏览学生信息。 二、设计数据库 接下来,我们需要设计数据库,为系统提供数据存储和管理。在这里,我们可以使用SQL Server数据库管理系统来创建一个名为“Student”的数据库,其中包含一个名为“StudentInfo”的表,用于存储学生信息。 表的结构如下: |字段名称|字段类型|字段长度|是否允许空值| |:----:|:----:|:----:|:----:| |StudentID|nvarchar|10|否| |Name|nvarchar|20|否| |Sex|nvarchar|4|否| |Age|int|4|否| |Class|nvarchar|20|否| |Address|nvarchar|50|是| |Phone|nvarchar|20|是| 在这个表中,我们设置了一个主键StudentID,用于唯一标识每一个学生。 三、设计架构 在这里,我们采用三层架构来设计学生信息管理系统。 1. 表示层:表示层是系统与用户之间的接口,主要负责用户交互和数据展示的工作。在这里,我们使用Windows Forms技术来实现用户界面。 2. 业务逻辑层:业务逻辑层是系统的核心部分,它负责处理系统的业务逻辑,将用户的请求转化为对数据访问层的请求,并将处理结果返回给用户。在这里,我们将业务逻辑层封装为一个名为“StudentBLL”的类。 3. 数据访问层:数据访问层是系统与数据库之间的接口,主要负责对数据库进行访问和操作。在这里,我们将数据访问层封装为一个名为“StudentDAL”的类。 四、编写代码 1. 表示层代码 在表示层中,我们需要实现用户界面和事件响应等功能。在这里,我们使用Windows Forms技术来实现用户界面,例如: ```csharp // MainForm.cs public partial class MainForm : Form { private StudentBLL studentBLL = new StudentBLL(); public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { RefreshData(); } private void RefreshData() { dgvStudent.DataSource = studentBLL.GetAllStudents(); } private void btnAdd_Click(object sender, EventArgs e) { AddStudentForm addStudentForm = new AddStudentForm(); if (addStudentForm.ShowDialog() == DialogResult.OK) { Student student = new Student(); student.StudentID = addStudentForm.txtStudentID.Text; student.Name = addStudentForm.txtName.Text; student.Sex = addStudentForm.cboSex.Text; student.Age = Convert.ToInt32(addStudentForm.txtAge.Text); student.Class = addStudentForm.txtClass.Text; student.Address = addStudentForm.txtAddress.Text; student.Phone = addStudentForm.txtPhone.Text; if (studentBLL.AddStudent(student)) { MessageBox.Show("添加成功!"); RefreshData(); } else { MessageBox.Show("添加失败,请检查输入信息!"); } } } private void btnEdit_Click(object sender, EventArgs e) { if (dgvStudent.CurrentRow != null) { EditStudentForm editStudentForm = new EditStudentForm(); editStudentForm.txtStudentID.Text = dgvStudent.CurrentRow.Cells["StudentID"].Value.ToString(); editStudentForm.txtName.Text = dgvStudent.CurrentRow.Cells["Name"].Value.ToString(); editStudentForm.cboSex.Text = dgvStudent.CurrentRow.Cells["Sex"].Value.ToString(); editStudentForm.txtAge.Text = dgvStudent.CurrentRow.Cells["Age"].Value.ToString(); editStudentForm.txtClass.Text = dgvStudent.CurrentRow.Cells["Class"].Value.ToString(); editStudentForm.txtAddress.Text = dgvStudent.CurrentRow.Cells["Address"].Value.ToString(); editStudentForm.txtPhone.Text = dgvStudent.CurrentRow.Cells["Phone"].Value.ToString(); if (editStudentForm.ShowDialog() == DialogResult.OK) { Student student = new Student(); student.StudentID = editStudentForm.txtStudentID.Text; student.Name = editStudentForm.txtName.Text; student.Sex = editStudentForm.cboSex.Text; student.Age = Convert.ToInt32(editStudentForm.txtAge.Text); student.Class = editStudentForm.txtClass.Text; student.Address = editStudentForm.txtAddress.Text; student.Phone = editStudentForm.txtPhone.Text; if (studentBLL.UpdateStudent(student)) { MessageBox.Show("修改成功!"); RefreshData(); } else { MessageBox.Show("修改失败,请检查输入信息!"); } } } else { MessageBox.Show("请先选择一条记录!"); } } private void btnDelete_Click(object sender, EventArgs e) { if (dgvStudent.CurrentRow != null) { string studentID = dgvStudent.CurrentRow.Cells["StudentID"].Value.ToString(); if (MessageBox.Show("确定要删除学号为" + studentID + "的学生信息吗?", "删除确认", MessageBoxButtons.YesNo) == DialogResult.Yes) { if (studentBLL.DeleteStudent(studentID)) { MessageBox.Show("删除成功!"); RefreshData(); } else { MessageBox.Show("删除失败!"); } } } else { MessageBox.Show("请先选择一条记录!"); } } private void btnSearch_Click(object sender, EventArgs e) { string keyword = txtKeyword.Text.Trim(); dgvStudent.DataSource = studentBLL.SearchStudents(keyword); } private void btnPrint_Click(object sender, EventArgs e) { PrintDialog printDialog = new PrintDialog(); if (printDialog.ShowDialog() == DialogResult.OK) { PrintDocument printDocument = new PrintDocument(); printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage); printDocument.PrinterSettings = printDialog.PrinterSettings; printDocument.Print(); } } private void printDocument_PrintPage(object sender, PrintPageEventArgs e) { Font font = new Font("宋体", 12, FontStyle.Regular); Brush brush = new SolidBrush(Color.Black); int x = e.MarginBounds.Left; int y = e.MarginBounds.Top; int rowHeight = dgvStudent.Rows[0].Height; int columnCount = dgvStudent.Columns.Count; for (int i = 0; i < columnCount; i++) { e.Graphics.DrawString(dgvStudent.Columns[i].HeaderText, font, brush, x, y); x += dgvStudent.Columns[i].Width; } y += rowHeight; for (int i = 0; i < dgvStudent.Rows.Count; i++) { x = e.MarginBounds.Left; for (int j = 0; j < columnCount; j++) { e.Graphics.DrawString(dgvStudent.Rows[i].Cells[j].Value.ToString(), font, brush, x, y); x += dgvStudent.Columns[j].Width; } y += rowHeight; } } } ``` 2. 业务逻辑层代码 在业务逻辑层中,我们需要实现系统的业务逻辑,例如: ```csharp // StudentBLL.cs public class StudentBLL { private StudentDAL studentDAL = new StudentDAL(); public DataTable GetAllStudents() { return studentDAL.GetAllStudents(); } public bool AddStudent(Student student) { if (studentDAL.GetStudentByID(student.StudentID) == null) { return studentDAL.AddStudent(student); } else { return false; } } public bool UpdateStudent(Student student) { if (studentDAL.GetStudentByID(student.StudentID) != null) { return studentDAL.UpdateStudent(student); } else { return false; } } public bool DeleteStudent(string studentID) { if (studentDAL.GetStudentByID(studentID) != null) { return studentDAL.DeleteStudent(studentID); } else { return false; } } public DataTable SearchStudents(string keyword) { return studentDAL.SearchStudents(keyword); } } ``` 3. 数据访问层代码 在数据访问层中,我们需要实现对数据库的访问和操作,例如: ```csharp // StudentDAL.cs public class StudentDAL { private string connectionString = ConfigurationManager.ConnectionStrings["Student"].ConnectionString; public DataTable GetAllStudents() { string sql = "SELECT StudentID, Name, Sex, Age, Class, Address, Phone FROM StudentInfo"; return SqlHelper.ExecuteDataTable(connectionString, CommandType.Text, sql); } public Student GetStudentByID(string studentID) { string sql = "SELECT StudentID, Name, Sex, Age, Class, Address, Phone FROM StudentInfo WHERE StudentID = @StudentID"; SqlParameter[] parameters = new SqlParameter[]{ new SqlParameter("@StudentID", studentID) }; DataTable dataTable = SqlHelper.ExecuteDataTable(connectionString, CommandType.Text, sql, parameters); if (dataTable.Rows.Count == 1) { Student student = new Student(); student.StudentID = dataTable.Rows[0]["StudentID"].ToString(); student.Name = dataTable.Rows[0]["Name"].ToString(); student.Sex = dataTable.Rows[0]["Sex"].ToString(); student.Age = Convert.ToInt32(dataTable.Rows[0]["Age"]); student.Class = dataTable.Rows[0]["Class"].ToString(); student.Address = dataTable.Rows[0]["Address"].ToString(); student.Phone = dataTable.Rows[0]["Phone"].ToString(); return student; } else { return null; } } public bool AddStudent(Student student) { string sql = "INSERT INTO StudentInfo(StudentID, Name, Sex, Age, Class, Address, Phone) VALUES(@StudentID, @Name, @Sex, @Age, @Class, @Address, @Phone)"; SqlParameter[] parameters = new SqlParameter[]{ new SqlParameter("@StudentID", student.StudentID), new SqlParameter("@Name", student.Name), new SqlParameter("@Sex", student.Sex), new SqlParameter("@Age", student.Age), new SqlParameter("@Class", student.Class), new SqlParameter("@Address", student.Address), new SqlParameter("@Phone", student.Phone) }; return SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql, parameters) == 1; } public bool UpdateStudent(Student student) { string sql = "UPDATE StudentInfo SET Name = @Name, Sex = @Sex, Age = @Age, Class = @Class, Address = @Address, Phone = @Phone WHERE StudentID = @StudentID"; SqlParameter[] parameters = new SqlParameter[]{ new SqlParameter("@StudentID", student.StudentID), new SqlParameter("@Name", student.Name), new SqlParameter("@Sex", student.Sex), new SqlParameter("@Age", student.Age), new SqlParameter("@Class", student.Class), new SqlParameter("@Address", student.Address), new SqlParameter("@Phone", student.Phone) }; return SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql, parameters) == 1; } public bool DeleteStudent(string studentID) { string sql = "DELETE FROM StudentInfo WHERE StudentID = @StudentID"; SqlParameter[] parameters = new SqlParameter[]{ new SqlParameter("@StudentID", studentID) }; return SqlHelper.ExecuteNonQuery(connectionString, CommandType.Text, sql, parameters) == 1; } public DataTable SearchStudents(string keyword) { string sql = "SELECT StudentID, Name, Sex, Age, Class, Address, Phone FROM StudentInfo WHERE StudentID LIKE '%' + @Keyword + '%' OR Name LIKE '%' + @Keyword + '%'"; SqlParameter[] parameters = new SqlParameter[]{ new SqlParameter("@Keyword", keyword) }; return SqlHelper.ExecuteDataTable(connectionString, CommandType.Text, sql, parameters); } } ``` 五、总结 通过以上的介绍,我们可以看到,采用C#语言和三层架构来开发学生信息管理系统可以提高系统的可维护性、可扩展性、可重用性和可测试性等方面的优势,同时也可以有效地分离业务逻辑和数据访问层,使系统的结构更加清晰和易于维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值