[英]How to get a richTextBox to read listbox
what i'm trying to do is this i have a listBox
that shows all .txt files in a folder but I want to be able to click a .txt in the listBox
and have my richTextBox
show the text from that .txt file. 我正在尝试做的是,我有一个
listBox
可以显示文件夹中的所有.txt文件,但是我希望能够单击listBox
的.txt并让我的richTextBox
显示该.txt文件中的文本。 Code to show files: 显示文件的代码:
private void Scripts_Load(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(@"scripts");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
list.Items.Add(file.Name);
}
As for the code to show in textBox
I have tried many internet answers and got nowhere main errors being Argument 1: cannot convert from 'object' to 'string'
The closest I have got is with this 至于要显示在
textBox
的代码,我尝试了许多Internet答案,但无处知道主要错误Argument 1: cannot convert from 'object' to 'string'
我最接近的是这个
//Get the FileInfo from the ListBox Selected Item
FileInfo SelectedFileInfo = (FileInfo) listBox.SelectedItem;
//Open a stream to read the file
StreamReader FileRead = new StreamReader(SelectedFileInfo.FullName);
//Read the file to a string
string FileBuffer = FileRead.ReadToEnd();
//set the rich text boxes text to be the file
richTextBox.Text = FileBuffer;
//Close the stream so the file becomes free!
FileRead.Close();
It Crashes saying: `System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'System.IO.FileInfo'.' 它崩溃时说:`System.InvalidCastException:'无法将类型为'System.String'的对象转换为类型为'System.IO.FileInfo'。
There was already a comment saying that this happens and the guy replied saying change line 1 to FileInfo SelectedFileInfo = new FileInfo(listBox1.SelectedItem); 已经有评论说发生这种情况,那个家伙回答说将第1行更改为FileInfo SelectedFileInfo = new FileInfo(listBox1.SelectedItem); this failed saying
Argument 1: cannot convert from 'object' to 'string'
` 这个失败的说法是
Argument 1: cannot convert from 'object' to 'string'
”
I did it YAY!!! 我做到了!
private async void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string value1 = list.SelectedItem.ToString();
richTextBox1.Text = value1;
using (StreamReader sr = new StreamReader("scripts\\" + value1))
{
{
String line = await sr.ReadToEndAsync();
richTextBox1.Text = line;
}
}
}
The reason is that in your Listbox are strings stored. 原因是在列表框中存储了字符串。
You musst try to convert them. 您想尝试转换它们。
var filename as listBox.SelectedItem as string;
if(string.IsNullOrWhiteSpace(filename))
{
return;
}
var path = Path.Combine(@"C:\", filename);
var fileinfo = new FileInfo(path);
I did it, it took me some time but I realized that I could get the text and move it to the textbox I didn't think that would help because I wanted the document not the name but then I put the Var as the file path, Here: 我做到了,花了一些时间,但我意识到我可以获取文本并将其移到文本框中,我认为这没有用,因为我想要的不是文件名,而是我把Var作为文件路径, 这里:
private async void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string value1 = list.SelectedItem.ToString();
richTextBox1.Text = value1;
using (StreamReader sr = new StreamReader("scripts\\" + value1))
{
{
String line = await sr.ReadToEndAsync();
richTextBox1.Text = line;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.