How to output variables to a rich text box one aft

2020-05-02 05:05发布

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.

标签: c# wpf
2条回答
家丑人穷心不美
2楼-- · 2020-05-02 05:11

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://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/strings/how-to-concatenate-multiple-strings

查看更多
戒情不戒烟
3楼-- · 2020-05-02 05:15

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

查看更多
登录 后发表回答