-1

Ok, this is hard to explain, I will try my best to be clear:

I have several hundred "string" items where each one may take you to one of 13 different forms. Instead of doing something like:

if (string == string) goto form1

hundreds of times I was hoping to do something like the following:

The user could care less which form comes up and does not need to know, they just click on the string object and it takes them to the correct form.

I think it would be easy if each string object had an associated INT value and then I go to the appropriate form instead of doing all of the string processing and comparison.

I do not see a way to do this with listbox. Is there a different structure I should use, or a work around to make this work with listbox?

Thanks for any help.

EDIT: So, I decided to use a dataset for this instead of a class object:

        DataRow row;
        row = itemTableA.NewRow();
        row["itemA"] = "Item Description";
        row["formA"] = 1;
        itemTableA.Rows.Add(row);
        row = itemTableA.NewRow();
        row["itemA"] = "Item Description";
        row["formA"] = 2;
        itemTableA.Rows.Add(row);

For every data object (maybe I will try to read in from a file if I can figure it out)

Then for my listbox I do:

        itemList.DataSource = itemTableA;
        itemList.DisplayMember = "itemA";
        itemList.ValueMember = "formA";

Lastly, I do my if statements:

        if (itemList.SelectedValue.ToString() == "1")
            do something;
        if (itemList.SelectedValue.ToString() == "2")
            do something;

etc.

Dr. Hoads
  • 79
  • 1
  • 8
  • What exactly is your binding container, List, Dictionary, Observable Collection? Also, what is contained in the container, just a string, or custom Objects? – Daryl Behrens Sep 30 '13 at 16:26
  • 4
    Haven't seen `goto` in a while. – sircodesalot Sep 30 '13 at 16:27
  • I just answered a question [here](http://stackoverflow.com/a/19098068/643085) from somebody with a similar XY problem. please read the answer. – Federico Berasategui Sep 30 '13 at 16:32
  • -1. Not only didn't you specify what you already tried, Now I find out this question is about winforms when it was clearly tagged WPF. – Federico Berasategui Sep 30 '13 at 17:38
  • Whoops. Sorry for the incorrect tag.. I did not try anything yet, because the way I know how to solve it is very inefficient. (just hundreds of string compares). So I was looking for a better solution just to start. – Dr. Hoads Sep 30 '13 at 17:43
  • @Dr.Hoads if you're `really` looking for a better, professional solution you should use WPF, which allows MVVM, as opposed to winforms, which doesn't support anything and requires a lot of noob-like code behind for anything. – Federico Berasategui Sep 30 '13 at 17:46

4 Answers4

2

Listboxes items are an object that are represented in the list by the value returned by ToString(). You can create a custom object that has the string to be displayed and a variable to mark which form to use. Then you have your custom object return the string in it's ToString() so it will display in the Listbox correctly:

public class ListItem
{
    public String value;
    public int form;
    public ListItem(string text, int formNumber)
    {
        this.value = text;
        this.form = formNumber;
    }


    public string ToString()
    {
        return value;
    }
}

Then wrap your strings around this object when you populate the Listbox.

I've used an int here as you mentioned but I would recommend an enum for clarity.

Fr33dan
  • 4,227
  • 3
  • 35
  • 62
1

There are a couple ways to do this (I am assuming you are not using the MVVM pattern by the way you phrased your question). Here is one

Create a custom Class

public Class CustomString
{
    public string MyString {get; set;}
    public int FormIdentifier {get; set;}
}

In Code Behind have a property that is a List of CustomStrings and a CustomerString instance called CurrentString

public List<CustomString> StringList {get; set;}
public CustomString CurrentString{get; set;}

Then in your xaml, bind SelectedItem to CurrentString

<ListBox ItemsSource="{Binding StringList}" DisplayMemberPath="MyString" SelectedValuePath="FormIdentifier" SelectedItem="{Binding CurrentString}" SelectionChanged="ChangedSelection">
    ........
</ListBox>

Create an Even handler in Code Behind File:

private void ChangedSelection(object sender, SelectionChangedEventArgs e)
{
   switch(CurrentString.FormIdentifier)
   {
       case 1:
           Form1 form = new Form1();
           form.ShowDialog();
           break;

       case 2:
           Form2 form2 = new Form2();
           form2.ShowDialog();
           break;
   } 
}

If you need an MVVM pattern solution please let me know and I will write one up.

Daryl Behrens
  • 643
  • 2
  • 7
  • 19
0
void formcheck()
{
   foreach(Object item in ListBox1.Items)
   {
     if (item.ToString() == "Form1")
     {
     Form F1 = new Form1{};
     F1.Show()
     }
     if (item.ToString() == "Form2")
     {
     Form F2 = new Form2{};
     F2.Show()
     }
     // And So On...
   }
}
Joe
  • 104
  • 1
  • 13
0

You can use a dictionary to hold form names and list of containing values. Here is sample meta code written in notepad:

private Dictionary<string, List<string>> formResolutionDictionary;

void SetUp()
{
    formResolutionDictionary = new Dictionary<string, List<string>> ();
    formResolutionDictionary.Add("form1", new List(){"input1", "input2"};
    formResolutionDictionary.Add("form2", new List(){"input3", "input4"};
    ...
}

string ResolveFormName(string input)
{
    return from keyValue in formResolutionDictionary
            where keyValue.Value == input
            select keyValue.Key;
}
oleksii
  • 35,458
  • 16
  • 93
  • 163