【问题标题】:Customized MessageBox (dialog box) Windows Forms C#自定义 MessageBox(对话框)Windows 窗体 C#
【发布时间】:2025-12-20 12:50:06
【问题描述】:

为了尝试使用对话框而不是 MessageBox,我使用了以下代码:

        static public DialogResult ShowDialog(string title, string largeHeading, string smallExplanation,
        string leftButton, string rightButton, Image iconSet)
    {
        using (BetterDialog dialog = new BetterDialog(title, largeHeading, smallExplanation, leftButton,
            rightButton, iconSet))
        {
            DialogResult result = dialog.ShowDialog();
            return result;
        }
    }

更多详情,此代码可在here找到

然后我使用按钮点击事件调用对话框如下:

        private void btnDialog_Click(object sender, EventArgs e)
    {
        BetterDialog dialogBox = new BetterDialog("Special Dialog", "large heading", "small explanation", null, "Ok", null);
        dialogBox.ShowDialog(this);
    }

我得到了错误:

“DotNetPerls.BetterDialog”不包含采用 6 个参数的构造函数。

怎么了,请问有什么办法吗?

【问题讨论】:

    标签: c# winforms dialog messagebox showdialog


    【解决方案1】:

    我猜BetterDialog 带有 6 个参数的构造函数是私有的(或受保护的)而不是公共的......

    这意味着使用它的接口不是由构造函数,而是仅通过静态方法:

    private void btnDialog_Click(object sender, EventArgs e)
    {
        DialogResult result = BetterDialog.ShowDialog("Special Dialog", "large heading", "small explanation", null, "Ok", null);
        if (result == DialogResult.OK)
        {
           // Do what you want to do when OK button is pressed
        }
    }
    

    【讨论】:

    • 你的回答是对的,非常感谢。请我再问一个小问题:如何从目录中指定要在对话框中显示的图像?
    • @eyossi,你看到我的最后一个问题了吗?
    【解决方案2】:

    在您的表单中添加一个图片框并使用Image.FromFile()

    【讨论】: