利用线程把文本文件填充到richTextBox;防止导入大文本文件窗口假死现象

 private void btnDr_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = ""; //richTextBox控件
            labelPercent.Visible = true; //显示当前行数的label
            string path = "";
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "文件文件(*.txt)|*.txt";
            if (openFileDialog1.ShowDialog() != DialogResult.OK) return;
            path = openFileDialog1.FileName;
            new Thread((ThreadStart)delegate
            {
                StreamReader sr = new StreamReader(openFileDialog1.FileName, Encoding.GetEncoding("gb2312"));
                int line = 0;
                int lineCount = line + 1;
                string strCount = "";
                string strline = sr.ReadLine();
                while (strline != "" && strline != null && !strline.Equals(""))
                {
                    this.Invoke((EventHandler)delegate { this.labelPercent.Text = "已经读取 " + lineCount.ToString() + " 行"; });
                    this.richTextBox1.Text = this.richTextBox1.Text + strline + "\r\n";
                    strline = sr.ReadLine();
                    line++;
                    lineCount++;
                }

                sr.Close();

            }).Start();
        }

 

转载于:https://www.cnblogs.com/xuxiaoshuan/p/3586247.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常感谢您的提问。关于您的问题,可以通过以下代码实现: ```c# public partial class Form1 : Form { [DllImport("user32.dll")] public static extern int SendMessage(IntPtr hWnd, uint Msg, int wParam, int lParam); [DllImport("user32.dll")] static extern IntPtr LoadImage(IntPtr hInst, string lpszName, uint uType, int cxDesired, int cyDesired, uint fuLoad); private const uint WM_SETREDRAW = 0x000B; private const uint OIC_DOCUMENT = 0x00000003; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //打开文件对话框 OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.Filter = "NC files (*.nc)|*.nc"; openFileDialog1.Title = "Select a NC File"; if (openFileDialog1.ShowDialog() == DialogResult.OK) { richTextBox1.Clear(); IntPtr hIcon = LoadImage(IntPtr.Zero, "shell32.dll", OIC_DOCUMENT, 16, 16, 0x00000000); richTextBox1.InsertImage(hIcon, new FileInfo(openFileDialog1.FileName).Name + "\n"); StreamReader sr = new StreamReader(openFileDialog1.FileName); richTextBox1.AppendText(sr.ReadToEnd()); sr.Close(); } } private void richTextBox1_VScroll(object sender, EventArgs e) { SendMessage(richTextBox1.Handle, WM_SETREDRAW, 0, 0); richTextBox1.Invalidate(); SendMessage(richTextBox1.Handle, WM_SETREDRAW, 1, 0); } } public static class RichTextBoxExtensions { public static void InsertImage(this RichTextBox box, Image image, string caption) { Clipboard.SetImage(image); if (box.CanPaste(DataFormats.GetFormat(DataFormats.Bitmap))) { box.SelectionStart = 0; box.Paste(); box.AppendText(caption + "\n"); } } } ``` 在这个例子中,利用SendMessage函数和WM_SETREDRAW消息实现了快速滚动和加速地绘制内容。同时,用InsertImage函数将NC文件的图标插入到RichTextBox中。 希望能够帮到您,感谢您的提问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值