Rohan Pas
Rohan Pas

Reputation: 177

C# How to select an object on a listbox from another listbox

Say I want listBox1 to contain a set of first names. And when someone clicks on one of those first names, it will display the last name in listBox2 and already selected.

I seem to not be able to have the second listbox have it already selected.

So if the first item in listBox1 is selected, the first item in listBox2 is selected. And so on.

How would such be possible?

Here is some code:

private void materialFlatButton3_Click_1(object sender, EventArgs e)
        {
            OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
            OpenFileDialog1.Multiselect = true;
            OpenFileDialog1.Filter = "DLL Files|*.dll";
            OpenFileDialog1.Title = "Select a Dll File";
            if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // put the selected result in the global variable
                fullFileName = new List<String>(OpenFileDialog1.FileNames);


                foreach (string s in OpenFileDialog1.FileNames)
                {
                    listBox2.Items.Add(Path.GetFileName(s));
                    listBox4.Items.Add(s);
                }

            }
        }

    private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
    {
        string text = listBox2.GetItemText(listBox2.SelectedItem);
        textBox3.Text = text;
    }

In listbox4, it displays the full path. In listbox2, it displays just the name of the file.

How would I make it so when someone clicks on a file in listbox2, its corresponding path will be selected in listbox4?

Upvotes: 0

Views: 124

Answers (2)

Fabio
Fabio

Reputation: 32445

Create a class which represent full path and name for displaying.
Then use bind loaded data to the ListBox

public class MyPath
{
    public string FullPath { get; private set; }
    public string Name '
    { 
        get { return Path.GetFileName(s) }             
    }

    public MyPath(string path)
    {
        FullPath = path;
    }
}

// Load and bind it to the ListBox

var data = OpenFileDialog1.FileNames
                            .Select(path => new MyPath(path))
                            .ToList();

// Name of the property which will be used for displaying text
listBox1.DisplayMember = "Name"; 
listBox1.DataSource = data;

private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
{
    var selectedPath = (MyPath)ListBox1.SelectedItem;
    // Both name and full path can be accesses without second listbox
    // selectedPath.FullPath
    // selectedPath.Name
}

Upvotes: 1

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112279

Create you own type for storing and displaying the file names:

public class FileItem
{
    public FileItem (string path) => FullPath = path;
    public string FullPath { get; }
    public override ToString() => Path.GetFileName(FullPath);
}

And add these items to the listbox. That way you can store the full path and display the file name at the same time.


Alternatively just keep a reference to the original Files array or copy its content to another array. Then get the full path from this array through the selected index instead of from a second listbox ony used to store things.

Upvotes: 1

Related Questions