Bluewolf Bluewolf - 4 months ago 10
C# Question

How to write a code in a specific line?

I need help. I don't know how 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 internet that could help me. If you know how to do this or a tutorial please 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)
{

}


I don't know if this can help you:

Update2
I know there is a double click event I just don't know how to delete an item when double click.

Update3
I can't do more questions so I ask here.

How can I write a text in specific line like this code?

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)
    {

    }
}