c#在一个富文本框中输出多个变量 [英] Output multiple variables in one rich text box in c#

查看:53
本文介绍了c#在一个富文本框中输出多个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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的变量.ToString, val44.ToString.几乎所有这些都只有一个富文本框.由于我是 C# 新手,所以我不知道.我在之前的一个帖子中问过,但我似乎无法得到我需要的答案.有人提到了 Enviorment.Newline,但我似乎无法正确编程.

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.

任何帮助表示赞赏.

推荐答案

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

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.

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

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

switch 语句示例:

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

这篇关于c#在一个富文本框中输出多个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆