Bluewolf Bluewolf - 4 months ago 8
C# Question

c# Ho to write a code in a specifique line

I need help I don't know I do to delete an item from the listbox by double click the item I have just started like 1 hour ago so I don't have code that can help. I didn't find anything on interned that could help me. if you know how to do this or a tutorial pleas comment that

UPDATE 1
this is all the code i have

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace Scratch
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

}

//close form
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}

//btnWhenClicked
private void btnWhenStart_Click(object sender, EventArgs e)
{
ListItemsBox.Items.Add("When Start");
btnWhenStart.Hide();
string path = @"C:\Users\Estagio\Desktop\MyTest.txt";

//Create And Write File
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("using System;");
}
}


}

//delete ListItemsBox Selected Item
private void ListItemsBox_DoubleClick(object sender, EventArgs e)
{

}


Idont know if this can help you

Update2
I know there is a double click event i just dont know how to delete a item when double click

Update3
I cant do more question so i ask here.
how can I write a text in specific line
like i have this code is
exp:

private void btnStringEdit_Click(object sender, EventArgs e)
{
ListItemsBox.Items.Add("When Start");
btnWhenStart.Hide();
string path = @"C:\MyTest.txt";

if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("line1");
sw.WriteLine("line2");
sw.WriteLine("line4");
}
}
}


then i have another

private void btnAnotherEdit_Click(object sender, EventArgs e)
{
ListItemsBox.Items.Add("When Start");
btnWhenStart.Hide();
string path = @"C:\MyTest.txt";

if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
//Here i want some code that put something //between line2 and line 4
}
}
}


how can i do it

Answer

UPDATE

private void ListItemsBox_DoubleClick(object sender, EventArgs e){
    DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this item?",
                                "Warning", MessageBoxButtons.YesNo, 
                                MessageBoxIcon.Warning,MessageBoxDefaultButton.Button2);
    if (dialogResult == DialogResult.Yes)
    {
        ListItemsBox.Items.Remove(ListItemsBox.SelectedItem);
    }
    else if (dialogResult == DialogResult.No)
    {

    }
}