无法从 'string' 转换为 'System.Windows.Forms.MessageBoxIcon

cannot convert from 'string' to 'System.Windows.Forms.MessageBoxIcon

我正在开发一款软件,可以让您生成自己的自定义 Messagebox。在我尝试放入 MessageBoxButtons 和 MessageBoxIcon 之前,我的代码没有错误。然后,我得到错误

2 Argument 3: cannot convert from 'string' to 'System.Windows.Forms.MessageBoxButtons' and Error 3 Argument 4: cannot convert from 'string' to 'System.Windows.Forms.MessageBoxIcon'. What is the issue here?

if (textBoxX1.Text == String.Empty)
    MessageBox.Show("You must enter a title.");
else if (richTextBoxEx1.Text == String.Empty)
    MessageBox.Show("You must enter a body.");
else
{
    string previewtype = string.Empty;
    string previewbutton = string.Empty;
    if (radioButton1.Checked == true)
        previewtype = "MessageBoxIcon.Error";
    else if (radioButton2.Checked == true)
        previewtype = "MessageBoxIcon.Information";
    else if (radioButton3.Checked == true)
        previewtype = "MessageBoxIcon.Exclamation";
    else if (radioButton4.Checked == true)
        previewtype = "MessageBoxIcon.Question";
    if (radioButton8.Checked == true)
        previewbutton = "MessageBoxButtons.AbortRetryIgnore";
    else if (radioButton7.Checked == true)
        previewbutton = "MessageBoxButtons.OK";
    else if (radioButton6.Checked == true)
        previewbutton = "MessageBoxButtons.OKCancel";
    else if (radioButton5.Checked == true)
        previewbutton = "MessageBoxButtons.RetryCancel";
    else if (radioButton9.Checked == true)
        previewbutton = "MessageBoxButtons.YesNo";
    else if (radioButton10.Checked == true)
        previewbutton = "MessageBoxButtons.YesNoCancel";
    MessageBox.Show(textBoxX1.Text, richTextBoxEx1.Text, previewbutton, previewtype);
}
MessageBox.Show(textBoxX1.Text, richTextBoxEx1.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);

第三个和第四个参数不需要字符串值。您应该传递 MessageBoxButtonMessageBoxIcon

类型的值

因此,您可以在 if-else 条件中给出所需的类型,而不是分配字符串值。

MessageBoxButton previewbutton;
MessageBoxIcon previewtype;

 if (radioButton1.Checked == true)
    previewtype = MessageBoxIcon.Error;

这个问题是当你说

MessageBox.Show(textBoxX1.Text, richTextBoxEx1.Text, previewbutton, previewtype);

您将名为 previewButtonpreviewtype 的两个字符串作为参数传递给 MessageBox.Show 方法,而系统期望输入 MessageBoxButtonsMessageBoxIcon他们的位置。

你用的overload for MessageBox.Show方法是这样的

MessageBox.Show(String, String, MessageBoxButtons, MessageBoxIcon)

但是您传递的是两个字符串 - 因此出现错误。

错误是因为您将字符串传递给 MessageBox.Show(),而您应该在此处传递 MessageBoxButtons 和 MessageBoxIcon。换句话说,你不能这样做:

MessageBox.Show("Some text", "a caption", "MessageBoxButtons.AbortRetryIgnore", "MessageBoxIcon.Error");

但是你可以这样做:

MessageBox.Show("Some text", "a caption", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);

您的 previewtype 和 previewbutton 变量应定义为实际类型,而不是字符串:

MessageBoxIcon previewtype;
MessageBoxButtons previewbutton;

如果我们更新您的样本以使用正确的数据类型,它看起来像这样:

if (textBoxX1.Text == String.Empty)
    MessageBox.Show("You must enter a title.");
else if (richTextBoxEx1.Text == String.Empty)
    MessageBox.Show("You must enter a body.");
else
{
    MessageBoxIcon previewtype;
    MessageBoxButtons previewbutton;
    if (radioButton1.Checked == true)
        previewtype = MessageBoxIcon.Error;
    else if (radioButton2.Checked == true)
        previewtype = MessageBoxIcon.Information;
    else if (radioButton3.Checked == true)
        previewtype = MessageBoxIcon.Exclamation;
    else if (radioButton4.Checked == true)
        previewtype = MessageBoxIcon.Question;
    if (radioButton8.Checked == true)
        previewbutton = MessageBoxButtons.AbortRetryIgnore;
    else if (radioButton7.Checked == true)
        previewbutton = MessageBoxButtons.OK;
    else if (radioButton6.Checked == true)
        previewbutton = MessageBoxButtons.OKCancel;
    else if (radioButton5.Checked == true)
        previewbutton = MessageBoxButtons.RetryCancel;
    else if (radioButton9.Checked == true)
        previewbutton = MessageBoxButtons.YesNo;
    else if (radioButton10.Checked == true)
        previewbutton = MessageBoxButtons.YesNoCancel;
    MessageBox.Show(textBoxX1.Text, richTextBoxEx1.Text, previewbutton, previewtype);