请问c#的ListBox的“全部选定”怎么做?
还有一个问题,怎么让ListBox的ContextMenu弹出来之前选定当时鼠标选定的那项,如果已经被选定,则不要重新选定了。
我是初学者,请高手指点一下,谢谢。

解决方案 »

  1.   

    麻烦一点,先设置多选
    this.listBox1.SelectionMode = SelectionMode.MultiSimple;
    再做循环
    this.listBox1.SetSelected(i,true);
      

  2.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using it175_c.Admin.Common;
    namespace it175_c.Admin.Member
    {
     /// <summary>
     /// Member_ModflyDist 的摘要说明。
     /// </summary>
     public class Member_ModifyDist : System.Web.UI.Page
     {
      protected System.Web.UI.WebControls.Button btnAdd;
      protected System.Web.UI.WebControls.Button btnAddAll;
      protected System.Web.UI.WebControls.Button btnRem;
      protected System.Web.UI.WebControls.Button btnRemAll;
      protected System.Web.UI.WebControls.Label lblInfo;
      protected System.Web.UI.WebControls.ImageButton ibtnSave;
      protected System.Web.UI.WebControls.ImageButton ibtnCancel;
      protected System.Web.UI.WebControls.Label lblName;  protected System.Web.UI.WebControls.ListBox ltbDistScr;
      protected System.Web.UI.WebControls.ListBox ltbDist;
      /// <summary>
      /// 名称:Update
      /// 依赖关系:it175_c.Admin.Common
      /// 目的:保存对用户权限的修改
      /// 注释:增加了权限不能为空的检验。但是还存在如下问题:
      ///       1.数据保存使用的方法是不是还可以改进,可否是用一个循环的sql实现?而不是频繁的发送sql
      ///       2.如何保证数据的回滚?
      ///       3.先删除该用户的所有权限,然后再添加修改后的权限,这种思路是不是正确?还能不能改进?
      /// </summary>
      private void Update()
      {
       //根据传递过来的username,查询出该用户的Pkid
       string UserName = Request.Params.Get("UserName");
       CommonClass comP = new CommonClass();
       string strSqlPkid = "SELECT Pkid,Name FROM tbl_Admin WHERE UserName = '" + UserName + "'";
       DataSet dbsPkid = comP.GetDataSet(strSqlPkid);
       string strPkid = dbsPkid.Tables[0].Rows[0][0].ToString();
       string strUserID = strPkid;   //首先删除该用户的所有权限
       if(ltbDist.Items.Count > 0)
       {
        CommonClass comDel = new CommonClass();
        string strSqlDel = "DELETE FROM tbl_DistinctionDict WHERE UserID = '" + strUserID + "'";
        bool bDel = comDel.UpdateData(strSqlDel);    //为该用户设置权限
        if(bDel)
        {
         CommonClass comInsert = new CommonClass();
         foreach(ListItem li in ltbDist.Items)
         {
          string strDistinctionID = li.Value.ToString().Trim();
          string strSql = "INSERT INTO tbl_DistinctionDict (UserID,DistinctionID) VALUES ('" + strUserID + "','" + strDistinctionID + " ')";
          bool b = comInsert.UpdateData(strSql);
          if(b)
          {
           CommonClass comR = new CommonClass();
           comR.RedirectParentWind(this.Page);
          }
          else
          {
           lblInfo.Text = "数据保存失败,该用户权限被清空,请联系技术部!";
          }
         }
        }
        else
        {
         lblInfo.Text = "数据保存失败,请重试或联系技术部!";
        }
       }
       else
       {
        lblInfo.Text = "每个用户最少有一个权限!";
       }
      }  /// <summary>
      /// 名称:FillltbDistScr
      /// 依赖关系:it175_c.Admin.Common
      /// 目的:填充左边的ListBox,其内容为该用户所不具有的权限  
      /// 注释:添加了排除功能,由原来的现实所有权限改进为过滤掉当前用户已经有的权限
      /// </summary>
      private void FillltbDistScr()
      {
       string strUserName = Request.Params.Get("UserName");
       CommonClass comFill = new CommonClass();
       string strSql = "SELECT DistinctionName,Pkid FROM tbl_Distinction WHERE Pkid NOT IN(SELECT d.Pkid FROM tbl_Admin AS a,tbl_Distinction AS d,tbl_DistinctionDict AS dd WHERE a.Pkid = dd.UserID AND d.Pkid = dd.DistinctionID AND a.UserName ='" + strUserName + "')";
       DataSet dbsFill = comFill.GetDataSet(strSql);
       for(int i = 0;i <= dbsFill.Tables[0].Rows.Count-1;i++)
       {
        ltbDistScr.Items.Add(new ListItem(dbsFill.Tables[0].Rows[i][0].ToString(),dbsFill.Tables[0].Rows[i][1].ToString()));
       }
      }
      /// <summary>
      /// 名称:FillltbDist
      /// 依赖关系:it175_c.Admin.Common
      /// 目的:填充右边的ListBox,其内容为当前用户所具有的权限
      /// 注释:
      /// </summary>
      private void FillltbDist()
      {
       string strUserName = Request.Params.Get("UserName");
       string strSql = "SELECT d.DistinctionName,d.Pkid FROM tbl_Admin AS a,tbl_Distinction AS d,tbl_DistinctionDict AS dd WHERE a.Pkid = dd.UserID AND d.Pkid = dd.DistinctionID AND a.UserName = '" + strUserName + "'";
       CommonClass comFill = new CommonClass();
       DataSet dbsFill = comFill.GetDataSet(strSql);
       for(int i=0;i <= dbsFill.Tables[0].Rows.Count-1;i++)
       {
        ltbDist.Items.Add(new ListItem(dbsFill.Tables[0].Rows[i][0].ToString(),dbsFill.Tables[0].Rows[i][1].ToString()));
       }
      }
      

  3.   

    /// <summary>
      /// 名称:Add
      /// 依赖关系:it175_c.Admin.Common
      /// 目的:把左边的移动到右边
      /// 注释:开始是一次只能移动一条,曾经考虑过使用foreach遍历整个集合,把selected == true的复制到右边,然后删除自己
      ///       问题是:删除一个以后,枚举对象发生了变化。后来的思路是:用foreach先复制过去,同时用数组纪录那个被复制了,然后
      ///       用for循环,一次删除已经复制的对象…………2:00的时候,终于想到办法,哈哈
      /// </summary>
      private void Add()
      {
       while(ltbDistScr.SelectedIndex > -1)
       {
         ltbDist.Items.Add(new ListItem(ltbDistScr.SelectedItem.Text,ltbDistScr.SelectedItem.Value));
         ltbDistScr.Items.Remove(ltbDistScr.SelectedItem);
       }
      }
      /// <summary>
      /// 名称:Rem
      /// 依赖关系:it175_c.Admin.Common
      /// 注释:与上边同理
      /// </summary>
      private void Rem()
      {
       while(ltbDist.SelectedIndex > -1)
       {
        ltbDistScr.Items.Add(new ListItem(ltbDist.SelectedItem.Text,ltbDist.SelectedItem.Value));
        ltbDist.Items.Remove(ltbDist.SelectedItem);
       }
      }
      /// <summary>
      /// 名称:AddAll
      /// 依赖关系:it175_c.Admin.Common
      /// 目的:把左边的全部移动到右边
      /// </summary>
      private void AddAll()
      {
       foreach(ListItem li in ltbDistScr.Items)
       {
        ltbDist.Items.Add(new ListItem(li.Text,li.Value));
       }
       ltbDistScr.Items.Clear();
      }
      /// <summary>
      /// 名称:RemAll
      /// 依赖关系:it175_c.Admin.Common
      /// 目的:把右边的全部移动到左边
      /// 注释:
      /// </summary>
      private void RemAll()
      {
       foreach(ListItem li in ltbDist.Items)
       {
        ltbDistScr.Items.Add(new ListItem(li.Text,li.Value));
       }
       ltbDist.Items.Clear();
      }   private void Page_Load(object sender, System.EventArgs e)
      {
       // 在此处放置用户代码以初始化页面
       if(!this.IsPostBack)
       {
        FillltbDistScr();
        FillltbDist();
        CommonClass comUserName = new CommonClass();
        string strSql = "SELECT Name FROM tbl_Admin WHERE UserName = '" + Request.Params.Get("UserName").Trim().ToString() + "'";
        DataSet dbsU = comUserName.GetDataSet(strSql);
        lblName.Text = Request.Params.Get("UserName").Trim() + "(" + dbsU.Tables[0].Rows[0][0].ToString() + ")";
        lblInfo.Text = "(按住Ctrl或Shift可以多选)";
       }
      }  #region Web 窗体设计器生成的代码
      override protected void OnInit(EventArgs e)
      {
       //
       // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
       //
       InitializeComponent();
       base.OnInit(e);
      }
      
      /// <summary>
      /// 设计器支持所需的方法 - 不要使用代码编辑器修改
      /// 此方法的内容。
      /// </summary>
      private void InitializeComponent()
      {    
       this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
       this.btnAddAll.Click += new System.EventHandler(this.btnAddAll_Click);
       this.btnRem.Click += new System.EventHandler(this.btnRem_Click);
       this.btnRemAll.Click += new System.EventHandler(this.btnRemAll_Click);
       this.ibtnSave.Click += new System.Web.UI.ImageClickEventHandler(this.ibtnSave_Click);
       this.ibtnCancel.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
       this.Load += new System.EventHandler(this.Page_Load);  }
      #endregion  private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
      {
       CommonClass comR = new CommonClass();
       comR.RedirectParentWind(this.Page);
      }  private void btnAdd_Click(object sender, System.EventArgs e)
      {
       Add();
      }  private void btnRem_Click(object sender, System.EventArgs e)
      {
       Rem();
      }  private void btnAddAll_Click(object sender, System.EventArgs e)
      {
       AddAll();
      }  private void btnRemAll_Click(object sender, System.EventArgs e)
      {
       RemAll();
      }  private void ibtnSave_Click(object sender, System.Web.UI.ImageClickEventArgs e)
      {
       Update();
      }
     }
    }
      

  4.   

    <%@ Page language="c#" Codebehind="Member_ModifyDist.aspx.cs" AutoEventWireup="false" Inherits="it175_c.Admin.Member.Member_ModifyDist" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
     <HEAD>
      <title>Member_ModflyDist</title>
      <LINK href="../common/styles.css" type="text/css" rel="Stylesheet">
     </HEAD>
     <body bottomMargin="0" bgColor="#d4d0c8" leftMargin="0" topMargin="0" rightMargin="0">
      <form id="Form1" method="post" runat="server">
       <FONT face="宋体">
        <TABLE id="Table1" cellSpacing="0" cellPadding="0" width="100%" border="0">
         <TR>
          <TD width="7" height="18"></TD>
          <TD height="18"></TD>
          <TD width="7" height="18"></TD>
         </TR>
         <TR>
          <TD></TD>
          <TD><fieldset><legend>
             <asp:Label id="lblName" runat="server"></asp:Label>的权限修改</legend>
            <TABLE id="Table2" cellSpacing="1" cellPadding="1" width="100%" border="0">
             <TR>
              <TD align="center">未添加的权限</TD>
              <TD width="23"></TD>
              <TD align="center">已添加的权限</TD>
             </TR>
             <TR>
              <TD vAlign="middle" align="center">
               <asp:ListBox id="ltbDistScr" runat="server" Rows="12" SelectionMode="Multiple"></asp:ListBox></TD>
              <TD vAlign="middle" align="center" width="23">
               <asp:Button id="btnAdd" runat="server" Text="->" Width="23px" title="添加"></asp:Button><BR>
               <asp:Button id="btnAddAll" runat="server" Text=">>" Width="23px" title="添加所有"></asp:Button><BR>
               <asp:Button id="btnRem" runat="server" Text="<-" Width="23px" title="移除"></asp:Button><BR>
               <asp:Button id="btnRemAll" runat="server" Text="<<" Width="23px" title="移除所有"></asp:Button></TD>
              <TD vAlign="middle" align="center">
               <asp:ListBox id="ltbDist" runat="server" Rows="12" SelectionMode="Multiple"></asp:ListBox></TD>
             </TR>
             <TR>
              <TD align="center" colSpan="3">
               <asp:Label id="lblInfo" runat="server" ForeColor="Red"></asp:Label></TD>
             </TR>
            </TABLE>
           </fieldset>
          </TD>
          <TD></TD>
         </TR>
         <TR>
          <TD></TD>
          <TD align="center"><BR>
           <asp:ImageButton id="ibtnSave" runat="server" ImageUrl="/it175_c/Admin/Images/Button_Save.gif" title="保存修改"></asp:ImageButton>&nbsp;
           <asp:ImageButton id="ibtnCancel" runat="server" ImageUrl="/it175_c/Admin/Images/Button_Cancel.gif" title="放弃修改"></asp:ImageButton></TD>
          <TD></TD>
         </TR>
        </TABLE>
       </FONT>
      </form>
     </body>
    </HTML>
      

  5.   

    for(int i=0;i<this.CheckBoxList1.Items.Count;i++)
    {
       CheckBoxList1.Items[i].Selected;
    }
      

  6.   

    this.Lb_sourse.SelectionMode = ListSelectionMode.Multiple;
    for (int i = 0;i<this.Lb_sourse.Items.Count;i++)
    {
        this.Lb_sourse.Items[i].Selected = true;
    }