Hi every one i need help really quickly. I have a deadline for tonight and need some to help me do a sum in c#.
In the textbox i have to enter values for example "5,5,10,10,7,7,9,9,20,20,3,3,4,4" and it has to take the first entered and the 3rd last and last entered and plus them together
Loading

FroglegRank #1,020 · 1.9K reputationPosted Apr 3, 2011, 1:53 PM EDT
if (buttClick == 4)
{
fileNum++;
string[] allLines = new string[firstClick.Length + secondClick.Length + thirdClick.Length];
for (int i = 0; i < firstClick.Length; i++)
{
allLines[i] = firstClick[i];
}
int oneL = firstClick.Length;
for (int i = 0; i < secondClick.Length; i++)
{
allLines[i + oneL] = secondClick[i];
}
int twoL = secondClick.Length;
twoL= twoL+oneL;
for (int i = 0; i < thirdClick.Length; i++)
{
allLines[i + twoL] = thirdClick[i];
}
System.IO.File.WriteAllLines("henry" + fileNum.ToString() + ".txt", allLines);
buttClick = 1;
button1.Enabled = false;<<<<<<<<<<<<<<<<<<
timer1.Start();<<<<<<<<<<<<<<<<<<<<<<<<<
and in timer event
private void timer1_Tick(object sender, EventArgs e)
{
button1.Enabled = true;
timer1.Stop();
}
}
Karthikeyan AnbarasanRank #71 · 25.5K reputationPosted Apr 2, 2011, 11:20 PM EDT
FroglegRank #1,020 · 1.9K reputationPosted Apr 2, 2011, 11:33 PM EDT
private void button1_Click(object sender, EventArgs e)
{
string[] str = textBox1.Text.Split(',');
int result = 0, first = 0;
for (int i = 0; i < str.Length; i++)
{
if (i==0)
{
first = Convert.ToInt32(str[0]);
}
else if (i == str.Length -3)
{
result = first + Convert.ToInt32(str[str.Length - 3]);
}
}
MessageBox.Show("Answer = " + Convert.ToString(result));
}
Henry Bartosch44 reputationPosted Apr 2, 2011, 11:35 PM EDT
so in the case i stated the "5,5,...."
So it has to do 5+3+4 = 12 and then it should show in time format. 12:00
Henry Bartosch44 reputationPosted Apr 2, 2011, 11:51 PM EDT
But if i enter 5,5,20,20,10,10,1,1
it gives me the amswer 15 and it should be 16
FroglegRank #1,020 · 1.9K reputationPosted Apr 2, 2011, 11:54 PM EDT
private void button1_Click(object sender, EventArgs e)
{
string[] str = textBox1.Text.Split(',');
int result = 0, first = 0,second = 0,third = 0;
for (int i = 0; i < str.Length; i++)
{
if (i==0)
{
first = Convert.ToInt32(str[0]);
}
else if (i == str.Length -3)
{
second = Convert.ToInt32(str[str.Length - 3]);
}
else if (i == str.Length -1)
{
third = Convert.ToInt32(str[str.Length -1]);
result = first + second + third;
}
}
MessageBox.Show("Answer = " + Convert.ToString(result));
Henry Bartosch44 reputationPosted Apr 3, 2011, 12:03 AM EDT
and now if there is anything over 24 it should devide it by 24
Henry Bartosch44 reputationPosted Apr 3, 2011, 12:06 AM EDT
Henry Bartosch44 reputationPosted Apr 3, 2011, 12:07 AM EDT
Henry Bartosch44 reputationPosted Apr 3, 2011, 12:25 AM EDT
the last 2 entered should now be subtracted from each other : 4-4
FroglegRank #1,020 · 1.9K reputationPosted Apr 3, 2011, 12:48 AM EDT
Summarize what you want in one question please
Henry Bartosch44 reputationPosted Apr 3, 2011, 12:54 AM EDT
Thanks
FroglegRank #1,020 · 1.9K reputationPosted Apr 3, 2011, 1:13 AM EDT
1: Get a time from the textbox - will have to be less than 24hrs otherwise it will be tomorrow
2: From day of entry (today) we need the date 30 days back and 30 days forward
3: Save all to text file
I have limited time tonight so hopefully someone else will join in to help 7.15pm here
Henry Bartosch44 reputationPosted Apr 3, 2011, 1:22 AM EDT
but with todays date and time the time should be the same for time for 30 days forward and backward : example todays date: Sunday 16:00(if it goes backward its gona be saturday 16:00, vryday 16:00...etc. and forward monday 16:00, tuesday 16:00...etc) and then save all to a text file.
FroglegRank #1,020 · 1.9K reputationPosted Apr 3, 2011, 1:33 AM EDT
DateTime d = DateTime.Today.AddDays(-30);// use minus to subtract (30) to add
to get string value use
d.ToString();
To write a text file use
using System.IO; // above namespace
using (StreamWriter sw = new StreamWriter(fileName))
{
sw.WriteLine(d.ToString());// for the days prior and forward use say DateTime d2 = DateTime.Today.AddDays(30);
}
Gotta go Bye
Henry Bartosch44 reputationPosted Apr 3, 2011, 1:41 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 3, 2011, 2:07 AM EDT
Building on Frogleg's code, this should do it:
Henry Bartosch44 reputationPosted Apr 3, 2011, 2:13 AM EDT
But it doesnt save to the text file. Searching for the text file but cant find it.
Should i create a path?
Henry Bartosch44 reputationPosted Apr 3, 2011, 2:17 AM EDT
Sunday 04:00
Tuesday 04:00
Friday 04:00
VulpesRank #55 · 29.2K reputationPosted Apr 3, 2011, 2:19 AM EDT
To find it quickly, I'd use a full path. The following should do as a temporary measure:
System.IO.File.WriteAllLines(@"c:\henry.txt", lines);
Henry Bartosch44 reputationPosted Apr 3, 2011, 2:21 AM EDT
Henry Bartosch44 reputationPosted Apr 3, 2011, 2:25 AM EDT
VulpesRank #55 · 29.2K reputationPosted Apr 3, 2011, 2:44 AM EDT
private int fileNum = 0; // need this form level variable to count the files
fileNum++;
Henry Bartosch44 reputationPosted Apr 3, 2011, 3:37 AM EDT
but now i have the problem of the text file. it keeps on saving to the same text file. Every time i say save a new text file should be created, not replace the previous one
VulpesRank #55 · 29.2K reputationPosted Apr 3, 2011, 4:16 AM EDT
fileNum++;
which will be incremented each time the button is pressed and thereby generate a new file name to save the calculations to.
Henry Bartosch44 reputationPosted Apr 3, 2011, 4:38 AM EDT
Henry Bartosch44 reputationPosted Apr 3, 2011, 5:13 AM EDT
I also want tit to do
first-first;
first + first
third +third;
second;
third;
first;
second - third;
second + second + third+third
and show all the diverent times with the dates in the same file
Henry Bartosch44 reputationPosted Apr 3, 2011, 5:13 AM EDT
I also want tit to do
first-first;
first + first
third +third;
second;
third;
first;
second - third;
second + second + third+third
and show all the diverent times with the dates in the same text file
VulpesRank #55 · 29.2K reputationPosted Apr 3, 2011, 5:46 AM EDT
However, if the results are all to be saved to the same file, then you'll need to use File.AppendAllLines rather than File.WriteAllLines.
Henry Bartosch44 reputationPosted Apr 3, 2011, 12:37 PM EDT
Please.
fileNum++;
FroglegRank #1,020 · 1.9K reputationPosted Apr 3, 2011, 12:43 PM EDT
Henry Bartosch44 reputationPosted Apr 3, 2011, 12:51 PM EDT
third - third;
second - third;
and it should give an answer and disply it in the text file
FroglegRank #1,020 · 1.9K reputationPosted Apr 3, 2011, 1:01 PM EDT
Henry Bartosch44 reputationPosted Apr 3, 2011, 1:08 PM EDT
f (i==0)
{
first = Convert.ToInt32(str[0]);
}
else if (i == str.Length -3)
{
second = Convert.ToInt32(str[str.Length - 3]);
}
else if (i == str.Length -1)
{
third = Convert.ToInt32(str[str.Length -1]);
result = first + second + third;
}
with this it gave me an answer. but now with the same button click event i want
a result = second - third
and a result = third - third
and a result = third + third
so if you can help me with an example; coz all of this should happen on the same button click event and save to the text file.
FroglegRank #1,020 · 1.9K reputationPosted Apr 3, 2011, 1:24 PM EDT
int fileNum = 0;
int buttClick = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string[] str = textBox1.Text.Split(',');
int result = 0, first = 0,second = 0,third = 0;
switch (buttClick)
{
case 0:
for (int i = 0; i < str.Length; i++)
{
if (i==0)
{
first = Convert.ToInt32(str[0]);
}
else if (i == str.Length -3)
{
second = Convert.ToInt32(str[str.Length - 3]);
}
else if (i == str.Length -1)
{
third = Convert.ToInt32(str[str.Length -1]);
result = first + second + third;
}
}
buttClick = 1;
break;
case 1:
for (int i = 0; i < str.Length; i++)
{
if (i == str.Length - 3)
{
second = Convert.ToInt32(str[str.Length - 3]);
}
else if (i == str.Length - 1)
{
third = Convert.ToInt32(str[str.Length - 1]);
result = second - third;
}
}
buttClick = 2;
break;
case 2:
for (int i = 0; i < str.Length; i++)
{
if (i == str.Length - 1)
{
third = Convert.ToInt32(str[str.Length - 1]);
result = third + third;
}
}
buttClick = 3;
break;
}
if (result >= 24) result %= 24;
DateTime dt = DateTime.Today.AddHours(result);
string format = "dddd hh:mm";
string[] lines = new string[61];
for (int i = 0; i < 61; i++)
{
lines[i] = dt.AddDays(i - 30).ToString(format);
}
if (buttClick == 3)
{
fileNum++;
System.IO.File.WriteAllLines("henry" + fileNum.ToString() + ".txt", lines);
buttClick = 0;
}
}
No point in third -third
Henry Bartosch44 reputationPosted Apr 3, 2011, 1:41 PM EDT
FroglegRank #1,020 · 1.9K reputationPosted Apr 3, 2011, 1:48 PM EDT
int fileNum = 0;
int buttClick = 1;
string[] firstClick, secondClick, thirdClick;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string[] str = textBox1.Text.Split(',');
int result = 0, first = 0,second = 0,third = 0;
switch (buttClick)
{
case 1:
for (int i = 0; i < str.Length; i++)
{
if (i==0)
{
first = Convert.ToInt32(str[0]);
}
else if (i == str.Length -3)
{
second = Convert.ToInt32(str[str.Length - 3]);
}
else if (i == str.Length -1)
{
third = Convert.ToInt32(str[str.Length -1]);
result = first + second + third;
}
}
break;
case 2:
for (int i = 0; i < str.Length; i++)
{
if (i == str.Length - 3)
{
second = Convert.ToInt32(str[str.Length - 3]);
}
else if (i == str.Length - 1)
{
third = Convert.ToInt32(str[str.Length - 1]);
result = second - third;
}
}
break;
case 3:
for (int i = 0; i < str.Length; i++)
{
if (i == str.Length - 1)
{
third = Convert.ToInt32(str[str.Length - 1]);
result = third + third;
}
}
break;
}
if (result >= 24) result %= 24;
DateTime dt = DateTime.Today.AddHours(result);
string format = "dddd hh:mm";
string[] lines = new string[61];
for (int i = 0; i < 61; i++)
{
lines[i] = dt.AddDays(i - 30).ToString(format);
}
if (buttClick == 1)
{
firstClick = lines;
buttClick = 2;
}
else if (buttClick == 2)
{
secondClick = lines;
buttClick = 3;
}
else if (buttClick == 3)
{
thirdClick = lines;
buttClick = 4;
}
if (buttClick == 4)
{
fileNum++;
string[] allLines = new string[firstClick.Length + secondClick.Length + thirdClick.Length];
for (int i = 0; i < firstClick.Length; i++)
{
allLines[i] = firstClick[i];
}
int oneL = firstClick.Length;
for (int i = 0; i < secondClick.Length; i++)
{
allLines[i + oneL] = secondClick[i];
}
int twoL = secondClick.Length;
twoL= twoL+oneL;
for (int i = 0; i < thirdClick.Length; i++)
{
allLines[i + twoL] = thirdClick[i];
}
System.IO.File.WriteAllLines("henry" + fileNum.ToString() + ".txt", allLines);
buttClick = 1;
}
}