2017-07-29 44 views
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(); 
} 

目前我正在將文本插入多個文本框,而不是我想在一個富文本框中輸出變量 - 通過附加字符串。如何在c#中將變量輸出到一個接一個的富文本框?

+1

已經在下面的答案解釋如何可以連接兩個字符串,使用'+'運算符和'string.Format'。你也可以使用['stringBuilder'](https://docs.microsoft.com/en-us/dotnet/standard/base-types/stringbuilder)類 - 當你進行多個連接時,它會稍微高效一些。 – Nisarg

回答

1

你可以做類似的字符串格式化,以幫助空間與空間的字符串。

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表示新行,你可以找到有關MSDN上的String.Format()方法的詳細信息: https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx

+2

您應該始終使用'Environment.NewLine'而不是'\ r \ n'。 – Nisarg

+0

richTextBox9.Text =「這是成本:」+ valcostsilk.ToString.enviroment.newline();這是正確的嗎? –

+0

@NelJ它看起來更像這樣,'richTextBox.Text =「這是成本:@',然後加入'richTextBox.Text = richTextBox.Text.Replace(」@「,Environment.NewLine());'那將用新行代替'@' –

相關問題