|
-
February 14th, 2011, 07:16 AM
#1
Having track of actions done by a button
Dear Friends,
I want to write a simple program which will allow user to only do something for limited times (which will be hard coded in to the program).
I have a TextBox and two Save buttons. I also have a third button which is named Start. if user click on Start then he can start entering a text into TextBox and save the text in two different files and he can do that n times which n is hard coded into program.
But I don't know what will the best way to do this. I am thinking of events but I can not figure it out myself so I really need your help. Here is the code I managed to write but it does not work for reasons I want to know from you!
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int save1 = 0;
int save2 = 0;
System.IO.StreamWriter file1 = new System.IO.StreamWriter("c:\\test1.txt");
System.IO.StreamWriter file2 = new System.IO.StreamWriter("c:\\test2.txt");
private void buttonSave1_Click(object sender, EventArgs e)
{
file1.WriteLine(textBox1.Text);
save1++;
}
private void buttonSave2_Click(object sender, EventArgs e)
{
file2.WriteLine(textBox1.Text);
save2++;
}
private void buttonStart_Click(object sender, EventArgs e)
{
int i = 3;
while (i >= 0)
{
switch (i)
{
case (3):
this.label1.Text = "Saved 0 of 3";
if (save1 == 0 && save2 == 0)
{
this.label1.Text = "Saved 1 of 3";
}
i--;
break;
case (2):
if (save1 == 1 && save2 == 1)
{
this.label1.Text = "Saved 2 of 3";
}
i--;
break;
case (1):
if (save1 == 2 && save2 == 2)
{
this.label1.Text = "Saved 3 of 3";
}
i = 0;
file1.Close();
file2.Close();
break;
}
}
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|