7 Replies - 12823 Views - Last Post: 07 April 2011 - 07:07 PM Rate Topic: -----

#1 TheCapm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 11

c# populating a listbox with text AND a value

Posted 07 April 2011 - 03:33 PM

Hello everyone. This is my first post on your forum so I'm going to try to do everything correctly as to not incite the ire of the torchbearers. :bigsmile:

Let me begin by outlining my situation. I'm a student and I'm trying to improve my skills, so I'm in the process of teaching myself C#, using visual studio 2010. I haven't taken a course in school specific to C#, so everything I know I've pieced together from previous experience and searching through articles on the internet. I apologize ahead of time if this question and my solution seems somewhat juvenile; as I said I am a student and attempting to teach myself. I'm well aware this may be a plodding implementation.

I'm writing a windows forms application to serve as a fantasy football draft manager. The player data is contained in a table in SQL Server Compact Edition 3.5. What I'm trying to do is to allow a user to pick a player from a listbox to "draft" to their team. The listbox is populated with a SQL query based on the option the user selects (Show all players, show only QBs, etc). In a column in the table is a "PCode" that I planned on using to cross-reference between the user's team and the list of players, that is to say it would be the "key" that allows the program to reference which players are contained in a user's team object and which ones are still available. (I hope that makes sense)

So, here's some code. This is the code that executes upon form load, and all the other versions (show only QBs, show only RBs, etc.) are derived from this.

conn = new SqlCeConnection(connectString);

            DataSet dsMasterTable = new DataSet();
            dsMasterTable = new DataSet();
            daMasterTable = new SqlCeDataAdapter("select pcode, overallrank, position, fname, lname, team from MasterTable where available = 'TRUE' order by overallrank", conn);
            SqlCeCommandBuilder cmdBldr = new SqlCeCommandBuilder(daMasterTable);
            daMasterTable.Fill(dsMasterTable, tableName);

            ArrayList arrayRank = new ArrayList();
            ArrayList arrayPos = new ArrayList();
            ArrayList arrayFname = new ArrayList();
            ArrayList arrayLname = new ArrayList();
            ArrayList arrayTeam = new ArrayList();

            foreach (DataRow row in dsMasterTable.Tables[0].Rows)
            {
                arrayRank.Add(row["OverallRank"]);
                arrayPos.Add(row["Position"]);
                arrayFname.Add(row["FName"]);
                arrayLname.Add(row["Lname"]);
                arrayTeam.Add(row["Team"]);
            }

            ArrayList arrayComplete = new ArrayList();

            for (int i = 0; i < arrayRank.Count; i++)
            {
                string buildString;

                buildString = Convert.ToString(arrayRank[i]) + "  " + Convert.ToString(arrayFname[i]) + " " + Convert.ToString(arrayLname[i])
                    + ", " + Convert.ToString(arrayPos[i]) + " - " + Convert.ToString(arrayTeam[i]);

                arrayComplete.Add(buildString);
            }

            string[] listBoxContents = (string[])arrayComplete.ToArray(typeof(string));

            lbxDisplay.Items.Clear();
            lbxDisplay.Items.AddRange(listBoxContents);

        }


As you can see, the function of this code is to put together a string pulling from the columns I want to show the user and enter it into the listbox. The problem I'm running in to is I don't know how to represent the listbox items in a way that I can use them as a key to place a player in a user's team object. Ideally, the user will select a row in the listbox (select a listbox item) and click the "Draft" button. The program would reference which item was selected, get the "PCode" value from either the DataSet or from the listbox item itself, then the user's team object would be updated to reflect the fact that the "PCode" has been added to the user's team. The DataSet would then be updated to make the drafted player unavailable (The bool column "Available" set to FALSE)and the listbox updated to only show the remaining available players.

I think I have stated my difficulties in a way that is understandable. I appreciate anyone who might take the time to help me think through this. I hope I haven't violated any rules of the forum in doing so. :whistling:

If any clarification is necessary, please let me know. I look forward to hearing from you all and posting again in the future. Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: c# populating a listbox with text AND a value

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: c# populating a listbox with text AND a value

Posted 07 April 2011 - 03:50 PM

Take a look at ListControl.ValueMember. It has an example at the bottom of how to use it.

You also need to learn more about classes, and to stay away from ArrayList :) Use List<T> .
Was This Post Helpful? 1
  • +
  • -

#3 MATTtheSEAHAWK  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 137
  • View blog
  • Posts: 782
  • Joined: 11-September 10

Re: c# populating a listbox with text AND a value

Posted 07 April 2011 - 04:52 PM

Well this approach might be considered different but you COULD use the tag property of the list box. Set it as an array of the values you need in the same order of the list box. Then when you get the index of the list box cast the list box tag as the type of array you need then get the value at the index you need with the index that was from the main list box item.

This post has been edited by MATTtheSEAHAWK: 07 April 2011 - 04:52 PM

Was This Post Helpful? 0
  • +
  • -

#4 TheCapm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 11

Re: c# populating a listbox with text AND a value

Posted 07 April 2011 - 04:54 PM

View PostMomerath, on 07 April 2011 - 05:50 PM, said:

Take a look at ListControl.ValueMember. It has an example at the bottom of how to use it.

You also need to learn more about classes, and to stay away from ArrayList :) Use List<T> .



Thanks for your quick reply. I've been using generic lists in other parts of my program, I guess I wrote this before I learned about them. It shouldn't be hard to convert them to the newer style List<T>. Thanks for that tip.

What do you mean "I need to learn more about classes"? I agree that I have a lot to learn about C# and object-oriented design in general, but what did you see in my post that a deeper knowledge of classes could help with?

Hoping to tap into the experience of the masses!
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: c# populating a listbox with text AND a value

Posted 07 April 2011 - 06:18 PM

The fact that you're using parallel arrays/lists. Create a class to contain each piece of data that makes up a single point, then make an array/list out of that.

For example, your class would have properties Rank, Position, FirstName, LastName, and Position. This could be called "Player" or whatever it represents.
Was This Post Helpful? 1
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: c# populating a listbox with text AND a value

Posted 07 April 2011 - 06:40 PM

The ListControl has a .DisplayMember and a .ValueMember.

The Display member is used to display something in the GUI. If you want to display something from your table, you can do something like:

listControl1.DisplayMember = "ColumnNameHere";



To set something more specific, finely tuned to what you need, read the example on this page:

http://msdn.microsof...ember.aspx#Y297

It shows you how to use DisplayMember and ValueMember and even how to bind data to it.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#7 TheCapm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-April 11

Re: c# populating a listbox with text AND a value

Posted 07 April 2011 - 07:00 PM

View PostCurtis Rutland, on 07 April 2011 - 08:18 PM, said:

The fact that you're using parallel arrays/lists. Create a class to contain each piece of data that makes up a single point, then make an array/list out of that.

For example, your class would have properties Rank, Position, FirstName, LastName, and Position. This could be called "Player" or whatever it represents.



Thanks for this. It's so obvious but I hadn't thought of it this way. It certainly makes more sense. This too would solve my original problem, because the display/value members could be set to whatever property of the class I choose, correct? This is going to work itself out quite easily I believe! :bananaman: :bananaman:
Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: c# populating a listbox with text AND a value

Posted 07 April 2011 - 07:07 PM

Bingo. It's an important design principle in Object Oriented design.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1