在文本框/块中打印多行.net c#

时间:2016-04-11 20:10:56

标签: c# .net visual-studio-2015

所以我想要做的就是将输出打印到文本框但是它发生了什么,它总是使用它找到的最后一个textbox2.text,因为我假设每次打印它它会覆盖最后一个输出。我正在寻找某种工作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Web;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace ProgrammingProject
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void btn_getinfo_Click(object sender, RoutedEventArgs e)
        {
            string UserInput = "76561198056932916";
           // string UserInput = textBox.Text;
            ProfileSummary(UserInput);
        }

        public void ProfileSummary(string SteamID)
        {
            string SteamKey = "CensoredAPIKEY";
            WebClient c = new WebClient(); 
            var data = c.DownloadString("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/" + "?key=" + SteamKey + "&steamids=" + SteamID);
            //Console.WriteLine(data);
            JObject o = JObject.Parse(data);
            JArray players = (JArray)o["response"]["players"];

            if (players.Count > 0)
            {
                int PrivacyLevel = (int)players[0]["communityvisibilitystate"];
                string username = (string)players[0]["personaname"];
                string ProUrl = (string)players[0]["profileurl"];
                double LastLogOff = (double)players[0]["lastlogoff"];
                int CurrentStatus = (int)players[0]["personastate"];
                int GamePlaying = (int)players[0]["gameid"];

                textBox2.Text = "your username is " + username + "\n";
                textBox2.Text = "You last logged off on " + ConvertUnix(LastLogOff) + "\n";

                if (PrivacyLevel == 3)
                {
                    textBox2.Text = "This user's account is public"; 
                }
                else if (PrivacyLevel == 1)
                {
                    textBox2.Text = "This user's account is private";
                }
                switch (CurrentStatus)
                {
                    case 1:
                        textBox2.Text = "This user is offline";
                        break;
                    case 2:
                        textBox2.Text = "This User is online and playing";
                        break;
                }
            }
            else
            {
                textBox2.Text = "You have entered the SteamID incorrectly";
            }

        Console.ReadLine();  // Don't remove you idiot

    }

    public static DateTime ConvertUnix(double unixTimeStamp)
    {
        // Unix timestamp is seconds past epoch
        System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Local);
        dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
        return dtDateTime;
    }
}

此代码的输出将是最后一个textbox2.text,因此在这种情况下它将是switch语句。我知道我没有解释它最好,但我不知道为什么会这样,谢谢

4 个答案:

答案 0 :(得分:5)

您应该使用textBox2.Text += "more text to be added to the text box"代替textBox2.Text = "more text to be added to the text box"

答案 1 :(得分:3)

这里的问题是,使用textBox2.Text =,您将覆盖该值。相反,如果要添加行,请使用textBox2.Text += "myString";。这将字符串附加到文本框而不是覆盖它。

答案 2 :(得分:3)

你应该先使用+ =运算符。

            textBox2.Text = "your username is " + username + "\n";
            textBox2.Text += "You last logged off on " + ConvertUnix(LastLogOff) + "\n";

            if (PrivacyLevel == 3)
            {
                textBox2.Text += "This user's account is public"; 
            }
            else if (PrivacyLevel == 1)
            {
                textBox2.Text += "This user's account is private";
            }


            switch (CurrentStatus)
            {
                case 1:
                    textBox2.Text += "This user is offline";
                    break;
                case 2:
                    textBox2.Text += "This User is online and playing";
                    break;

            }
        }

        else
        {
            textBox2.Text += "You have entered the SteamID incorrectly";

        }

答案 3 :(得分:1)

答案如上所述,但我建议您使用 richtextbox,因为你有多条消息,如果你在单独的行中显示它们会更好

richtextbox1.text="\r\n"+some string;

“\ r \ n”是转到新行,然后在该新行中插入文本