C# 如何从txt文件加载信息以显示在面板上?
因此,我使用此方法将数据保存到txt文件中:C# 如何从txt文件加载信息以显示在面板上?,c#,winforms,C#,Winforms,因此,我使用此方法将数据保存到txt文件中: private void button1_Click(object sender, EventArgs e) { using (StreamWriter objWriter = new StreamWriter("test1.txt")) { objWriter.Write(textBox1.Text); objWriter.Write(textBox2.Text
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的形式出现。这是我的主要表格中的面板:
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;
}
}
}
}
private void panel dailyview\u Paint(对象发送器,PaintEventArgs e)
{
int paintWidth=panelDailyView.ClientRectangle.Size.Width-vScrollBar.Width;
int paintHeight=panelDailyView.ClientRectangle.Size.Height;
int displayedRowCount=油漆高度/面板行高度;
int PALLETOPROW;
int nextRow;
int apptStartRow;
国际应用长度;
字符串显示时间;
字体=新字体(“Arial”,10);
笔刷drawBrush=新的SolidBrush(颜色为深蓝色);
笔刷指定笔刷=新的SolidBrush(颜色为浅蓝色);
图形g=e.图形;
//填充面板的背景
g、 FillRectangle(新SolidBrush(Color.Linen),0,0,paintWidth,paintHeight);
panelTopRow=vScrollBar.Value;
如果(\u SelectedRow>=panelTopRow)&&
_已选择的低漆面高度)
{
APPTDISPLEGTH=油漆高度-apptDispStart;
}
矩形apptRectangle=新矩形(APPTOFSET,
apptDispStart,
油漆宽度-(ApptOffset*2),
附件(第四节);
//画一块浅蓝色
g、 圆角矩形(指定笔刷,
apptRectangle);
//在它周围画一条黑线
g、 DrawRectangle(钢笔、黑色、apptRectangle);
if(Utility.ConvertTimeToRow(appointment.Start)>=panelTopRow)
{
//如果显示约会的顶行,
//写下主题和位置。暂时
//减少图形对象的剪辑区域以确保
//文本不会超出矩形
区域oldClip=g.Clip;
g、 Clip=新区域(apptRectangle);
g、 抽绳(appointment.DisplayableDescription,
字体,
画笔,
APPTOFSET+6,
apptDispStart+4);
g、 Clip=oldClip;
}
}
}
}
我要做的就是将保存在.txt文件中的所有信息加载到面板中
// replace filepath below with filepath of your text file.
string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt");
一旦填充了文本
变量,就可以对其执行任何操作。喜欢将它添加到面板中
// something like...
panel.Text = text;
您还可以将文本文件的每一行读入数组,并对数组执行任何操作:
string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt");
那么,你在哪里尝试过这样做?我看不到你看文件的地方。这也是我要问的。在代码中,我必须在哪里读取文件?然后如何在面板上显示它。你能告诉我这段代码在我的代码中的确切位置吗?@ChristosSpanos你不是写了你在问题中发布的代码吗?您不能从逻辑上推断读取文件的代码应该放在哪里吗?我已经给了你足够的时间让你继续下去,这取决于你如何处理这些信息。@ChristosSpanos如果你认为我写的是粗鲁的话,我很抱歉。然而,我的观点仍然是:你写了代码,你应该知道在哪里添加代码来读取文本文件。@ChristosSpanos:我认为他实际上很有礼貌,因为你显然不知道自己在做什么。这并不意味着是一种侮辱,除非侮辱你有助于你决定学习足够的知识,从而停止接受这种侮辱。对于任何了解您发布的代码流程的人来说,很明显,在需要显示信息之前,您需要获取信息。问问自己:如果您要显示常量文本“Christos”,您会怎么做?如果你不能理解,那么这已经超过了你目前的理解水平。你需要停下来,读一些关于C#和一般编程的入门书。