1

What I want to do is display a messagebox(which is working) and take the MessageBox.Show(textBox1.Text, textBox2.Text) code and put it into a richtextbox when the button is clicked. I am getting an error though, Cannot implicitly convert type 'void' to 'string' Any idea how I can do this?

private void button1_Click(object sender, EventArgs e) { yo(); richTextBox1.Text = yo(); } void yo() { MessageBox.Show(textBox1.Text, textBox2.Text); } 
1
  • +1 for your method name. Yo(); Commented Jan 27, 2014 at 5:06

4 Answers 4

4

yo() returns void, not a string. First you call yo(), then you set richTextBox1.Text to the value returned by yo() which is why the compiler is complaining.

You need to either change the return value of yo() to a string, then return a string from it, or write another method altogether that creates the string that you want to set to richTextBox1.Text.

Sign up to request clarification or add additional context in comments.

Comments

3

A void method does not return a value. Consider changing your method to return a string:

string yo() { MessageBox.Show(textBox1.Text, textBox2.Text); return textBox1.Text + " " + textBox2.Text; } 

Edit based on your comment:

Using composite formatting:

return string.Format("MessageBox.Show(\"{0}\", \"{1}\");", textBox1.Text, textBox2.Text); 

Comments

0

void method doesn't return a value.

Comments

0

Correct me if I'm wrong.

You want to display the string "MessageBox.Show(textBox1.Text, textBox2.Text)" in your rich text box?
If so the following would suffice

string yo() { return "MessageBox.Show(textBox1.Text, textBox2.Text)"; } 

3 Comments

He wants to "display a messagebox", not to display a "MessageBox.Show..." string.
tbh, I like the approach.
@Johnny maybe you were too quick to judge, the answer provided seems to have the same result as what I suggested.