如何从txt文件加载信息以显示在面板上?

时间:2015-04-29 18:47:14

标签: 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);
            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文件中保存的所有信息加载到面板中。

0 个答案:

没有答案