地图上有两个机场,乌鲁木齐和拉萨,窗体FormFind中有一个RichTextBox,Name属性为txtReadTxtStr,,E盘中有两个文件,1.txt和2.txt。我想实现的是点击乌鲁木齐时RichTextBox中显示1.txt中的内容,点击拉萨时显示2.txt中的内容,
     string[] cities = new string[] { "乌鲁木齐","拉萨"};
     string[] files = new string[] { @"e:\1.txt", @"e:\2.txt" };
     foreach (var city in cities)
         {                                               
            foreach (var file in files)
                {                                                                                                          
                    using (StreamReader sr = new StreamReader(file, System.Text.Encoding.Default))
                        {
                             string TextStr;
                             TextStr = sr.ReadToEnd().ToString();
                             sr.Close();
                             FormFind.txt += TextStr;
                         }                 
                }
          }
txt是我在FormFind窗体中定义的变量, public  static string txt;this.txtReadTxtStr.Text=txt;也就是说怎样 让乌鲁木齐和1.txt对应,拉萨和2.txt对应,,上面的代码运行不出来结果,,请高手帮忙看看,,非常感谢,,

解决方案 »

  1.   

    地图上有两个机场,乌鲁木齐和拉萨,窗体FormFind中有一个RichTextBox,Name属性为txtReadTxtStr,,E盘中有两个文件,1.txt和2.txt。我想实现的是点击乌鲁木齐时RichTextBox中显示1.txt中的内容,点击拉萨时显示2.txt中的内容,
      string[] cities = new string[] { "乌鲁木齐","拉萨"};
      string[] files = new string[] { @"e:\1.txt", @"e:\2.txt" };
      for (var i=0; i<cities.Length; i++)
      {
        var city = cities[i];   
        var file= files[i];
        using (StreamReader sr = new StreamReader(file, System.Text.Encoding.Default))
        {
            .....
        }
        break;   
      }
      
      

  2.   

    晕,然你传染了,犯了跟你一样的错误。 string[] cities = new string[] { "乌鲁木齐","拉萨"};
      string[] files = new string[] { @"e:\1.txt", @"e:\2.txt" };
      for (var i=0; i<cities.Length; i++)
      {
        var city = cities[i];   
        if(city == txt)
        {
          var file= files[i];
          using (StreamReader sr = new StreamReader(file, System.Text.Encoding.Default))
          {
            .....
          }
          break;
        }   
      }
      

  3.   

    请问一下2楼,if(city == txt),当前上下文中不存在txt啊,他是在FormFind中定义的,但是我把它改成if(city == FormFind.txt),点击乌鲁木齐和拉萨时还是不能读取,而你第一次发的代码运行后,点击乌鲁木齐时读取1.txt的内容,点击拉萨时RichTextBox中显示1.txt中的内容显示了两遍,我C#才学,好多不懂,,麻烦了,,
      

  4.   

    http://topic.csdn.net/u/20120825/21/902af21b-f2aa-416f-84ab-10839118eafc.html这个人和你是一个人?晕!最近论坛总是问几拨相似的问题
      

  5.   

    txt是随便写的变量,没想到跟你的重复了。
      

  6.   

    我们是今年参加ESRI公司开发大赛的,刚大二,有关C#的,数据库的好多都不会,,并不是你们说的那样。。
      

  7.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;namespace WindowsFormsApplication2
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }
            private Test test;
            private void button_Click(object sender, EventArgs e)
            {
                Button btn = (Button) sender;
                txtReadTxtStr.Text = test.GetString(btn.Text);
            }        private void Form3_Load(object sender, EventArgs e)
            {
                var list = new List<City>
                               {new City {Name = "乌鲁木齐", File = @"E:\1.txt"}, new City {Name = "拉萨", File = @"E:\2.txt"}};
                test = new Test(list);
            }
        }
        public class City
        {
            public string Name { get; set; }
            public string File { get; set; }
        }
        public class Test
        {
            private IList<City> _citys;
            public Test(IList<City> citys)
            {
                _citys = citys;
            }
            /// <summary>
            /// 根据名称获取文件内容
            /// </summary>
            /// <param name="name"></param>
            /// <returns></returns>
            public string GetString(string name)
            {
                var ent = _citys.First(u => u.Name == name);
                return ReadFileContent(ent.File);
            }
            /// <summary>
            /// 读取文件内容
            /// </summary>
            /// <param name="filepath">文件路径</param>
            /// <returns></returns>
            private string ReadFileContent(string filepath)
            {
                string result;
                using (var sr = new StreamReader(filepath, Encoding.Default))
                {
                    result = sr.ReadToEnd();
                }
                return result;
            }
        }
    }
      

  8.   

    建议开始的定义修改下,那样好处理。
      string[] cities = new string[] { "乌鲁木齐","拉萨"};
      string[] files = new string[] { @"e:\1.txt", @"e:\2.txt" };
    改为:
            Dictionary<string, string> citymap = new Dictionary<string, string> 
            {
                { "乌鲁木齐", @"e:\1.txt" }, 
                { "拉萨", @"e:\2.txt" }
            };
    另外你要点击的两个东西是按钮还是图片?这个必须要交代,按钮的话,在按钮点击事件里处理,图片的话,在鼠标按下事件里判断鼠标所在位置,看是否在预定义的范围内,触发对应的文件选择。
      

  9.   

    加个计数变量就可以了,就好像有次在百度回答问题一样,起先写好了,FOR形式却又要求用DO写法,那就只好加个计数变量一样可以.