Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过代码向文本框中添加行,同时保留前面的行?C#_C# - Fatal编程技术网

通过代码向文本框中添加行,同时保留前面的行?C#

通过代码向文本框中添加行,同时保留前面的行?C#,c#,C#,我使用的是一个基本的文本框,程序是用c#编码的,我目前正在使用一个生成器生成字符串,这些字符串将输出到用户的文本框中,但是我希望生成的前几行对用户保持可见,而不是用新的字符串替换旧的生成字符串 //Random generator Random rnd = new Random(); //Int int Player1_Attack = rnd.Next(1, 11); String p1_attack = Player1_At

我使用的是一个基本的文本框,程序是用c#编码的,我目前正在使用一个生成器生成字符串,这些字符串将输出到用户的文本框中,但是我希望生成的前几行对用户保持可见,而不是用新的字符串替换旧的生成字符串

//Random generator
        Random rnd = new Random();

        //Int
        int Player1_Attack = rnd.Next(1, 11);

        String p1_attack = Player1_Attack.ToString();

        if (Player1_Attack == 4 || Player1_Attack == 6 || Player1_Attack == 8 || Player1_Attack == 10) {

            txt_combat.Text = "\nYou have Struck your opponent!";

        }

        if (Player1_Attack == 2)
        {
            txt_combat.Text = "\nYou landed a Critical Attack!";

        }

        else
        {

            txt_combat.Text = "\nSigh... your attack missed.";

        }
简言之:

txt_combat.Text += "\r\nYou landed a critical attack!";

两者的作用相同。

您可以使用+=运算符而不是此=运算符

string Name = "MyName";

Name = "Tornike";

Console.WriteLine(Name) // Output will be Tornike

Name += "Tornike";

Console.WriteLine(Name) // Output will be MyNameTornike

您是否尝试过
.Text+=“…”?这是Windows窗体还是ASP.NET(或者我猜可能是WPF或Silverlight)?C#不够具体。。。我这样问是因为
txt\u combat.AppendText()
可能更有效:
string Name = "MyName";

Name = "Tornike";

Console.WriteLine(Name) // Output will be Tornike

Name += "Tornike";

Console.WriteLine(Name) // Output will be MyNameTornike