| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 382 人关注过本帖
标题:c#代码—求解释
只看楼主 加入收藏
罗森林
Rank: 1
来 自:广东梅州
等 级:新手上路
帖 子:12
专家分:5
注 册:2012-10-15
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
c#代码—求解释
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using

namespace HelloWorld
{
    public partial class Form1 : Form
    {
   
        private string FileName="a.txt";
        private string FileContent;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FileInfo info = new FileInfo(FileName);
            if (!info.Exists)
            {
                MessageBox.Show("File Does't Exist!Will Create!");
            }
            FileStream fs=info.Open(FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite);
            StreamReader sr=new StreamReader(fs);
            string strContent=sr.ReadLine();
            sr.Close();
            fs.Close();
            this.textContent.Text=strContent;
            this.FileContent=strContent;

        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ArrayList listContent = new ArrayList();
                //将fileContent按照空格或者制表符分割成字符串组
                string[] strContent = FileContent.Split(null);
                foreach (string str in strContent)
                {
                    if (str != "")
                    {
                        listContent.Add(str);
                    }
                }
                string strDisplay = "";
                foreach (object obj in listContent)
                {
                    string str = obj as string;
                    if (str != null)
                    {
                        strDisplay += str + System.Environment.NewLine;
                    }
                }
                MessageBox.Show(strDisplay);
            }
            //右键
            else
            {
                Hashtable ht =new Hashtable();
                //将FileContent按照空格或者制表符分割成字符串数组
                string[] strContent=FileContent.Split(null);
                int number=0;
                foreach(string str in strContent)
                {
                    if(str!="")
                    {
                        ht.Add("Number" + number.ToString(), str);
                    }
                    number++;
                }
                string strDisplay="";
                IDictionaryEnumerator enumer=ht.GetEnumerator();
                while(enumer.MoveNext()){
                    strDisplay+=enumer.Key.ToString()+":"+enumer.Value.ToString()+System.Environment.NewLine;
                }
                MessageBox.Show(strDisplay);
            }         

        }

        private void btnSaveFile_Click(object sender, EventArgs e)
        {
            FileInfo info = new FileInfo(FileName);
            if (!info.Exists)
            {
                MessageBox.Show("   File Doesn't Exist! Will Create!");
            }
            FileStream fs = info.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(FileContent);
            sw.Close();
            fs.Close();
        }

        private void btnLoadFile_Click(object sender, EventArgs e)
        {
            FileInfo info = new FileInfo(FileName);
            if (!info.Exists)
            {
                MessageBox.Show("   File Doesn't Exist! Will Create!");
            }
            FileStream fs = info.Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
            StreamReader sr = new StreamReader(fs);
            string strContent = sr.ReadLine();
            sr.Close();
            fs.Close();
            MessageBox.Show("File Content is:" + strContent);
        }

        private void btnException_Click(object sender, EventArgs e)
        {
            throw new Exception("I throw an Exceotion,but i don't konw what is to do ,hehe");   
        }

        private void textContent_TextChanged(object sender, EventArgs e)
        {
            this.FileContent = this.textContent.Text;
        }

        
    }
}
搜索更多相关主题的帖子: void public private 
2012-10-15 22:39
ZWYyj
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:9
帖 子:124
专家分:587
注 册:2012-8-10
收藏
得分:7 
是哪里不清楚?
2012-10-16 17:32
变异病毒
Rank: 1
等 级:新手上路
帖 子:1
专家分:7
注 册:2012-10-17
收藏
得分:7 
没看懂
2012-10-17 16:03
快速回复:c#代码—求解释
数据加载中...
 
   



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

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