已经实现了把指定路径的所有文件名显示到下拉列表里面,现在的问题是,如何通过下拉列表选择的文件名,把对应的文本内容显示到文本框,比如说:下拉列表读取到了1,2,3,4四个文本文件名,我选择文件1的时候,需要把文本1的内容显示到另一个文本框
3条回答 默认 最新
- CSDN专家-showbo 2021-07-27 09:18关注
combobox添加SelectedIndexChanged事件,然后用IO读取文件内容设置文本框的内容。有帮助麻烦点个采纳【本回答右上角】,谢谢~~
using System; using System.IO; using System.Linq; using System.Windows.Forms; using System.Text; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { var textPath = @"D:\文件\C#\windowform\WindowsFormsApp1\bin\Debug\txt\";//文件所路径 var datasource = Directory.GetFiles(textPath).Select(i => new { path = i, fn = Path.GetFileName(i) }).ToList();//数据源 comboBox1.DisplayMember = "fn";//显示为文件名 comboBox1.ValueMember = "path";//值为文件的路径 comboBox1.DataSource = datasource; } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { var path = comboBox1.SelectedValue.ToString(); var s = File.ReadAllText(path, Encoding.UTF8); textBox1.Text = s; } } }
解决 3无用
悬赏问题
- ¥15 2020长安杯与连接网探
- ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
- ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
- ¥16 mybatis的代理对象无法通过@Autowired装填
- ¥15 可见光定位matlab仿真
- ¥15 arduino 四自由度机械臂
- ¥15 wordpress 产品图片 GIF 没法显示
- ¥15 求三国群英传pl国战时间的修改方法
- ¥15 matlab代码代写,需写出详细代码,代价私
- ¥15 ROS系统搭建请教(跨境电商用途)