| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1514 人关注过本帖
标题:有关记事本的问题?
只看楼主 加入收藏
guoQT123
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-9-16
收藏
 问题点数:0 回复次数:11 
有关记事本的问题?
我编写了一个简单的记事本,只能打开一段字符。请教高手为什么?谢谢!
搜索更多相关主题的帖子: 记事本 
2008-10-19 22:52
ecjob
Rank: 3Rank: 3
等 级:论坛游侠
威 望:8
帖 子:321
专家分:110
注 册:2006-10-15
收藏
得分:0 
你不会是用textbox 来加载字符吧?

86年出生,广东求 女人/项目/工作 ,见广告即有效.论坛PM
2008-10-19 23:06
guoQT123
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-9-16
收藏
得分:0 
我是用richTextBox。
2008-10-19 23:14
乖老鼠
Rank: 5Rank: 5
来 自:四川省
等 级:职业侠客
威 望:2
帖 子:434
专家分:394
注 册:2008-9-8
收藏
得分:0 
你加载语句怎么写的?

转眼就从编程菜鸟混成了半灌水
2008-10-19 23:38
guoQT123
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-9-16
收藏
得分:0 
private void MenuItemOpen_Click(object sender, EventArgs e)
        {
            if(needtosave==true)
            {
                DialogResult result=MessageBox.Show("文本内容已经改变,是否需要保存?","保存文件",MessageBoxButtons.YesNoCancel,MessageBoxIcon.Question);
                if(result==DialogResult.Yes)
                {
                    MenuItemSave_Click(sender,e);
                    needtosave=false;
                    richTextBoxText.Clear();
                }
                else if (result == DialogResult.Cancel)
                {
                    return;
                }
                    else
                {
                    string file=GetOpenFile();
                    if(file==null)
                    {
                        return;
                    }
                    else
                    {
                        currenfilename=file;
                        OpenFile();
                        needtosave=false;
                    }
                }
            }
                    
            else
                {
                    richTextBoxText.Clear();
                    string file = GetOpenFile();
                    if (file == null)
                    {
                        return;
                    }
                    else
                    {
                        currenfilename = file;
                        OpenFile();
                        needtosave = false;
                    }
                }
                  
            
        }
        private string GetOpenFile()
        {
            dlg = new OpenFileDialog();
            dlg.Title = "打开文本文件";
            dlg.ValidateNames = true;
            dlg.CheckFileExists = true;
            dlg.CheckPathExists = true;
            dlg.ShowHelp = true;
            dlg.Multiselect = true;
            dlg.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
           // string dir = Environment.GetFolderPath(Environment.SpecialFolder.Templates);
           // dlg.InitialDirectory = dir;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                return dlg.FileName;
            }
            else
            {
                return  null;
            }
           
         
        }
        private void OpenFile()
        {
            try
            {
                FileStream fs = new FileStream(currenfilename, FileMode.Open, FileAccess.Read);
                if (fs.CanRead)
                {
                    FileInfo f = new FileInfo(currenfilename);
                    StreamReader sr = new StreamReader(fs, Encoding.Default);
                    string strline = sr.ReadLine();
                    if (strline != null)
                    {
                        //richTextBox1.Text += strline + "\n";
                        richTextBoxText.Text = strline + "\n";
                        strline = null;
                    }
                    sr.Close();
                    this.Text = f.Name;
                }

            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
2008-10-20 22:08
guoQT123
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-9-16
收藏
得分:0 
今天又发现:当我编辑了一些汉字的时候,保存后再打开它时,就变成了乱码!请高手帮忙!感激不禁!谢谢~~~~`
2008-10-20 22:12
wang4868656
Rank: 1
来 自:沧州
等 级:新手上路
帖 子:14
专家分:0
注 册:2008-10-20
收藏
得分:0 
我是这么写的,没有问题啊!
 private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream mystream = null;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "D:\\My Documents";
            openFileDialog1.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*";
            openFileDialog1.FilterIndex = 1;   //1为打开对话框默认为txt;2默认是所有文件。
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog()==DialogResult.OK)
            {
                try
                {
                    if ((mystream = openFileDialog1.OpenFile()) != null)
                    {
                        using (mystream)
                        {
                            richTextBox1.LoadFile(openFileDialog1.FileName,RichTextBoxStreamType.PlainText);
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error:could not read file from disk.Original error:" + ex.Message);
                }
            }
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {//还很不好,可以往里面加入一些try..catch
            saveFileDialog1.Filter = "文本文档(*.txt)|*.txt|所有文件|*.*";

            if (saveFileDialog1.ShowDialog() ==DialogResult.OK
                && saveFileDialog1.FileName.Length > 0)
            {

                richTextBox1.SaveFile(saveFileDialog1.FileName,
                    RichTextBoxStreamType.PlainText);
            }

        }
我是菜鸟啊,代码可能很不好看,见谅!
2008-10-20 22:19
乖老鼠
Rank: 5Rank: 5
来 自:四川省
等 级:职业侠客
威 望:2
帖 子:434
专家分:394
注 册:2008-9-8
收藏
得分:0 
乱码是因为你没有设置按指定的编码格式读取。

LZ试试用下面的代码读取文件:

richTextBox1.Text = File.ReadAllText(this.openFileDialog1.FileName, Encoding.GetEncoding("gb2312"));

这个应该可以的哦,而且代码没你的复杂

转眼就从编程菜鸟混成了半灌水
2008-10-21 10:35
guoQT123
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-9-16
收藏
得分:0 
richTextBox1.Text = File.ReadAllText(this.openFileDialog1.FileName, Encoding.GetEncoding("gb2312"));
乖老鼠,还是不行啊~~~~~~~~~
2008-10-21 15:07
guoQT123
Rank: 1
等 级:新手上路
帖 子:32
专家分:0
注 册:2008-9-16
收藏
得分:0 
TextBoxEdit.Text = File.ReadAllText(this.dlgOpenFile.FileName,Encoding.Default);//Default是获取系统的当前ANSI代码页的编码。
这样就不会出现乱码,也不会只打开一段字符。谢谢乖老鼠的提示!!!感激不禁!
2008-10-21 15:25
快速回复:有关记事本的问题?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025041 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved