liav bahar
liav bahar

Reputation: 247

How I Select value from object on the listbox?

Here is the list item Class:

class ListItem
{
    public string Key;
    public string Value;
    public ListItem()
    {
    }
    public string key
    {
        get { return Key; }
        set { key = value; }
    }
    public string value
    {
        get { return Value; }
        set { Value = value; }
    }
    public override string ToString()
    {
        return Key;
    }
    public string getvalue(string blabla)
    {
        return Value;
    }
}

private void btnOpen_Click(object sender, EventArgs e)
{
    string[] Folders = Directory.GetDirectories(txtFolder.Text);

    foreach (string f in Folders)
    { 
        ListItem n = new ListItem();
        n.Value = f;
        n.Key = Path.GetFileName(f);  
        listBoxSidra.Items.Add(n);
    }
}

private void listBoxSidra_SelectedIndexChanged_1(object sender, EventArgs e)
{
    try
    {
        lblmsg.Text = null;
        comboBoxSeason.Items.Clear();
        string[] seasons = Directory.GetDirectories(listBoxSidra.SelectedValue.ToString());
        for (int i = 0; i < seasons.Length; i++)
        {
            comboBoxSeason.Items.Add(seasons[i]);
        }
        comboBoxSeason.SelectedIndex = 0;
    }
    catch (Exception ex)
    {
        lblmsg.Text = ex.Message;
    }
}

First Method : I open class named it ListItem , its contains foldername(key) and folder location (value). Secound Method: I created an array wich contains all the subdirectories from the the directory I set on the text box . I also created ListItem object named it 'n' , then I set Values to 'n' ,n.Value(represnt the directory location) and n.Key(represnt the directory name). next step is to add the 'n' object to the list box, now on the listbox i can see the directory name , and each object contains his location .

Thrid Method : thats where im stuck , i created an array, the array suppoed to contain the subdirectory from the chosen listbox item , I mean ,when I will click on a listbox item I want to get his value(the value represent the location ) and by that add the subdirectories to the array , what should i write instead of listBoxSidra.SelectedValue.ToString() ??

Thanks!

Upvotes: 1

Views: 509

Answers (1)

Sangram Chavan
Sangram Chavan

Reputation: 351

In order to make your code work make following changes

    private void btnOpen_Click(object sender, EventArgs e)
    {
        string[] Folders = Directory.GetDirectories(txtFolder.Text);


        var dataSource = new List<ListItem>();
        foreach (string f in Folders)
        {
            ListItem n = new ListItem();
            n.Value = f;
            n.Key = Path.GetFileName(f);
            dataSource.Add(n);
        }

        listBoxSidra.DataSource = dataSource;
        listBoxSidra.DisplayMember = "key";
        listBoxSidra.ValueMember = "value";
    }

Upvotes: 2

Related Questions