ExtremeSwat
ExtremeSwat

Reputation: 824

Getting selected item from a listbox

My issue is pretty weird, I am trying to write to a string the selected item of a list, and my string is always empty.

This is my code:

void loader(MethodInfo[] methArr)
{
    //string abba = "";

    listBox1.Items.Clear();

    //loop
    for (int i = 0; i < methArr.Length; i++)
    {
         ab = ab.AppendLine(methArr[i].ToString());
         //abba = ab.ToString();
         listBox1.Items.Add(ab);
         ab.Clear();
    }


}

private void button1_Click(object sender, EventArgs e)
{
    checker();
}

private void button2_Click(object sender, EventArgs e)
{

    string a = listBox1.GetItemText(listBox1.SelectedItem.ToString());
    string b = listBox1.SelectedItem.ToString();
    string c = "dummy";
    MessageBox.Show(a + b + c);
}

If I use SelectedIndex it will show me the index, but otherwise I'll get just the C string

This is my full code : http://pastebin.com/i0McETZz

enter image description here

DEBUG - EDIT2 enter image description here

Upvotes: 0

Views: 6167

Answers (5)

petelids
petelids

Reputation: 12815

This is because of the way you are adding the items rather than the way you are retrieving them.

When you call listBox1.Items.Add(ab); you are adding the instance ab to the listbox. This means you have 4 references to ab in your listbox but ab is empty due to the ab.Clear() call. The text must be set on the value of ab at the time it was added to the listbox but when you retrieve the referenced item you get the current ab which is empty.

To solve this you can create a new StringBuilder on each iteration of your loop and remove the call to Clear():

for (int i = 0; i < methArr.Length; i++)
{
    StringBuilder ab = new StringBuilder();    
    ab = ab.AppendLine(methArr[i].ToString());
    listBox1.Items.Add(ab);
}

Or use the ToString() method of the StringBuilder:

StringBuilder ab = new StringBuilder();                
for (int i = 0; i < methArr.Length; i++)
{

    ab = ab.AppendLine(methArr[i].ToString());
    listBox1.Items.Add(ab.ToString());
    ab.Clear();
}

Upvotes: 2

James
James

Reputation: 9965

Don't add the string builder to the list, that's an object reference to an object collection. Use the ToString() method (as you have commented out)

See this example:

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;

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

        private void button1_Click(object sender, EventArgs e)
        {
            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < 10; i++)
            {
                builder.Append(i);
                listBox1.Items.Add(builder);
                builder.Clear();
            }

            for (int i = 0; i < 10; i++)
            {
                builder.Append(i);
                listBox2.Items.Add(builder.ToString());
                builder.Clear();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string a = listBox1.GetItemText(listBox1.SelectedItem.ToString());
            string b = listBox1.SelectedItem.ToString();
            string c = listBox1.GetItemText(listBox1.SelectedItem);
            string d = listBox2.GetItemText(listBox2.SelectedItem.ToString());
            string f = listBox2.SelectedItem.ToString();
            string g = listBox2.GetItemText(listBox2.SelectedItem);
            MessageBox.Show("1:" + a + b + c + "\r\n2: " + d+ f + g);
        }
    }
}

enter image description here

Upvotes: 4

Redaa
Redaa

Reputation: 1590

You just need to get the selected item to ListBoxItem and then assign the content to the string you need:

            ListBoxItem listboxitem = (ListBoxItem) ListBox1.SelectedItem;
            string a = listboxitem.Content.ToString();

this is working for me, hope it helps

Upvotes: 1

ivan.mylyanyk
ivan.mylyanyk

Reputation: 2101

I guess you are using a multiple-selection ListBox, so, in this way you should use ListBox.SelectedItems, and iterate over the received items to print them separately.

Upvotes: 1

user3913686
user3913686

Reputation:

If you want to retrieve the display text of the item, use the GetItemText method:

string text = listBox1.GetItemText(listBox1.SelectedItem);

Upvotes: 2

Related Questions