List box detects that iteam is being selected

Asked

Viewed 33 times

-1

I want when I double click on the listbox item, when start it do an action, when I double click on any other item it do another action.

Code:

//btnWhenClicked
private void btnWhenStart_Click(object sender, EventArgs e)
{
    ListItemsBox.Items.Add("When Start"); //é este iteam que eu quero que detect
    btnWhenStart.Hide();
    string path = AppDomain.CurrentDomain.BaseDirectory.ToString() + "MyTest.txt";

    //Create And Write File
    if (!File.Exists(path))
    {
        using (StreamWriter sw = File.CreateText(path))
        {
            sw.WriteLine("using System;");
            sw.WriteLine("using System.Collections.Generic;");
            sw.WriteLine("using System.Linq;");
            sw.WriteLine("using System.Text;");
            sw.WriteLine("using System.Threading.Tasks;");

            sw.WriteLine("\r\n namespace CMD");
            sw.WriteLine("{");
            sw.WriteLine("    class Program");
            sw.WriteLine("    {");
            sw.WriteLine("        static void Main(string[] args)");
            sw.WriteLine("        {");
            sw.Close();
        }

    }



}

//delete ListItemsBox Selected Item
private void ListItemsBox_DoubleClick(object sender, EventArgs e)
{
    string path = AppDomain.CurrentDomain.BaseDirectory.ToString() + "MyTest.txt";
    DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this item?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
    if (dialogResult == DialogResult.Yes)
    {
        ListItemsBox.Items.Remove(ListItemsBox.SelectedItem);
    }
    else if (dialogResult == DialogResult.No)
    {

    }
}

1 answer

0


I was able to figure out how to make it simpler than it looks

private void ListItemsBox_DoubleClick(object sender, EventArgs e)
{
    string path = AppDomain.CurrentDomain.BaseDirectory.ToString() + "MyTest.cs";
    if (ListItemsBox.SelectedItem.Equals("When Start"))
    {
        DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this item?All items will be deleted including all files!", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        if (dialogResult == DialogResult.Yes)
        {
            ListItemsBox.Items.Clear();
            File.Delete(path);
            btnWhenStart.Show();
        }
        else if (dialogResult == DialogResult.No)
        {

        }
    }
    else
    {
        DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this item?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        if (dialogResult == DialogResult.Yes)
        {
            ListItemsBox.Items.Remove(ListItemsBox.SelectedItem);
        }
        else if (dialogResult == DialogResult.No)
        {

        }
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.