Adding and removing text to / from a ListBox hosted in WinForm using C #

I am working on a simple application to add / remove the string / s to an array and show this in a ListBox.

An image that shows my app

My code only shows the last value that was entered in the text box and

private void Add_Click(object sender, EventArgs e)
{
    string add = textBox1.Text;
    List<string> ls = new List<string>();
    ls.Add(add);
    String[] terms = ls.ToArray();
    List.Items.Clear();
    foreach (var item in terms)
    {
        List.Items.Add(item);
    }
}


private void Delete_Click(object sender, EventArgs e)
{

}
+4
source share
4 answers

This code does not make sense. You add one element to the list, and then convert it to an array (still in contact with one element) and finally skip this array, which, of course, adds one element to the previously cleared list. Therefore, your list will always contain one element. Why not just add the item directly?

private void Add_Click(object sender, EventArgs e)
{
    List.Items.Add(textBox1.Text);
}

private void Delete_Click(object sender, EventArgs e)
{
    List.Items.Clear();
}

Also clear the list in Delete_Clickinstead Add_Click.


, List<string> DataSource .

, , , null, .

private List<string> ls = new List<string>();

private void Add_Click(object sender, EventArgs e)
{
    string add = textBox1.Text;

    // Avoid adding same item twice
    if (!ls.Contains(add)) {
        ls.Add(add);
        RefreshListBox();
    }
}

private void Delete_Click(object sender, EventArgs e)
{
    // Delete the selected items.
    // Delete in reverse order, otherwise the indices of not yet deleted items will change
    // and not reflect the indices returned by SelectedIndices collection anymore.
    for (int i = List.SelectedIndices.Count - 1; i >= 0; i--) { 
        ls.RemoveAt(List.SelectedIndices[i]);
    }
    RefreshListBox();
}

private void RefreshListBox()
{
    List.DataSource = null;
    List.DataSource = ls;
}
+2

. . , :

  • .
  • "", ( , . ).
  • "", .

, Add_Click, , Delete_Click. , .

private void Add_Click(object sender, EventArgs e)
{
    // Since you do not want to add empty or null
    // strings check for it and skip adding if check fails
    if (!String.IsNullEmptyOrWhiteSpace(textBox1.Text)
    {
        // Good habit is to remove trailing and leading 
        // white space what Trim() method does
        List.Items.Insert(0, textBox1.Text.Trim());
    }
}

private void Delete_Click(object sender, EventArgs e)
{
    // Get all selected items indices first
    var selectedIndices = List.SelectedIndices;

    // Remove every selected item using it index
    foreach(int i in selectedIndices)
        List.Items.RemoveAt(i);
}

, " ", List.Items.Clear(). , " " @Olivier Jacot-Descombes.

+2

You can use in C #:

    private void Delete_Click(object sender, EventArgs e)
    {
        if(myList.Contains(textbox1.value))//if your list containt the delete value
        {
           myList.Remove(textbox1.value); //delete this value
        }
        else
        {
           //the list not containt this value
        }
    }

and you can use the same method to check if a value exists when trying to add

+1
source
private void AddItem()
{
    if (!String.IsNullEmptyOrWhiteSpace(textBox1.Text))
    {
        var newItem = textBox1.Text.Trim();
        if (!List.Items.Contains(newItem))
        {
            List.Items.Add(newItem);
            // Alternative if you want the item at the top of the list instead of the bottom
            //List.Items.Insert(0, newItem);

            //Prepare to enter another item
            textBox1.Text = String.Empty;
            textBox1.Focus();
        }    
    }
}

private void Add_Click(object sender, EventArgs e)
{
    AddItem();
}

Private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        AddItem();
    }
}  

private void Delete_Click(object sender, EventArgs e)
{
    // Remove every selected item using it index
    foreach(var item in List.SelectedItems)
    {
        List.Items.Remove(item);
    }
}
+1
source

Source: https://habr.com/ru/post/1688037/


All Articles