well guys , i want to make a read and write from textfile ...
but the problem is when i click the calendar, and click create now(button 1), it cant work... if i didnt click the calendar, it works well.. so how to solve this problem anyone?
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;
using System.IO;
namespace Timetable
{
public partial class Form1 : Form
{
private string[,] m_strdata = new string[11, 5];//11-events, 5[m_intNumberOfEvents]
private int m_intNumberOfEvents;
//write the text to a file
private StreamWriter m_objOutput = File.AppendText("1.txt");// add line to write instead of rewrite
//initalize StreamReader to read from file
StreamReader objInput;
private void CreateEventList()
{
int intCount;
//stores event info in array m_strdata
ExtractData(mvwDate.SelectionStart);
//remove combobox items
cboevent.Items.Clear();
//add each new event name to comboBox
if (m_intNumberOfEvents > 0)
{
for (intCount = 0; intCount < m_intNumberOfEvents;
intCount++)
{
//extract and display event name
cboevent.Items.Add(m_strdata[intCount, 3]);//1-time,2- price,3- name,4-description
}
//inform user that events are scheduled
cboevent.Text = "- " + Convert.ToString(m_intNumberOfEvents) + " Events found -";
txtDescription.Text = "Pick an event.";
}
//inform user that no events are scheduled
else
{
cboevent.Text = "-No Events-";
txtDescription.Text = "No events today";
}
} // end method CreateEventList
// extracts event data for a specified day from calendar.txt
// and returns number of events during that day
private void ExtractData(DateTime dtmDay)
{
//set to selected date in Mobthalendar control
int intChosenDay = dtmDay.Day;
int intFileDay;//day of event from file
int intLineNumbers;//counts lines to skip
objInput = new StreamReader("calendar.txt");
m_intNumberOfEvents = 0;//set number of events to 0
//read first line before entering loop
string strline = objInput.ReadLine();
//loop though lines in file
while (objInput.Peek() > -1 &&
m_intNumberOfEvents < 11)//11-events
{
intFileDay = Int32.Parse(strline);//extract day
//if event scheduled for specified day,
//store information
if (intFileDay == intChosenDay)
{
m_strdata[m_intNumberOfEvents, 0] = strline;
m_strdata[m_intNumberOfEvents, 1] = objInput.ReadLine();
m_strdata[m_intNumberOfEvents, 2] = objInput.ReadLine();
m_strdata[m_intNumberOfEvents, 3] = objInput.ReadLine();
m_strdata[m_intNumberOfEvents, 4] = objInput.ReadLine();
//m_strdata[m_intNumberOfEvents, 5] = objInput.ReadLine();//sensitive-5 lines
m_intNumberOfEvents++;
}
else
{
//skip to next event in file
for (intLineNumbers = 0; intLineNumbers <= 3; intLineNumbers++)//<=4-5 lines
strline = objInput.ReadLine();
}
//read day of next events in file
strline = objInput.ReadLine();
}//end while
}
public Form1()
{
InitializeComponent();
}
// populates ComboBox with current day's events (if any)
private void Form1_Load(object sender, EventArgs e)
{
CreateEventList();
btncreate.Enabled = false;
txtcost.Enabled = false;
txtday.Enabled = false;
txtname.Enabled = false;
txttime.Enabled = false;
txtwritedescription.Enabled = false;
}
private void mvwDate_DateChanged(object sender, DateRangeEventArgs e)
{
//displayed any events for the specified date in ComboBox
CreateEventList();
}
private void cboevent_SelectedIndexChanged(object sender, EventArgs e)
{
//place time, price and desciption of event in textbox
txtDescription.Text =
m_strdata[cboevent.SelectedIndex, 1];
txtDescription.Text += "\r\n" +"$"+
m_strdata[cboevent.SelectedIndex, 2];
txtDescription.Text += "\r\n" +
m_strdata[cboevent.SelectedIndex, 4];
}
private void clearuserinput()
{
txtcost.Clear();
txtday.Clear();
txtname.Clear();
txttime.Clear();
txtwritedescription.Clear();
}
private void btncreate_Click(object sender, EventArgs e)
{
m_objOutput.WriteLine(txtday.Text);
m_objOutput.WriteLine(txttime.Text);
m_objOutput.WriteLine(txtcost.Text);
m_objOutput.WriteLine(txtname.Text);
m_objOutput.WriteLine(txtwritedescription.Text);
clearuserinput();
objInput.Close();
m_objOutput.Close();
m_objOutput = File.AppendText("1.txt");
objInput = new StreamReader("calendar.txt");
}
private void button1_Click(object sender, EventArgs e)
{
objInput.Close();
m_objOutput.Close();
objInput = new StreamReader("1.txt");
m_objOutput = File.AppendText("calendar.txt");
btncreate.Enabled = true;
txtcost.Enabled = true;
txtday.Enabled = true;
txtname.Enabled = true;
txttime.Enabled = true;
txtwritedescription.Enabled =true;
}
}
}