473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Multiselct ListBox

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()

Nov 17 '05 #1
6 1515
Hi,
In such a scenario where you have a multiselect list there is no other
option then to interate through all the list item and then check
whether it is selected or not.
SelectedIndex and SelectedValue don't work for such a scenario.

HTH,
Regards,
Angrez

Nov 17 '05 #2
Thanks Angrez, but how do I do this?

"Angrez Singh" wrote:
Hi,
In such a scenario where you have a multiselect list there is no other
option then to interate through all the list item and then check
whether it is selected or not.
SelectedIndex and SelectedValue don't work for such a scenario.

HTH,
Regards,
Angrez

Nov 17 '05 #3
Hi,

No really, ListBox support ListBox.SelectedIndices which is a collection of
the items that are selected.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"jez123456" <je*******@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
Thanks Angrez, but how do I do this?

"Angrez Singh" wrote:
Hi,
In such a scenario where you have a multiselect list there is no other
option then to interate through all the list item and then check
whether it is selected or not.
SelectedIndex and SelectedValue don't work for such a scenario.

HTH,
Regards,
Angrez

Nov 17 '05 #4
I've tried the following, but only the first selected seems to work.

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;
// Loop through all items the ListBox.
for (int x = 0; x < m_listBoxUsers.Items.Count; x++)
{
// Determine if the item is selected.
if(m_listBoxUsers.GetSelected(x) == true)
{

// Get the current user index.
//int userIndex = m_listBoxUsers.SelectedIndex;
int userIndex = x;

// 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()
"jez123456" wrote:
Thanks Angrez, but how do I do this?

"Angrez Singh" wrote:
Hi,
In such a scenario where you have a multiselect list there is no other
option then to interate through all the list item and then check
whether it is selected or not.
SelectedIndex and SelectedValue don't work for such a scenario.

HTH,
Regards,
Angrez

Nov 17 '05 #5
I've now made a bit of progess with the following code by deselecting the
first selected, however, I would like to still show which users were
originally selected. Is it possible to store this somehow before I deselect
with

m_listBoxUsers.SetSelected(x, false);


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;
// Loop through all items the ListBox.
for (int x = 0; x < m_listBoxUsers.Items.Count; x++)
{
// Determine if the item is selected.
if(m_listBoxUsers.GetSelected(x) == true)
{

// Get the current user index.
//int userIndex = m_listBoxUsers.SelectedIndex;
int userIndex = x;

// 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();

m_listBoxUsers.SetSelected(x, false);
}
}

} // End try

catch (Exception ex)
{
MessageBox.Show(ex.Message);
} // End catch

} // End m_buttonAddUserRole_Click()

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,

No really, ListBox support ListBox.SelectedIndices which is a collection of
the items that are selected.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"jez123456" <je*******@discussions.microsoft.com> wrote in message
news:32**********************************@microsof t.com...
Thanks Angrez, but how do I do this?

"Angrez Singh" wrote:
Hi,
In such a scenario where you have a multiselect list there is no other
option then to interate through all the list item and then check
whether it is selected or not.
SelectedIndex and SelectedValue don't work for such a scenario.

HTH,
Regards,
Angrez


Nov 17 '05 #6
Hi Ignacio,

Actually that is true if you are developing a Windows application but
will not work for web applications. But by checking the status of each
item will apply to both the world.

Regards,
Angrez

Nov 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: amber | last post by:
Hello. Can someone tell me what I may be doing wrong here? I'm using the code (lboxRP is a listbox): Dim newRPindex As Integer newRPindex = Me.lboxRP.FindString(RP)...
9
by: Megan | last post by:
Hi- I'm creating a database of music bands with their cds and songs. I'm trying to program an SQL statement so that I can enter a string of text in a textbox, press the 'Enter' key, and have...
3
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice should apear automatically next to the product name...
8
by: Oddball | last post by:
Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler. What I want to draw as the item is another control. I have created a user control that I would like to list in...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
7
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've looked for over a week now and can't find anything but...
3
by: Ali Chambers | last post by:
Hi, I have created a listbox called "dtlist1" on my VB.NET form. I call a procedure as follows: Private Sub openfile(flname As String) dtlist1.Items.Clear() etc..
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
5
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.