ユーザーは、次のように ListBox 内の項目をクリックできます。
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox2.Clear();
listBox2.Items.Clear();
string[] p =
Directory.GetFiles(
textBoxDir.Text,
listBox1.SelectedItem.ToString(),
SearchOption.AllDirectories);
foreach (string open in p)
......
}
すべて良好。ただし、ユーザーが ListBox の空のスペースをクリックすると、次のエラーが表示されます。
System.NullReferenceException
これは、次のコード行によるものです。
string[] p =
Directory.GetFiles(
textBoxDir.Text,
listBox1.SelectedItem.ToString(),
SearchOption.AllDirectories);
誰かが巧妙な回避策を持っていますか? または、私のコードに代替案を提案しますか?