【问题标题】:Output multiple variables in one rich text box in c# [duplicate]在c#的一个富文本框中输出多个变量[重复]
【发布时间】:2017-07-30 13:11:43
【问题描述】:
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,但我似乎无法正确编程。

任何帮助表示赞赏。

【问题讨论】:

  • 有一个 RTB 方法AppendText,是的,还添加了一个 Environment.NewLine。
  • 你能给我一个例子让我开始吗?我现在正在尝试这样做,但它似乎不想为我这样做。
  • commonRTB.AppendText( "This is the number of primer tins" + primer.ToString() + Environment.NewLine);
  • Environment.NewLine 特定于代码执行的环境。例如,对于非 Unix 平台,这将获取包含“\r\n”的字符串,或者对于 Unix 平台,获取包含“\n”的字符串 - 所以它只是一个字符串,就像任何其他具有您想要的特定内容的字符串一样。跨度>
  • 您正在将基于两个组合框 comboBox3comboBox5 的文本(在您的 clear 方法中为空)替换为各种文本框。在一个地方做多件这样的事情似乎会让你感到困惑;这是真的吗?如果您为每个文本框创建一个StringBuilder,它也可能对您有所帮助,将它们设置为您希望文本框的内容,然后使用您拥有的每个文本框设置一次文本;帮助您更好地理解您的代码。

标签: c# variables richtextbox


【解决方案1】:

您需要将 if 语句放入事件处理程序中以更改 comboBox3。为此,只需在设计器视图中双击组合框,它将在代码隐藏中为您创建事件处理程序。将你的 if 语句全部移到那里(你也可以使用 switch 语句)。还展示了如何在 Text 属性中创建单个复杂字符串,包括换行符。我避免与 + 连接。 $"{}" 恕我直言更干净。

您的代码也需要清理 - 例如,您在方法中清除了两次相同的 RTB,并且您在一系列 if 中引用了 comboBox5 和 3 显然是相同的目的。您还将需要更少的 RTB。希望对您有所帮助。

    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语句示例:

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;
        }
}

【讨论】:

    猜你喜欢
    • 2017-12-16
    • 2015-04-20
    • 1970-01-01
    • 1970-01-01
    • 2019-09-03
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    相关资源
    最近更新 更多