问题
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();
}
Hi, I need to output the variables of valcostsilk.ToStrin
, silkval.ToStrin
, valmatt.ToString
, val44.ToString
, val44.ToString
. Pretty much all of them to just ONE rich text box. As I am new to c# so, I have no idea. I asked on a previous thread, but I couldn't seem to get the answer I needed. Someone mentioned Enviorment.Newline
, but I can't seem to program it right.
Any help appreciated.
回答1:
You need to put your if statements into the event handler for comboBox3 changing. To do this just double-click the comboBox in the designer view and it will create the event handler in the code-behind for you. Move your if statements all into there (you could also use a switch statement). Also shown is how you create a single complex string into the Text property including a newline. I avoid concatenating with +. $"{}"
is cleaner IMHO.
Your code also needs to be cleaned up - you're clearing the same RTB twice in your method for instance, and you reference both comboBox5 and 3 for apparently the same purpose in your series of ifs. You will also need fewer RTBs. Hope it helps.
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();
}
}
Example of switch statement:
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;
}
}
来源:https://stackoverflow.com/questions/45400181/output-multiple-variables-in-one-rich-text-box-in-c-sharp