I'm working on a piece of software that lets you generate your own customized Messagebox
. My code has no errors until I try and put in the MessageBoxButtons and MessageBoxIcon
. Then, I get Error
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);
}
The error is because you're passing strings to MessageBox.Show(), where you should be passing MessageBoxButtons and MessageBoxIcon. In other words, you cannot do this:
MessageBox.Show("Some text", "a caption", "MessageBoxButtons.AbortRetryIgnore", "MessageBoxIcon.Error");
But you can do this:
MessageBox.Show("Some text", "a caption", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error);
Your previewtype and previewbutton variables should be defined as the actual types, not as strings:
MessageBoxIcon previewtype;
MessageBoxButtons previewbutton;
If we update your sample to use the correct data types, it looks like this:
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);