0

I have two lists. The first contains names (string) the second contains filters (object).

What I want is to supply a UI to the user, in order to allow them to match a name with a filter.

A filter can be matched to many names, a name must be matched to exactly one filter.

How do I do this? I believe there are no ready-made controls for the job.

Example:

User should be able to do this:

  • name1 -> filterA
  • name2 -> filterA
  • name3 -> filterB
Odys
  • 8,951
  • 10
  • 69
  • 111

2 Answers2

1

IF you are using ListView you can use Tag & Name propety of ListViewItem to supply some hidden data (atleast from user) and compare those properties of each listview item.

You can try something like this (it is not tested and written on the fly :P) I am still not sure if I got your question. so forgive me!!

public Form1()
{
    InitializeComponent();

    ListViewItem item1;

    int i = 1;
    while (i < 6)
    {
        item1 = new ListViewItem();
        item1.Text = "Item" + i.ToString();
        item1.Tag = new List<string>();
        listView1.Items.Add(item1);

        i++;
    }

    i = 1;
    while (i < 6)
    {
        item1 = new ListViewItem();
        item1.Text = "Filter" + i.ToString();
        listView2.Items.Add(item1);

        i++;
    }
}

private void button1_Click(object sender, EventArgs e)
{
    List<string> temp = (List<string>)listView1.SelectedItems[0].Tag;

    temp.Add(listView2.SelectedItems[0].Text);

    listView1.SelectedItems[0].Tag = temp;
 }
Dumbo
  • 13,555
  • 54
  • 184
  • 288
  • how is user going to do the matching? – Odys Dec 13 '11 at 16:03
  • Can you update your question with some sample data so people can get a better idea of what you are looking for? Obviously the matching should be done with your code :P User just sees a string on the screen! – Dumbo Dec 13 '11 at 16:09
  • added an example in my question – Odys Dec 13 '11 at 16:14
1

Most natural way I can think of would be to supply a ListView with all the names on the left, and a ComboBox on the right that holds the filters.

This would also ensure the user can only select one filter.

Louis Kottmann
  • 16,268
  • 4
  • 64
  • 88
  • This was also my first thought. Have you found any examples on how to do this using datasources? I really do not want to map everything and get the values back from the ui and add them in classes and so on.. – Odys Dec 13 '11 at 21:45
  • It's been a while since I did any winforms development... however there are plenty of questions on the subject here on SO. Example: http://stackoverflow.com/questions/600869/how-to-bind-a-list-to-a-combobox-winforms – Louis Kottmann Dec 14 '11 at 00:03