私は何をしようとしていますこれは私がlistBox
フォルダ内のすべての.txtファイルを表示していますが、listBox
の.txtをクリックできるようにしたいrichTextBox
その.txtファイルのテキストを表示します。ファイル表示する コード:textBox
に表示するコードについてはリストボックスを読み込むにはrichtextBoxを取得する方法
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);
}
は、私は多くのインターネットの回答を試してみましたが、持っているどこにも主なエラーであること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();
であります'System.InvalidCastException:' 'System.String.FileInfo'と入力すると 'System.String'タイプのオブジェクトをキャストできません。
このことが起こったとのコメントがありましたが、男は変更行1をFileInfo SelectedFileInfo = new FileInfo(listBox1.SelectedItem)に返信しました。これは失敗Argument 1: cannot convert from 'object' to 'string'
私はそれをしました!
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;
}
}
}
* textBox *で表示するコードについては、コードとエラーがありますが、それもあなたの質問に追加できますか? –
何を試しましたか?その部分を分かち合うことができますか?どの例外がこの例外を投げていますか? – Abhay
私は他のものも試したことがあります。ほとんどの場合、それはうまくいったが、間違った結果(ファイル名やそれに類するもの)が表示される。 –