Reputation: 3
I'm very new to programming, so apologies if this is too simple.
I'm attempting my first program, a GUI form in C# that will use a textbox to send text to a "key" file, which will in turn start a robocopy sync. In the code I'm trying to test this with a .txt file.
from 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));
}
}
This is from the form1.cs, I'm not sure what I need to add here to get the button to work. Currently the buttons do nothing despite being set as Cancel and OK.
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();
}
}
Upvotes: 0
Views: 139
Reputation: 7713
I guess you forgot to add the event handler to your method. You can do it from the editor in the button property window or in your code, with for example:
public Form1()
{
InitializeComponent();
this.button_OK.Click+=new EventHandler(button_OK_Click);
this.button_Cancel.Click+=new EventHandler(button_Cancel_Click);
}
Anyway, your code has a lot of errors. It's a nonsense in your button_OK_Click
that you create a new instance of Form1
from the same form. And you should'n have any of the code you have in Program.cs
Edit
Let's analize what is wrong in your code:
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();
}
}
Now more or less how it should be:
First delete all the code you added to Program.cs
. And then yout Form1 should look more or less like this:
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();
}
}
Upvotes: 1