Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there an equivelant to lstBox.ItemData(lstBox.NewIndex) 1

Status
Not open for further replies.

CaptainD

Programmer
Jul 13, 1999
644
0
0
US
I'm switching from VB6 to C#

With VB6 I could assign a recordsets ID number to the index so that when you selected values in a listbox or combobox I could use those values to pull records or update records from/in the database.

Code:
        lstPersonnel.AddItem rs.("LName").Value
        lstPersonnel.ItemData(lstPersonnel.NewIndex) = rs("Personnelid").Value

In the above example the listbox would show a persons last name and when I selected that person the value to be used to update their information would be based off of the persons "PersonnelID"

Is there a way to do that with a listobx in C# ?

I can populate the listbox, get counts, text values etc. but I have not found the equivilant to my question above.
 
Since there has been no replies to the above question I came up with this solution so far but I'm not sure if this is the correct way to go. (But it is working)

I'm using a form level SortedList<>
Code:
        public frmMain()
        {
            InitializeComponent();
        }
        //SortedList to hold the newIndex values
        SortedList<int, int> NewIndex = new SortedList<int, int>(2);

From there I'm populating the SortedList with an int variable for the index and, in this case a "BadgeNumber" for testing.
There is a SqlDataReader to populate the listbox
Code:
                rdr = cmd.ExecuteReader();
                //Clear the listbox
               lstPersonnel.Items.Clear();
               lstPersonnel.BeginUpdate();
               int i = 0;
                //Start the top values
                lstPersonnel.Items.Add("Captains", false);
                NewIndex.Add(i, 0);
                i += 1;
                lstPersonnel.Items.Add("Engineers", false);
                NewIndex.Add(i, 1);
                i += 1;
                lstPersonnel.Items.Add("Fire Fighters", false);
                NewIndex.Add(i, 2);
                i += 1;
                lstPersonnel.Items.Add("Captains and A/C", false);
                NewIndex.Add(i, 3);
                i += 1;
                lstPersonnel.Items.Add("Engineers and A/E", false);
                NewIndex.Add(i, 4);
                i += 1;
                //Populate the rest off the datareader
                while (rdr.Read())

                {
                    lstPersonnel.Items.Add(rdr["fname"].ToString() +
                        ", " + rdr["lname"].ToString(), false);
                    NewIndex.Add(i, Convert.ToInt16(rdr["BadgeNum"].ToString()));
                    i += 1;
                }
                lstPersonnel.EndUpdate();


            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
                if (con.State == ConnectionState.Open)
                    con.Close();
            }
        }

Is this a good way or a bad way, in C#, to do this ?

I'm also concidering creating a custom control but I'm not sure what is the best way to go.

 
public class Customer
{
int id;
string name;
}

set the Value of the DropDownList to id and the Text to name.
Then access DropDownList.SelectedValue or DropDownList.SelectedItem.Text

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Sorry but I'm still trying to get my head around when and how to use classes.

I thought about using a class called NewIndex but, as I understand classes. I would need a New "Unique" class named "NewIndex#"

public class NewIndex
{
int id;
string name;

}

NewIndex NewIndex1 = new NewIndex;
NewIndex NewIndex2 = new NewIndex;

etc.

But I don't see how that would tie into the personnel selected in the checked listbox.

Say I have 50 Names in a checked listbox and I select 10 names. I want to get the id number of each person "checked" so I can retrieve information from a database based off the id of those selected.

I'm having a hard time seeing how I would tie selectedIndex.Value to which NewIndex#

How do I know which NewIndex to get?
 
Code:
public class NewIndex
{
  private int id;
  private string name;

  // Id Property
  public int Id
  {
    get 
    { 
       return id;
    }
  }

  // Name Property
  public string Name
  {
    get
    {
       return name;
    }
  }

  // Constructor
  public NewIndex( int id, string name )
  {
    this.id = id;
    this.name = name;
  }
}

To set up the list box:
Code:
listBox1.ValueMember = "Id";
listBox1.DisplayMember = "Name";

NewIndex ni1 = new NewIndex( 1, "ABC" );
NewIndex ni2 = new NewIndex( 2, "DEF" );

listBox1.Items.Add( ni1 );
listBox1.Items.Add( ni2 );

To get the Id of the selected customer:
Code:
int selectedId = (int)listBox1.SelectedValue;

Does that clear your questions?

|| ABC
 
>Does that clear your questions?

Not quite

I see how you are using the class for populating the listbox and that part I was missing so now that makes since.

The main problem I have is that for each "Person" placed in the listbox I need to create a unique NewIndex object.

1 or 2 NewIndex objects is no problem but what if I have 50 - 100?

I assume I can use a name based off of a variable in the "rdr.read" loop

int x = 0

NewIndex iID + i = new NewIndex(1, "abc")

I'll give that idea a try in a little while.

 
The lstPersonnel.ValueMember and the lstPersonnel.DisplayMember were not available to me.

So researching it, they are only made available if you have a bound datasource.

Finding this answers my original question because the "ValueMember" property was what I was looking for in the first place.

Understanding how to use the class in this case was a great help so have a star.

I still have not found a fix to the multiple NewIndex object question since I have not gone that far.
 
50 - 100 shouldn't be a problem either.

I suppose you would have something like the following:

Code:
rdr = cmd.ExecuteReader();
NewIndex n;
while (rdr.Read())
{
   n = new NewIndex( rdr["id"], rdr["fname"].ToString() +  ", " + rdr["lname"].ToString() );
   lstPersonnel.Items.Add( n );
}

(Provided that the NewIndex class is as described in my previous post)

Instead of having 50 - 100 strings in the listbox, you are having 50 - 100 NewIndex objects.


|| ABC
 
That's clean code, I like that. It also gives me a better understanding of using classes that I'm not finding in books.

But, what is showing up in the listbox is the namespace ("CFD_SendMail")and the class
example: "CFD_SendMail.NewIndex" for each "person" instead of their name.

I tried it both with lstPersonnel.Items.Add(n);
and
lstPersonnel.Items.Add(n.ToString());

with no change.

Again I'm thinking it because it is not a bound form which means I don't have a ValueMember and DisplayMember property to set.

Here is my code and it does loop through each record.

Code:
                while (rdr.Read())

                {
                    n = new NewIndex(Convert.ToInt32((rdr["PersonnelID"])), rdr["fname"].ToString() +  ", " + rdr["lname"].ToString());
                    lstPersonnel.Items.Add(n.ToString());

                 }
 
lstPersonnel is a listbox, right? You mustn't have a bound datasource to use the ValueMember and DisplayMember of a listbox. I've tested what I wrote in my first post here and it worked fine.

You should be able to do the following:
Code:
listBox1.ValueMember = "Id";
listBox1.DisplayMember = "Name";
where Id and Name are the name of the properties of the NewIndex class.

|| ABC
 
No, it is a checked listbox

I went ahead and bound the form to the class and tried to bind it but was not able to get it to work.

I started looking into "CheckedListbox Datasource" and found this.


According to this article you can not bind the CheckedListbox like you can a standard listbox.

from the article said:
Introduction
It seems that the .NET CheckedListBox even doesn't have primary binding facilities exists in the ListBox and ComboBox, because it must save multiple values in the database. Current article introducing an extended CheckedListBox that has a single property for setting and getting checked items, and also has binding facilities.
 
I switched it over to a standard ListBox and your method works.

Thanks for the help and clearing up some questions on how classes are used.
 
Ouch. There's the problem.

Then you should try to use the ExCheckedListBox from that site, instead of the framework's CheckedListBox.

Otherwise, you may use a Dictionary to have a mapping between the names and the IDs (similar to the way you used SortedList).

Code:
Dictionary<string, int> d = new Dictionary<string, int>()

Code:
rdr = cmd.ExecuteReader();
string name;
while (rdr.Read())
{
   name = rdr["fname"].ToString() +  ", " + rdr["lname"].ToString();
   lstPersonnel.Items.Add( name );
   d.Add( name, rdr["id"] );
}

To get the Id of a selected person:
Code:
int selectedId = d[lstPersonnel.SelectedItem];

|| ABC
 
Ah well, if you can go well with a ListBox, that's fine.
Glad to be of help.

|| ABC
 
I still want to use a checked listbox.

I'm going to try and use the ExCheckedListBox and I have a book coming on creating custom controls to see if can create a control with that feature built in.

When I used the SortedList I also had concidered the Dictionary, just opted for the SortedList.

again, Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top