地图上有两个机场,乌鲁木齐和拉萨,窗体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对应,,上面的代码运行不出来结果,,请高手帮忙看看,,非常感谢,,
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对应,,上面的代码运行不出来结果,,请高手帮忙看看,,非常感谢,,
解决方案 »
- 循环生成的Textbox的TextChanged事件只触发一次
- 委托中视图状态保持问题
- FileStream将不会打开Win32设备(如带磁盘分区 解决方法
- 使用web.config配置的数据库连接,并将页面绑定到DataSet控件的情况下,如何设置自定义DataGrid列
- 循环怎么写????
- C# 用过Rad Controls的朋友进来看看?
- TempMenuItem.Click += new System.EventHandler(NewConnection);
- 请教:为什么正确的字符会变成错误:输入字符串的格式不正确?
- 高手看一下这个正则表达式哪里有错误
- 关于类的属性问题?
- c# Excel 两列交换问题!!!
- 终于写好我的hello world 了。。。 上来献丑下
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;
}
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;
}
}
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;
}
}
}
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" }
};
另外你要点击的两个东西是按钮还是图片?这个必须要交代,按钮的话,在按钮点击事件里处理,图片的话,在鼠标按下事件里判断鼠标所在位置,看是否在预定义的范围内,触发对应的文件选择。