Can't delete an datasource in a listbox

When i try to delete an datasource i got an panic, that it can't b

Page 1 of 1

2 Replies - 2923 Views - Last Post: 13 November 2008 - 03:02 AM Rate Topic: -----

#1 micke  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-August 08

Can't delete an datasource in a listbox

Post icon  Posted 11 November 2008 - 02:20 PM

Hi,

When i try to delete an datasource i got an panic, that it can't be deleted. How can i delete that one?

and when first running the prg. it isen't load the added info from the arrayList how am i doing that? it is only loading from the arrayList when i mark an object in the listbox...

thanks...



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

namespace ArrayListTest
{
    public partial class Form1 : Form
    {
        //Create an instance of ArrayList class
        ArrayList arrList = new ArrayList();

        public Form1()
        {
            InitializeComponent();
        }

       private void Form1_Load(object sender, EventArgs e)
        {
            listBoxCD.SelectedIndex = 0;

            //Add items
            arrList.Add(new CD { Title = "Title1", Artist = "Artist1", Country = "Country1", Company = "Company1", Year = "Year1" });
            arrList.Add(new CD { Title = "Title2", Artist = "Artist2", Country = "Country2", Company = "Company2", Year = "Year2" });
            arrList.Add(new CD { Title = "Title3", Artist = "Artist3", Country = "Country3", Company = "Company3", Year = "Year3" });
            arrList.Add(new CD { Title = "Title4", Artist = "Artist4", Country = "Country4", Company = "Company4", Year = "Year4" });

            //Set no item selected
            listBoxCD.SelectedIndex = -1;
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {

            if (this.textTitle.Text.Equals(""))
            {
                MessageBox.Show("You must enter a Title");
                this.textTitle.Focus();
            }

            if (this.textArtist.Text.Equals(""))
            {
                MessageBox.Show("You must enter a Artist");
                this.textArtist.Focus();
            }

            if (this.textCountry.Text.Equals(""))
            {
                MessageBox.Show("You must enter a Country");
                this.textCountry.Focus();
            }

            if (this.textCompany.Text.Equals(""))
            {
                MessageBox.Show("You must enter a Company");
                this.textCompany.Focus();
            }

            if (this.textYear.Text.Equals(""))
            {
                MessageBox.Show("You must enter a Year");
                this.textYear.Focus();
            }

            listBoxCD.Items.Add(textTitle.Text);
            //listBoxCD.Items.Add(textArtist.Text);
            //listBoxCD.Items.Add(textCountry.Text);
            //listBoxCD.Items.Add(textCompany.Text);
            //listBoxCD.Items.Add(textYear.Text);

            countCD.Text = String.Format("Current Inventory: {0} Item", this.arrList.Count.ToString()); 

            arrList.Add(new CD { Title = textTitle.Text, Artist = textArtist.Text, Country = textCountry.Text, Company = textCompany.Text, Year = textYear.Text });
        }


        private void btnRemove_Click(object sender, EventArgs e)
        {

            if (listBoxCD.SelectedIndex == -1)
            {
                MessageBox.Show("Please select an item first.", "No item selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            else
            {
                listBoxCD.Items.RemoveAt(listBoxCD.SelectedIndex);
            } 
        }


        private void listBoxCD_SelectedIndexChanged(object sender, EventArgs e)
        {
            //MessageBox.Show(listBoxCD.SelectedIndex.ToString() + "\n" + listBoxCD.GetItemText(listBoxCD.SelectedItem), "listBoxCD_SelectedIndexChanged");

            listBoxCD.DataSource = null;
            listBoxCD.DataSource = arrList;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            listBoxCD.Items.Clear();
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Can't delete an datasource in a listbox

#2 zakary  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 27
  • View blog
  • Posts: 427
  • Joined: 15-February 05

Re: Can't delete an datasource in a listbox

Posted 12 November 2008 - 06:04 AM

in your btnRemove_Click method instead of
private void btnRemove_Click(object sender, EventArgs e)  
{
	if (listBoxCD.SelectedIndex == -1)  
	{  
		MessageBox.Show("Please select an item first.", "No item selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);  
	}  
	else  
	{  
		listBoxCD.Items.RemoveAt(listBoxCD.SelectedIndex);  
	}   
} 


try using SelectedItem like the following
private void btnRemove_Click(object sender, EventArgs e)  
{
	if (listBoxCD.SelectedItem == null)  
	{  
		MessageBox.Show("Please select an item first.", "No item selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);  
	}  
	else  
	{  
		listBoxCD.Items.RemoveAt(listBoxCD.SelectedItem);  
	}   
} 


Was This Post Helpful? 0
  • +
  • -

#3 micke  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 25-August 08

Re: Can't delete an datasource in a listbox

Posted 13 November 2008 - 03:02 AM

Hi,

thanks, but it still dosen't work.

Tried this as well

int delete = Convert.ToInt32(listBoxCD.SelectedIndex);
               listBoxCD.Items.RemoveAt(delete);


and

 string[] sa = new string[listBoxCD.SelectedItems.Count];
           listBoxCD.SelectedItems.CopyTo(sa, 0);

           foreach (string filename in sa)
           {
               listBoxCD.Items.Remove(filename);
           }

This post has been edited by micke: 13 November 2008 - 03:03 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1