我如何... wpf openfiledialoge multiselect从列表框传递到所选项目的文本框 [英] How do i...wpf openfiledialoge multiselect pass from listbox to textbox on selected item

查看:79
本文介绍了我如何... wpf openfiledialoge multiselect从列表框传递到所选项目的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要它,当列表选择项目在文本框上显示它及其多字节时,我的代码将在下面导致文件重新发送请帮助!!!



我尝试过:



OpenFileDialog openFileDialog = new OpenFileDialog();

private void btnOpenFiles_Click(object发件人,RoutedEventArgs e)

{



openFileDialog.Multiselect = true;

openFileDialog.Filter =Text files(* .txt)| * .txt |所有文件(*。*)| *。*;

openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

if(openFileDialog.ShowDialog()== true)

{

foreach(openFileDialog.FileNames中的字符串文件名)

{

lbFiles.Items.Add(System.IO.Path.GetFileName(filename));



}



}

}



private void lbFiles_SelectionChanged(object sender,SelectionChangedEventArgs e)

{

object item = lbFiles.SelectedItem;



if(item == null)

{


txtEditor.Text =没有选择项目;



}

其他

{



txtEditor.Text = File.ReadAllText(openFileDialog.FileName);





}

}

I NEED IT WHEN THE LISTBOX SELECTED ITEM TO DISPLAY IT ON TEXTBOX AND ITS MULTISELECT TXT MY CODE BELOW THE RESULT WAS IT REAPETS THE FILE PLEASE HELP!!!

What I have tried:

OpenFileDialog openFileDialog = new OpenFileDialog();
private void btnOpenFiles_Click(object sender, RoutedEventArgs e)
{

openFileDialog.Multiselect = true;
openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (openFileDialog.ShowDialog() == true)
{
foreach (string filename in openFileDialog.FileNames)
{
lbFiles.Items.Add(System.IO.Path.GetFileName(filename));

}

}
}

private void lbFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
object item = lbFiles.SelectedItem;

if (item == null)
{

txtEditor.Text = "No Item Selected";

}
else
{

txtEditor.Text = File.ReadAllText(openFileDialog.FileName);


}
}

推荐答案

txtEditor.Text = File.ReadAllText(openFileDialog.FileName);



您正在打开 openFileDialog 中的文件,而不是<$ c中所选项目中的文件$ c> ListBox 。



[edit]

代码应该是这样的:


You are opening the file from the openFileDialog instead of the one in the selected item of the ListBox.

[edit]
The code should be something like:

private void lbFiles_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    object item = lbFiles.SelectedItem;
    if (item == null)
    {
        txtEditor.Text = "No Item Selected";
    }
    else
    {
        txtEditor.Text = File.ReadAllText(item.ToString());
    }
}



[/ edit]


[/edit]


这篇关于我如何... wpf openfiledialoge multiselect从列表框传递到所选项目的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆