使用表单和文件.AppendAllText更新多个日志
本文关键字:更新 日志 AppendAllText 文件 表单 | 更新日期: 2023-09-27 17:53:39
我对编程很陌生,所以如果这太简单了,我很抱歉。
我正在尝试我的第一个程序,c#中的GUI表单,将使用文本框将文本发送到"密钥"文件,这将反过来启动机器人复制同步。在代码中,我试图测试这与。txt文件。
从Program.cs{
string fileName = @"c:''Test.txt";
string textToAdd = textBox1.ToString();
var culture = new CultureInfo("en-US");
string newTime = new DateTime.Now.ToString(culture);
//File.AppendAllText(fileName, newTime, System.Environment.UserName, textToAdd);
//using (StreamWriter w = new StreamWriter(fileName))
{
File.AppendAllText(fileName, String.Format("{0} {1} {2}", newTime, System.Environment.UserName, textToAdd));
}
}
这是来自form .cs,我不确定我需要在这里添加什么才能使按钮工作。目前这些按钮什么也不做,尽管被设置为取消和确定。
public void textBox1_TextChanged(object sender, EventArgs e)
{
string var;
var = textBox1.Text;
}
private void button_OK_Click(object sender, EventArgs e)
{
Form1 myForm = new Form1();
if(myForm.DialogResult == DialogResult.OK)
{
MessageBox.Show("You have successfully added this entry to the key files.");
// object MessageBoxButtons = new MessageBoxButtons;
// MessageBoxButtons.OK = this.Close();
}
}
private void button_Cancel_Click(object sender, EventArgs e)
{
Form1 myForm = new Form1();
if(myForm.DialogResult == DialogResult.Cancel)
{
// this.Close();
Application.Exit();
}
}
我想你忘记添加事件处理程序到你的方法。您可以从按钮属性窗口中的编辑器或代码中执行此操作,例如:
public Form1()
{
InitializeComponent();
this.button_OK.Click+=new EventHandler(button_OK_Click);
this.button_Cancel.Click+=new EventHandler(button_Cancel_Click);
}
无论如何,你的代码有很多错误。在您的button_OK_Click
中,您从相同的形式创建Form1
的新实例是无稽之谈。你不应该使用Program.cs
编辑
让我们分析一下代码中的错误:
Form1
public void textBox1_TextChanged(object sender, EventArgs e)
{
//This does nothing. You are creating a variable whose scope is just
// this method, so you wouldn't be able to access it from outside
string var;
var = textBox1.Text;
}
private void button_OK_Click(object sender, EventArgs e)
{
Form1 myForm = new Form1(); //You are creating a new Form1..Why?
//there won't be a dialogResult,as you are not showing the form with ShowDialog
if(myForm.DialogResult == DialogResult.OK)
{
MessageBox.Show("You have successfully added this entry to the key files.");
// object MessageBoxButtons = new MessageBoxButtons;
// MessageBoxButtons.OK = this.Close();
}
}
private void button_Cancel_Click(object sender, EventArgs e)
{
Form1 myForm = new Form1();
if(myForm.DialogResult == DialogResult.Cancel)
{
// this.Close();
Application.Exit();
}
}
现在多少应该是这样了:
首先删除您添加到Program.cs
的所有代码。然后你的Form1看起来应该是这样的:
Form1
public Form1()
{
InitializeComponent();
this.button_OK.Click += new EventHandler(button_OK_Click);
this.button_Cancel.Click += new EventHandler(button_Cancel_Click);
}
private void button_OK_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Are you sure?", "Add text", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
string fileName = @"c:''Test.txt";
string textToAdd = textBox1.ToString();
string newTime = DateTime.Now.ToString("dd/MM/yy HH:mm:ss");
try
{
File.AppendAllText(fileName, String.Format("{0} {1} {2}", newTime, System.Environment.UserName, textToAdd));
MessageBox.Show("You have successfully added this entry to the key files.");
}
catch (Exception ex)
{
MessageBox.Show("There was an error adding this entry to the key files.");
}
}
}
private void button_Cancel_Click(object sender, EventArgs e)
{
Form1 myForm = new Form1();
if (MessageBox.Show("Are you sure?", "Exit", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
Application.Exit();
}
}