0
void ClearAllRichtextboxes()
{
    richTextBox3.Clear();
    richTextBox5.Clear();
    richTextBox6.Clear();
    richTextBox9.Clear();
    richTextBox10.Clear();  
}

ClearAllRichtextboxes();

if (comboBox5.Text == "Primer")
{
    richTextBox5.Text = "This is the number of primer tins" + primer.ToString();
    richTextBox6.Text = "This is the cost of the primer tins" + primercost.ToString();
}

if (comboBox3.Text == "Matt")
{
    richTextBox10.Text = "This is how many 2.5 tins of paint are needed: " + val44.ToString();
    richTextBox9.Text = "This is the matt cost" + valmatt.ToString();
}

if (comboBox3.Text == "Vinyl ")
{
    richTextBox10.Text = "This is how many 2.5 tins of paint are needed" + val44.ToString();
    richTextBox9.Text = "This is the of vinyl cost" + valmatt.ToString();
}

if (comboBox3.Text =="Silk")
{
    richTextBox10.Text = "This is how many 2.5 tins of paint are needed" + silkval.ToString();
    richTextBox9.Text = "This is the cost: " + valcostsilk.ToString();
}

Currently I am inserting text into multiple textboxes, instead I would like to output variables in one rich text box - by appending the strings.

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
Nel J
  • 13
  • 3
  • 1
    The answers below already explain how you could concatenate two strings, using `+` operator and `string.Format`. You could also use the [`stringBuilder`](https://learn.microsoft.com/en-us/dotnet/standard/base-types/stringbuilder) class - which is slightly more efficient when you are doing multiple concatenations. – Nisarg Shah Jul 30 '17 at 05:28

2 Answers2

1

Update the richTextBox.Text with the new information. If you want to append the new strings to what is already there use "+". You can save the string as its own variable if it helps.

richTextBox.Text = "First segment.";  
richTextBox.Text = richTextBox.Text + " Second segment.";  

More info about string concatenation: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/how-to-concatenate-multiple-strings

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
Colin
  • 11
  • 2
1

You could do something like string formatting to help space the strings with spaces.

something like

richTextBox1.Text = String.Format("This is the number of A in B: {0}\r\n This is the number of X in Y: {1}", output1, output2);

\r\n indicates a new line, you can find more information about the String.Format() method on msdn: https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx

S. Nog
  • 61
  • 10
  • 2
    You should always use `Environment.NewLine` instead of `\r\n`. – Nisarg Shah Jul 30 '17 at 05:23
  • richTextBox9.Text = "This is the cost: " + valcostsilk.ToString.enviroment.newline(); Is this right? – Nel J Jul 30 '17 at 12:17
  • @NelJ It would look more like this, `richTextBox.Text = "This is the cost: @`, then adding `richTextBox.Text = richTextBox.Text.Replace("@", Environment.NewLine());` That will replace '@' with a new line. – S. Nog Jul 31 '17 at 12:52
  • @NelJ Please select an answer in the future to avoid unnecessary traffic to this question – S. Nog Aug 01 '17 at 05:22