当前位置:编程学习 > C#/ASP.NET >>

richtextbox读取数据的问题

现在我将数据从数据库中读取出来,放在richtextbox中,显示数据我用了LoadFile方法,我发现LoadFile方法可以将数据显示在richtextbox中,这被错,但是该richtextbox只显示了当前加入的数据,并不能一起显示我在加载数据流之前写的字符串。。请高手指点

txtShowMessage.Text = " " + UserHelper.userNickName + "<" + UserHelper.userId + ">    " + DateTime.Now.ToString() + "\r\n" + " ";
txtShowMessage.LoadFile(fileName);

注:  只能显示fileName中的数据,而txtShowMessage.Text = " " + UserHelper.userNickName + "<" + UserHelper.userId + ">    " + DateTime.Now.ToString() + "\r\n" + " ";显示不了了。。 --------------------编程问答-------------------- [MSDN]
RichTextBox.LoadFile Method (String)

Remarks
When loading a file with the LoadFile method, the contents of the file being loaded replace the entire contents of the RichTextBox control. This will cause the values of the Text and Rtf properties to change. You can use this method to load a previously created text or RTF document into the control for manipulation. If you want to save the file, you can use the SaveFile method. 

所以你之前放进去richTextBox的内容都被filename这个指定文件里面的内容覆盖掉了! --------------------编程问答-------------------- 所以后面需要加载文件内容的话可以这样!


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.Diagnostics;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        

        public Form1()
        {
            InitializeComponent();
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
           string s;
            //在StreamReader里面给定文件的全路径
            using (StreamReader sr = new StreamReader("d:\\temp.txt"))
            {
                s = sr.ReadToEnd();
            }
            richTextBox1.Text = "---------------------\r\nSTART:\r\n";
            richTextBox1.Text += s;
        }

        

    }

    
}
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,