如何正确将文本框和标签转换为字符串
我有这段代码
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码中的其他位置可能有以下行:
您应该在代码中单击单词
label1
,然后单击“查找所有引用”以查看您还用它做什么。顺便说一下,您应该使用
File,而不是创建流。 WriteAllText
,如下所示:You probably have the following line somewhere else in your code:
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:您正在构造函数中使用
FileMode.OpenOrCreate
。这不会删除文件以前的内容。我怀疑如果您删除该文件然后再次尝试运行程序,您将不会看到任何额外的内容。我建议使用
FileMode.Create
或FileMode.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
orFileMode.Append
. Use the first if you want to overwrite the results, the second if you want to... well, append.