返回值的C#自定义消息框确定或取消

时间:2015-10-02 20:35:18

标签: c# winforms

因为我想要一个看起来更好看的消息框,它实际上出现在我希望它出现的位置,我必须创建自己的自定义消息框。

如何制作一个返回值的文件?

内置的MessageBox.Show可以返回一个DialogResult。我猜我创建了类似DialogResult类的东西,还是可以使用那个类?

2 个答案:

答案 0 :(得分:1)

您需要做的就是创建自己的自定义表单。所有表单都继承System.Windows.Forms.Form。当您想要显示表单时,调用ShowDialog(),它在窗体关闭时返回一个DialogResult。根据您在消息框中单击的内容,您可以设置内部DialogResult属性并调用Close()。

CustomMessageBox类

public partial class CustomMessageBox : Form
{
    public CustomMessageBox()
    {
        InitializeComponent();
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        // Some other logic for OK button
        this.DialogResult = DialogResult.OK;
        this.Close();
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        // Some other logic for Cancel button
        this.DialogResult = DialogResult.Cancel;
        this.Close();
    }
}        

要使用消息框,它只是

CustomMessageBox customMessage = new CustomMessageBox();
DialogResult result = customMessage.ShowDialog();

当然你需要添加更多来显示实际的消息,你可以创建一个像MessageBox.Show()一样静态的show方法,但这是DialogResult部分的基础。

答案 1 :(得分:1)

我很欣赏James的回答,但是我想发布一个答案,提供我为解决这个问题所做的全部结果,以创建一个更实用的自定义消息框工具。

请参阅以下代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;

namespace MyProgram
{
    class CustomMessageBox
    {
        Label txtMsg = new Label();
        Button btnOK = new Button();
        Button btnCancel = new Button();
        Form newForm = new Form();

        private DialogResult spawnForm(string title, string text, MessageBoxButtons type)
        {            
            if(type == MessageBoxButtons.OKCancel)
            {
                newForm.Text = title;
                newForm.Controls.Add(txtMsg);
                txtMsg.AutoSize = true;
                txtMsg.Text = text;
                newForm.Width = txtMsg.Width + 125;
                newForm.Height = txtMsg.Height + 125;
                newForm.MaximumSize = new Size(newForm.Width, newForm.Height);
                newForm.MinimumSize = new Size(newForm.Width, newForm.Height);
                txtMsg.Location = new Point(newForm.Width / 2 - txtMsg.Width / 2, newForm.Height / 2 - 40);
                newForm.Controls.Add(btnOK);
                newForm.Controls.Add(btnCancel);
                btnOK.Text = "OK";
                btnCancel.Text = "Cancel";

                btnOK.Location = new Point(newForm.Width / 2 - btnOK.Width / 2 - 60, txtMsg.Location.Y + txtMsg.Height + 20);
                btnCancel.Location = new Point(newForm.Width / 2 - btnOK.Width / 2 + 40, btnOK.Location.Y);
                btnOK.DialogResult = DialogResult.OK;
                btnCancel.DialogResult = DialogResult.Cancel;
                newForm.StartPosition = FormStartPosition.CenterParent;
                return newForm.ShowDialog();
            } else
            {                
                newForm.Text = title;
                newForm.Controls.Add(txtMsg);
                txtMsg.AutoSize = true;
                txtMsg.Text = text;
                newForm.Width = txtMsg.Width + 125;
                newForm.Height = txtMsg.Height + 125;
                newForm.MaximumSize = new Size(newForm.Width, newForm.Height);
                newForm.MinimumSize = new Size(newForm.Width, newForm.Height);
                txtMsg.Location = new Point(newForm.Width / 2 - txtMsg.Width / 2 - 10, newForm.Height / 2 - 40);
                newForm.Controls.Add(btnOK);
                newForm.Controls.Remove(btnCancel);
                btnOK.Text = "OK";
                btnOK.Location = new Point(newForm.Width / 2 - btnOK.Width / 2 -10, txtMsg.Location.Y + txtMsg.Height + 20);
                btnOK.DialogResult = DialogResult.OK;
                newForm.StartPosition = FormStartPosition.CenterParent;
                return newForm.ShowDialog();
            }              
        }   

        public DialogResult Text(string title, string text, MessageBoxButtons type)
        {
            return spawnForm(title, text, type);
        }
    }
}
相关问题