2015-04-29 37 views
-2

所以我用這個方法將數據保存到一個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文件到面板上的所有信息。

+1

那麼,你嘗試過這樣做呢?我無法在任何地方看到*閱讀*文件。 –

+0

這就是我所要求的。在哪裏我必須閱讀代碼中的文件?然後如何在面板上顯示它。 –

回答

1
// replace filepath below with filepath of your text file. 
string text = System.IO.File.ReadAllText(@"C:\Users\Public\TestFolder\WriteText.txt"); 

一旦你有了text變量填充,你可以做任何你想做的事情。像將其添加到您的面板。

// something like... 
panel.Text = text; 

您還可以閱讀文本文件的每一行到一個數組,做任何你與數組一樣:

string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt"); 
+0

你能告訴我,我在哪裏把這段代碼放在我的代碼中? –

+3

@ChristosSpanos你沒有寫過你在上面發佈的代碼嗎?難道你不能從邏輯上推斷出讀取文件的代碼應該放在哪裏?我已經給你足夠的時間來繼續,這取決於你如何處理這些信息。 – Kevin

+0

你不覺得你必須跟我說更好一點嗎?這是你跟任何問你問題的人談話的方式嗎?你不覺得我是編程新手嗎?談一個更好的粗魯的男人! –