• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

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

0
ProWebNetworks
Asked:
ProWebNetworks
  • 6
  • 5
1 Solution
 
ProWebNetworksAuthor Commented:
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

0
 
baijajusavCommented:

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
        }
0
 
ProWebNetworksAuthor Commented:
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.
0
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
baijajusavCommented:

I have modified your class. See the change below.

What happens is a ListBox contains a collection of Objects via the ListBox.Items property. The listbox doesn't know anything about your object. I was messing with this yesterday and I found the easiest way to tell the ListBox what to display for each Object you've added to it is to override the ToString() method. Remember that all classes inherit from the base class Object (behind the scenes). Object defines a method called ToString(). By default this method will simply return the name of your class; however, you can override this method and provide a new implementation. This is what we're doing by overriding the ToString() method.
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;
            }
 
public override string ToString()
        {
            return PermTitle ;
        }
}

Open in new window

0
 
baijajusavCommented:

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   );
0
 
baijajusavCommented:


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.
0
 
ProWebNetworksAuthor Commented:
Jeez man, you rock, that is exactly the solution I am looking for. Thank you very much. It works :)
0
 
ProWebNetworksAuthor Commented:
At this point I was able to put:

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

And it returned PermTitle from the override.
0
 
baijajusavCommented:

No problem at all. I was just mulling this exact thing over yesterday. Lucky timing I suppose.
0
 
ProWebNetworksAuthor Commented:
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!
0
 
baijajusavCommented:

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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now