2017-07-30 115 views
1
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(); 
} 

您好,我需要輸出的valcostsilk.ToStrinsilkval.ToStrinvalmatt.ToStringval44.ToStringval44.ToString的變量。幾乎所有的人都只是一個富文本框。由於我是c#的新手,所以我不知道。我詢問了上一個主題,但似乎無法得到我需要的答案。有人提到Enviorment.Newline,但我似乎無法編程正確。輸出多個變量在C#

任何幫助表示讚賞。

+0

有一個RTB mehod'AppendText'和是添加一個Environment.NewLine爲好。 – TaW

+0

你能舉個例子讓我開始嗎?我現在想做,但似乎並不想爲我做。 –

+0

'commonRTB.AppendText(「This is the number of primer tins」+ primer.ToString()+ Environment.NewLine);' – TaW

回答

0

您需要將您的if語句放入comboBox3更改的事件處理程序中。要做到這一點,只需雙擊設計器視圖中的組合框,它將在代碼隱藏中爲您創建事件處理程序。將你的if語句全部移到那裏(你也可以使用switch語句)。還顯示瞭如何在Text屬性中創建一個包含換行符的單個複雜字符串。我避免與+連接。 $"{}"是更清潔的恕我直言。

您的代碼也需要清理 - 例如,您在方法中清除了相同的RTB兩次,並且在ifs系列中顯然具有相同目的的情況下引用comboBox5和3。您還需要更少的RTB。希望能幫助到你。 switch語句的

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     if (comboBox3.Text == "Matt") 
     { 
      richTextBox10.Text = $"This is how many 2.5 tins of paint are needed: {val44.ToString()}.{Environment.NewLine}This is the matt cost: {valmatt.ToString()}"; 
      richTextBox10.Update(); 
     } 
    } 

實施例:

switch (comboBox3.Text) 
{ 
    case ("Matt"): 
     { 
      richTextBox10.Text = $"This is how many 2.5 tins of paint are needed: {val44.ToString()}.{Environment.NewLine}This is the matt cost: {valmatt.ToString()}"; 
      richTextBox10.Update(); 
      break; 
     } 

    case ("Vinyl"): 
     { (insert code) 
      break; 
     } 
}