Re: Having track of actions done by a button
NOOOOO it is very very important that user just be able to add 3 lines to the text files. and if you saw my program picture in here :
http://[img]http://forums.codeguru.c....jpg[/img]
When the form starts, save1 and save2 button Enabled property is set to false so user cant do something. As soon as he click on start, then those save buttons will be enabled and user can enter a text in textbox and save it in text files using those buttons maximum 3 times. AND THIS 3 times just is a example it could be less or more. after the saving is done, the buttons will be disabled again!
I know I dont need the start button,,,but lets see this as a fight with the code and insist on precense of the start button.
OMG is this that hard :( :(
Re: Having track of actions done by a button
1. I think you've misread what I've posted...
2. I get what your intent is here but you're going about it in an odd way. I get you're trying to allow the user to write to a file 3 times but at no point in your application are you allowing the user to do anything. The code is sequential so once you've enabled the save buttons you then enter a while loop that runs infinitely because save1 and save2 forever equal '0'. The while loop and its checks need to be moved out of start and into its own method. You can achieve something closer to your intended 'event handler' solution if you move everything inside the while loop into a thread.
3. I've rewritten what you've posted to something that works at least close to what you're trying to get at here. Again one issue that you are having is that you have to make sure that save1 and save2 are equal because if I the user hit buttonSave1 4 times and buttonSave2 once then save1=4 while save2=1 and at no point in time will they ever equal 3.
Code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//--- Number of saves
int save1 = 0; // Counter for buttonSave1
int save2 = 0; // Counter for buttonSave2
//--- Maximum number of saves allowed
int max = 3;
//Note: changed 'i' to max because its easier to read that way
System.IO.StreamWriter file1;
System.IO.StreamWriter file2;
public Form1()
{
InitializeComponent();
//--- Initialize save buttons to false
buttonSave1.enabled = false;
buttonSave2.enabled = false;
this.label3.Text = "0";
this.label4.Text = "0";
}
//--- Check the number of saves
private void CheckSaves()
{
//--- Make sure that both values are the same
if(save1 == save2)
{
//--- Since both saves are equal we only need to check one of them not both
if(save1 == 1)
{
//--- Decrement
max--;
}
else if(save1 == 2)
{
//--- Decrement
max--;
}
else if(save1 == 3)
{
//--- Decrement
max--;
//--- Reset values
save1 = 0;
save2 = 0;
//You can put 'file1.close();' and 'file2.close();' here but you need to remove it in the handlers...
}
}
else //--- Else save1 and save2 are not equal and we should make sure that they do not save more than the maximum amount
{
//--- if either save1 or save2 have hit the max then do not allow the user to keep saving
if(save1 == max)
{
//--- disable the save so that we don't go past the max
buttonSave1.enable = false;
}
else if(save2 == max)
{
//--- disable the save so that we don't go past the max
buttonSave2.enable = false;
}
}
}
//--- ButtonSave1's event handler
private void buttonSave1_Click(object sender, EventArgs e)
{
//--- If we are over the limit for the number of saves then do not allow another write to happen
if(save1 <= max)
{
//Note: this should be within a Try/Catch block
//--- ensure that we correctly open the stream writer
file1 = new System.IO.StreamWriter("c:\\test1.txt");
//--- write stream to text
file1.WriteLine(textBox1.Text);
//--- close writer
file1.close();
//--- increment number of saves
save1++;
//--- Display current number of saves
this.label3.Text = "Save " + save1.ToString() + " of " + max.ToString();
}
//--- determine if we have hit the mazimum
CheckSaves();
}
//--- ButtonSave2's event handler
private void buttonSave2_Click(object sender, EventArgs e)
{
//--- If we are over the limit for the number of saves then do not allow another write to happen
if(save1 <= max)
{
//Note: this should be within a Try/Catch block
//--- ensure that we correctly open the stream writer
file2 = new System.IO.StreamWriter("c:\\test2.txt");
//--- write stream to text
file2.WriteLine(textBox1.Text);
//--- close writer
file2.close();
//--- increment number of saves
save2++;
//--- Display current number of saves
this.label3.Text = "Save " + save2.ToString() + " of " + max.ToString();
}
//--- determine if we have hit the mazimum
CheckSaves();
}
private void buttonStart_Click(object sender, EventArgs e)
{
//--- Turn save buttons on
buttonSave1.enabled = true;
buttonSave2.enabled = true;
}
}
}
Re: Having track of actions done by a button
Thanks, this work a little bit like I want, but still cant do the job. I am trying to fix it since yesterday but got me to no where yet.
Re: Having track of actions done by a button
I just whipped this up in about five minutes - is this what you're trying to do?
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace SaveLines
{
public partial class Form1 : Form
{
private int save1 = 0;
private int save2 = 0;
private string file1 = "";
private string file2 = "";
public Form1()
{
InitializeComponent();
btnSave1.Enabled = false;
btnSave2.Enabled = false;
label1.Text = "File 1: 0";
label2.Text = "File 2: 0";
}
private void btnStart_Click(object sender, EventArgs e)
{
btnSave1.Enabled = true;
btnSave2.Enabled = true;
}
private void btnSave1_Click(object sender, EventArgs e)
{
file1 += textBox1.Text;
save1++;
label1.Text = "File 1: " + save1;
if (save1 >= 3)
btnSave1.Enabled = false;
else
file1 += Environment.NewLine;
File.WriteAllText("text1.txt", file1);
}
private void btnSave2_Click(object sender, EventArgs e)
{
file2 += textBox1.Text;
save2++;
label2.Text = "File 2: " + save2;
if (save2 >= 3)
btnSave2.Enabled = false;
else
file2 += Environment.NewLine;
File.WriteAllText("text2.txt", file2);
}
}
}