如何正确将文本框和标签转换为字符串

发布于 2024-08-16 20:38:58 字数 895 浏览 11 评论 0原文

我有这段代码

FileStream fs = new FileStream("Scores.txt", FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs);
        sw.Write("Name: " + name_box.Text + " Time " + label1.Text);
        sw.Close();

,很简单,label1 被分配给计时器刻度,如下所示,

private void timer1_Tick(object sender, EventArgs e)
    {
        // Format and display the TimeSpan value. 
        TimeSpan ts = stopWatch.Elapsed;
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
       label1.Text = elapsedTime;
    }

现在当我打开文本文件时,我发现以下结果

Name: Tony Time 00:00:06.67Text:  Time [System.Windows.Forms.Timer], Interval: 100

是完美的,但是什么(文本:时间[System.Windows.Forms.Timer] ,间隔:100) 我不希望它

提前出现在 txt 中

I have this code

FileStream fs = new FileStream("Scores.txt", FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter sw = new StreamWriter(fs);
        sw.Write("Name: " + name_box.Text + " Time " + label1.Text);
        sw.Close();

which is simple the label1 is assgined to a Timer tick as in the folowing

private void timer1_Tick(object sender, EventArgs e)
    {
        // Format and display the TimeSpan value. 
        TimeSpan ts = stopWatch.Elapsed;
        string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
            ts.Hours, ts.Minutes, ts.Seconds,
            ts.Milliseconds / 10);
       label1.Text = elapsedTime;
    }

now when I open the Text File I found the following results

Name: Tony Time 00:00:06.67Text:  Time [System.Windows.Forms.Timer], Interval: 100

which are perfect but what is ( Text: Time [System.Windows.Forms.Timer], Interval: 100)
I don't want that to appear in the txt

thanx in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽心 2024-08-23 20:38:58

您的代码中的其他位置可能有以下行:

label1.Text = timer1.ToString();

您应该在代码中单击单词 label1,然后单击“查找所有引用”以查看您还用它做什么。


顺便说一下,您应该使用 File,而不是创建流。 WriteAllText,如下所示:

File.WriteAllText("Scores.txt", "Name: " + name_box.Text + " Time " + label1.Text);

You probably have the following line somewhere else in your code:

label1.Text = timer1.ToString();

You should write-click the word label1 in your code, then click Find All References to see what else you're doing with it.


By the way, instead of creating a stream, you should use File.WriteAllText, like this:

File.WriteAllText("Scores.txt", "Name: " + name_box.Text + " Time " + label1.Text);
绝不放开 2024-08-23 20:38:58

您正在构造函数中使用FileMode.OpenOrCreate。这不会删除文件以前的内容。我怀疑如果您删除该文件然后再次尝试运行程序,您将不会看到任何额外的内容。

我建议使用 FileMode.CreateFileMode.Append。如果您想覆盖结果,请使用第一个,如果您想……好吧,追加,请使用第二个。

You are using FileMode.OpenOrCreate in the constructor. This does not erase the previous contents of the file. I suspect that if you delete the file and then try running your program again, you won't see any of that extra stuff.

I suggest either using FileMode.Create or FileMode.Append. Use the first if you want to overwrite the results, the second if you want to... well, append.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文