以下のコードに問題があります。基本的には、拡張子が.configのファイルが保存されているパスから読み取られ、拡張子のないファイルの名前が読み取られ、すべてコンボボックスに表示されます。これは問題なく機能し、下矢印をクリックして名前を選択すると、実際には想定どおりに機能しますが、マウスでドロップダウンから項目を選択し、戻ってコンボボックス内で入力を開始すると、アプリケーションがクラッシュします。例外をスローします。
try-catch-finallyを追加しようとしましたが、同じエラーがスローされ続けます。コンボボックスに入力し始めると、アプリケーションがクラッシュする原因となっているのはループでしょうか?
pdマウスを使用してドロップダウンメニューから項目を選択すると、アプリケーションは正常に機能しますが、マウスで項目を選択し、キーボードを使用してコンボボックス内に別の項目名を入力すると、アプリがクラッシュします。任意のポインタが役立ちます。
// Gets all the file names from the path assigned to templatePath
// and assigns it to the string array fname
string[] fname = Directory.GetFiles(templatePath);
// Begin sorting through the file names assigned to the string array fname
foreach (string file in fname)
{
// Remove the extension from the file names and compare the list with
// the dropdown selected item
if (System.IO.Path.GetFileNameWithoutExtension(file) == cbTemplates.SelectedItem.ToString())
{
// StreamReader gets the contents from the found file and assigns
// them to the labels
using (var obj = new StreamReader(File.OpenRead(file)))
{
lbl1.Content = obj.ReadLine();
lbl2.Content = obj.ReadLine();
lbl3.Content = obj.ReadLine();
lbl4.Content = obj.ReadLine();
lbl5.Content = obj.ReadLine();
lbl6.Content = obj.ReadLine();
lbl7.Content = obj.ReadLine();
lbl8.Content = obj.ReadLine();
lbl9.Content = obj.ReadLine();
lbl10.Content = obj.ReadLine();
obj.Dispose();
}
}
}