【发布时间】:2014-07-20 18:02:28
【问题描述】:
在组合框中选择了错误的项目(C#、WPF)
我确实有一个comboBox 和一个textBox。当我在组合框中选择一个项目(html 文件)时,我想将内容添加到我的文本框中。这是我到目前为止所得到的:
public void SetDataPoolToComboBox()
{
comboBox_DataPool.Items.Clear();
comboBox_DataPool.Items.Add("Please choose a file...");
comboBox_DataPool.SelectedIndex = 0;
if (true == CheckPath())
{
foreach (string s in Directory.GetFiles(pathTexts, "*.html"))
{
comboBox_DataPool.Items.Add(Path.GetFileNameWithoutExtension(s));
}
}
}
public void comboBox_DataPool_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SetContentToTextBox();
}
public void SetContentToTextBox()
{
if (true == CheckPath())
{
FileInfo[] fileInfo = directoryInfo.GetFiles("*.html");
if (fileInfo.Length > 0)
{
if (comboBox_DataPool.Text == "Please choose a file..." || comboBox_DataPool.Text == "")
{
textBox_Text.Text = string.Empty;
}
else
{
StreamReader sr = new StreamReader(fileInfo[comboBox_DataPool.SelectedIndex].FullName, Encoding.UTF8);
textBox_Text.Text = sr.ReadToEnd();
sr.Close();
}
}
}
}
问题:当我选择第二个项目时,会显示第三个项目的内容。每次都会发生同样的情况 - 我选择哪个项目无关紧要。当我选择最后一项时,应用程序被踢:“错误:索引超出范围!”
【问题讨论】:
标签: c# wpf combobox textbox fileinfo