所以我用這個方法將數據保存到一個txt文件:如何從txt文件加載信息以在面板上顯示?
private void button1_Click(object sender, EventArgs e)
{
using (StreamWriter objWriter = new StreamWriter("test1.txt"))
{
objWriter.Write(textBox1.Text);
objWriter.Write(textBox2.Text);
objWriter.Write(comboBox1.Text);
objWriter.Write(comboBox2.Text);
MessageBox.Show("Details have been saved");
}
}
所有這些文本框和組合框在一個叫作NewAppointment形式。這是我的MainForm面板:
private void panelDailyView_Paint(object sender, PaintEventArgs e)
{
int paintWidth = panelDailyView.ClientRectangle.Size.Width - vScrollBar.Width;
int paintHeight = panelDailyView.ClientRectangle.Size.Height;
int displayedRowCount = paintHeight/PanelRowHeight;
int panelTopRow;
int nextRow;
int apptStartRow;
int apptLength;
string dispTime;
Font font = new Font("Arial", 10);
Brush drawBrush = new SolidBrush(Color.DarkBlue);
Brush appointmentBrush = new SolidBrush(Color.LightBlue);
Graphics g = e.Graphics;
// Fill the background of the panel
g.FillRectangle(new SolidBrush(Color.Linen), 0, 0, paintWidth, paintHeight);
panelTopRow = vScrollBar.Value;
if (_SelectedRow >= panelTopRow &&
_SelectedRow <= panelTopRow + displayedRowCount)
{
// If the selected time is displayed, mark it
g.FillRectangle(new SolidBrush(Color.DarkKhaki),
0,
(_SelectedRow - panelTopRow) * PanelRowHeight,
paintWidth,
PanelRowHeight);
}
// Display the times at the start of the rows and
// the lines separating the rows
nextRow = panelTopRow;
for (int i = 0; i <= displayedRowCount; i++)
{
dispTime = (nextRow/2).ToString("0#") + (nextRow % 2 == 0 ? ":00" : ":30");
nextRow++;
g.DrawString(dispTime, font, drawBrush, 2, (i * PanelRowHeight + 4));
g.DrawLine(Pens.DarkBlue, 0, i * PanelRowHeight, paintWidth, i * PanelRowHeight);
}
// Now fill in the appointments
foreach (IAppointment appointment in _TodaysAppointments)
{
apptStartRow = Utility.ConvertTimeToRow(appointment.Start);
apptLength = Utility.ConvertLengthToRows(appointment.Length);
// See if the appointment is inside the part of the day displayed on the panel
if (((apptStartRow >= panelTopRow) &&
(apptStartRow <= panelTopRow + displayedRowCount)) ||
(apptStartRow + apptLength > panelTopRow))
{
// Calculate the area of the panel occupied by
// the appointment
if (apptStartRow < panelTopRow)
{
apptLength = apptLength - (panelTopRow - apptStartRow);
apptStartRow = panelTopRow;
}
int apptDispStart = (apptStartRow - panelTopRow) * PanelRowHeight;
int apptDispLength = apptLength * PanelRowHeight;
if (apptDispStart + apptDispLength > paintHeight)
{
apptDispLength = paintHeight - apptDispStart;
}
Rectangle apptRectangle = new Rectangle(ApptOffset,
apptDispStart,
paintWidth - (ApptOffset * 2),
apptDispLength);
// Draw the block of light blue
g.FillRectangle(appointmentBrush,
apptRectangle);
// Draw the black line around it
g.DrawRectangle(Pens.Black, apptRectangle);
if (Utility.ConvertTimeToRow(appointment.Start) >= panelTopRow)
{
// If the top line of the appointment is displayed,
// write out the subject and location. Temporarily
// reduce the clip area for the graphics object to ensure
// that the text does not extend beyond the rectangle
Region oldClip = g.Clip;
g.Clip = new Region(apptRectangle);
g.DrawString(appointment.DisplayableDescription,
font,
drawBrush,
ApptOffset + 6,
apptDispStart + 4);
g.Clip = oldClip;
}
}
}
}
所有我想要做的是加載保存在.txt文件到面板上的所有信息。
那麼,你嘗試過這樣做呢?我無法在任何地方看到*閱讀*文件。 –
這就是我所要求的。在哪裏我必須閱讀代碼中的文件?然後如何在面板上顯示它。 –