Hey guys, I am needing to detect when the last item in a listbox is selected. I first tried comparing the listbox.items.count with the listbox.selectedIndex, but unfortunately, whenever i load the items from a file to the listbox, there are 1 or two extra items at the end that are blank.

I then tried going to the last index in the listbox, and if it was "", then i went up an item, until i got one that wasn't "", and that would be my last item. Unfortunately, that didn't seem to work.

Is there an easy way to detect the last NON NULL item in a listbox??

Please show us your code.

if (Listbox1.SelectedIndex == Listbox1.Items.Count - 1)
            {

            }

Please show us your code.

if (Listbox1.SelectedIndex == Listbox1.Items.Count - 1)
            {

            }

yes sir, as I said in my post, I have already tried comparing the listbox count and listbox index, and it doesn't work b/c for some reason I am getting extra NULL valued items at the end of the listbox.

Can you post that code which populates listbox please? For easy readability, always wrap programming code within posts in CODE

the code used to populate the listbox was this:

this.listBoxAccounts.Items.Clear();
            OpenFileDialog Open = new OpenFileDialog();
            Open.Filter = "Text Document|*.txt|All Files|*.*";
            try
            {
                Open.ShowDialog();
                StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
                while (Import.Peek() >= 0)
                    listBoxAccounts.Items.Add(Convert.ToString(Import.ReadLine()));
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex.Message));
                return;
            }

Do not allow to add empty or null string into items collection.

string[] lines = System.IO.File.ReadAllLines(Open.FileName);

listBoxAccounts.Items.Clear();

foreach (string line in lines)
{
  if (!string.IsNullOrEmpty(line))
   {
    listBoxAccounts.Items.Add(listBoxAccounts);
   }
}

ok..so i did what u said, so now this is my code

this.listBoxAccounts.Items.Clear();
            OpenFileDialog Open = new OpenFileDialog();
            Open.Filter = "Text Document|*.txt|All Files|*.*";
            try
            {
                Open.ShowDialog();
                StreamReader Import = new StreamReader(Convert.ToString(Open.FileName));
                while (Import.Peek() >= 0)
                    if (!string.IsNullOrEmpty(Import.ReadLine()))
                    {
                        listBoxAccounts.Items.Add(Convert.ToString(Import.ReadLine()));
                    }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex.Message));
                return;
            }


            listBoxAccounts.SelectedIndex = (listBoxAccounts.Items.Count - 1);
            accSwitcher = listBoxAccounts.SelectedItem.ToString();
            

            MessageBox.Show(accSwitcher);

but it still adds a null line as the last item in teh listbox.

Please upload that datafile here. (Use attachment option).

umm i would rather not upload the file b/c it is a list of accounts and passwords lol...but basically it looks like this

acc1:pass1
acc2:pass2
acc3:pass3
etc

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.