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+Perm issionTank
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?
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+Perm
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)
{
}
}
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
}
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Se
Now you can do this:
messagebox.show(pt.PermTit
Correction:
PermissionTank pt = (PermissionTank )lstassignedpermissions.Se
if(pt != null)
messagebox.show(pt.PermTit
need to check pt for null otherwise you might get a null pointer exception by calling pt.PermTitle.
ASKER
Jeez man, you rock, that is exactly the solution I am looking for. Thank you very much. It works :)
ASKER
At this point I was able to put:
MessageBox.Show(lstAssigne dPermissio ns.Selecte dItem.ToSt ring());
And it returned PermTitle from the override.
MessageBox.Show(lstAssigne
And it returned PermTitle from the override.
No problem at all. I was just mulling this exact thing over yesterday. Lucky timing I suppose.
ASKER
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!
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
ASKER
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.
Open in new window