Link to home
Start Free TrialLog in
Avatar of ProWebNetworks
ProWebNetworks

asked on

Load Listbox in C# with an Object Class

C# Windows Form Application

I am working with some code that is "supposed" to load a listbox with an object class that I created. When I load the listbox it displays:

SJRDB_C.frmModifyUser+PermissionTank

Instead of listing the values of the property I set as the DisplayMember. Can someone please see my code below and tell me what I am doing wrong?
private void LoadAssignedPermissions()
        {
            SqlConnection cn = new SqlConnection(PubVar.dbConn);
            cn.Open();
 
            SqlCommand cmd = new SqlCommand(PubVar.dbConn, cn);
            cmd.CommandText = "sp_LookupPermissionsByUserID";
            cmd.CommandType = CommandType.StoredProcedure;
 
 
            SqlParameter param;
            param = cmd.Parameters.AddWithValue("@UserID", PubVar.UserIDToModify);
 
            DataTable tbl = new DataTable();
            SqlDataReader rdr = cmd.ExecuteReader();
 
       
 
 
 
            while (rdr.Read())
            {
             
 
                lstAssignedPermissions.Items.Add(new PermissionTank(rdr["PermissionID"].ToString(),rdr["PermissionTitle"].ToString()));
            }
            
            
 
            cn.Close();
            cmd.Dispose();
        }
 
        public class PermissionTank
        {
 
 
            public PermissionTank(string PermissionID, string PermissionTitle)
            {
 
            }
 
 
        }

Open in new window

Avatar of ProWebNetworks
ProWebNetworks

ASKER

I modified my class to what is shown below, and now it works - can someone tell me how to:

messagebox.show( the valuemember for the listbox )

I will be using that value member for another stored procedure. Also my class may be convoluted, if you see where it can be reduced - cheers.

Thanks.
       public  class PermissionTank
        {
            string pID="";
            string pTitle="";
 
            public string PermID
            {
                get { return pID; } 
            }
            public string PermTitle 
        {
            get { return pTitle; }
        }
 
            public  PermissionTank(string PermissionID, string PermissionTitle)
            {
                pID = PermissionID;
                pTitle = PermissionTitle;
            }

Open in new window


Your class PermissionTank was override the ToString method. Then the listbox will display whatever you want by calling the ToString method on the selected object in the ListBox.

Add this code to your class:


public override string ToString()
        {
            return //property to display in listbox
        }
I don't think I quite understand. I am novice. I got the listbox to finally display what I would like to have displayed. I am not able to:

messagebox.show( lstassignedpermissions. "selectedvalue??" ) - I need to retrieve the value of the selected item. Every time I do the message box now - it shows the name of the object and not the value.

Can you explain further where I should put the
public override string ToString()
        {
            return //property to display in listbox
        }

and how it will help me?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of baijajusav
baijajusav

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Missed answering part of your question.

In order to get a reference to the object that is currently selected do this:

PermissionTank pt = (PermissionTank )lstassignedpermissions.SelectedValue;

Now you can do this:

messagebox.show(pt.PermTitle   );


Correction:

PermissionTank pt = (PermissionTank )lstassignedpermissions.SelectedValue;
if(pt != null)
messagebox.show(pt.PermTitle   );

need to check pt for null otherwise you might get a null pointer exception by calling pt.PermTitle.
Jeez man, you rock, that is exactly the solution I am looking for. Thank you very much. It works :)
At this point I was able to put:

MessageBox.Show(lstAssignedPermissions.SelectedItem.ToString());

And it returned PermTitle from the override.

No problem at all. I was just mulling this exact thing over yesterday. Lucky timing I suppose.
LOL I spent the last 4 hours trying to get this thing figured out - it was really starting to get to me. I know enough to get the job semi-done. I am not real hip on all the foundation behind "why" everything works - just simply that things work the way they do.

This is only my 3rd week messing with this language, I figure by the time I am 30-40 weeks in I should be "better".

Thank you again for the help - it was great!

No problem man. I'm trolling the area now so if you've got any more questions, I have a point quota to meet so I don't have to pay these peeps. LOL