private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if (listBox1.SelectedIndex >= 0) { ConfigProfile cp = listBox1.Items[listBox1.SelectedIndex] as ConfigProfile; if (cp != null) { txtCfgFile.Text = cp.ProfilePath; } } }
private void btDelete_Click(object sender, EventArgs e) { int n = listBox1.SelectedIndex; if (n >= 0) { ConfigProfile cp = listBox1.Items[n] as ConfigProfile; if (cp != null) { if (MessageBox.Show(this, string.Format(CultureInfo.InvariantCulture, "Do you want to delete this configuration file [{0}]?", cp), this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { try { File.Delete(cp.ProfilePath); listBox1.Items.RemoveAt(n); if (n < listBox1.Items.Count) { listBox1.SelectedIndex = n; } else if (listBox1.Items.Count > 0) { listBox1.SelectedIndex = listBox1.Items.Count - 1; } else { listBox1.SelectedIndex = -1; } } catch (Exception err) { MessageBox.Show(this, err.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } } }