Multiselct ListBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jez123456

    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_listBoxUserRo les – 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_listBoxUserRo les 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_buttonAddUser Role_Click(obje ct sender, System.EventArg s 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_listBo xUsers.Selected Value;
    int roleID = (Int32)m_roleTa ble.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(use rID).Tables[0];

    // Bind the GUI to the table.
    m_listBoxUserRo les.DataSource = m_userRoleTable ;
    m_listBoxUserRo les.DisplayMemb er = "role_name" ;

    _UpdateGUI();

    } // End try

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

    } // End m_buttonAddUser Role_Click()

  • Angrez Singh

    #2
    Re: Multiselct ListBox

    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

    Comment

    • jez123456

      #3
      Re: Multiselct ListBox

      Thanks Angrez, but how do I do this?

      "Angrez Singh" wrote:
      [color=blue]
      > 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
      >
      >[/color]

      Comment

      • Ignacio Machin \( .NET/ C# MVP \)

        #4
        Re: Multiselct ListBox

        Hi,

        No really, ListBox support ListBox.Selecte dIndices 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" <jez123456@disc ussions.microso ft.com> wrote in message
        news:324CA02C-3C0E-4C9E-8AB2-67897D3D8568@mi crosoft.com...[color=blue]
        > Thanks Angrez, but how do I do this?
        >
        > "Angrez Singh" wrote:
        >[color=green]
        >> 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
        >>
        >>[/color][/color]


        Comment

        • jez123456

          #5
          Re: Multiselct ListBox

          I've tried the following, but only the first selected seems to work.

          private void m_buttonAddUser Role_Click(obje ct sender, System.EventArg s 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_listBoxUse rs.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_listBo xUsers.Selected Value;
          int roleID = (Int32)m_roleTa ble.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(use rID).Tables[0];

          // Bind the GUI to the table.
          m_listBoxUserRo les.DataSource = m_userRoleTable ;
          m_listBoxUserRo les.DisplayMemb er = "role_name" ;

          _UpdateGUI();
          }
          }

          } // End try

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

          } // End m_buttonAddUser Role_Click()


          "jez123456" wrote:
          [color=blue]
          > Thanks Angrez, but how do I do this?
          >
          > "Angrez Singh" wrote:
          >[color=green]
          > > 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
          > >
          > >[/color][/color]

          Comment

          • jez123456

            #6
            Re: Multiselct ListBox

            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_buttonAddUser Role_Click(obje ct sender, System.EventArg s 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_listBoxUse rs.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_listBo xUsers.Selected Value;
            int roleID = (Int32)m_roleTa ble.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(use rID).Tables[0];

            // Bind the GUI to the table.
            m_listBoxUserRo les.DataSource = m_userRoleTable ;
            m_listBoxUserRo les.DisplayMemb er = "role_name" ;

            _UpdateGUI();

            m_listBoxUsers. SetSelected(x, false);
            }
            }

            } // End try

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

            } // End m_buttonAddUser Role_Click()

            "Ignacio Machin ( .NET/ C# MVP )" wrote:
            [color=blue]
            > Hi,
            >
            > No really, ListBox support ListBox.Selecte dIndices 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" <jez123456@disc ussions.microso ft.com> wrote in message
            > news:324CA02C-3C0E-4C9E-8AB2-67897D3D8568@mi crosoft.com...[color=green]
            > > Thanks Angrez, but how do I do this?
            > >
            > > "Angrez Singh" wrote:
            > >[color=darkred]
            > >> 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
            > >>
            > >>[/color][/color]
            >
            >
            >[/color]

            Comment

            • Angrez Singh

              #7
              Re: Multiselct ListBox

              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

              Comment

              Working...