Hi I have a windows form with 3 ListBoxes.
m_listBoxUsers – this displays all users from a database.
m_listBoxRoles – this displays available roles that can be assigned to users.
m_listBoxUserRoles – this displays the role assigned to a selected user.
When I select a user in m_listBoxUsers and a role in m_listBoxRoles, I can
assign the selected role to the user with the following code. The role is
then added to m_listBoxUserRoles for that user.
This all works for a single selected user. I need to now extend this so that
a selected role from m_listBoxRoles can be assigned to multiple users at the
same time. I’ve changed the m_listBoxUsers SelectionMode property to
MultiExtended, but I can’t get the code to loop through the users.
private void m_buttonAddUserRole_Click(object sender, System.EventArgs e)
{
try
{
// Get the current role index .
int roleIndex = m_listBoxRoles.SelectedIndex;
// Should we ignore the the event?
if (roleIndex == -1)
return;
// Get the current user index.
int userIndex = m_listBoxUsers.SelectedIndex;
// Should we ignore the the event?
if (userIndex == -1)
return;
// Get the identifiers.
m_listBoxUsers.ValueMember = "user_id" ;
int userID = (Int32)m_listBoxUsers.SelectedValue;
int roleID = (Int32)m_roleTable.Rows[roleIndex]["role_id"];
// Search for an existing row.
DataRow[] rows = m_userRoleTable.Select(
"role_id = " + roleID + " AND user_id = " + userID
);
// Is there already an association?
if (rows.Length > 0)
return;
// Create a new association.
UserRoleManager.Create(userID, roleID);
// Get the roles for the user.
m_userRoleTable = UserRoleManager.FindByUser(userID).Tables[0];
// Bind the GUI to the table.
m_listBoxUserRoles.DataSource = m_userRoleTable;
m_listBoxUserRoles.DisplayMember = "role_name";
_UpdateGUI();
} // End try
catch (Exception ex)
{
MessageBox.Show(ex.Message);
} // End catch
} // End m_buttonAddUserRole_Click()