当前位置: 首页>>代码示例>>C#>>正文


C# ListBox.SelectionMode属性代码示例

本文整理汇总了C#中System.Windows.Forms.ListBox.SelectionMode属性的典型用法代码示例。如果您正苦于以下问题:C# ListBox.SelectionMode属性的具体用法?C# ListBox.SelectionMode怎么用?C# ListBox.SelectionMode使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在System.Windows.Forms.ListBox的用法示例。


在下文中一共展示了ListBox.SelectionMode属性的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InitializeMyListBox

private void InitializeMyListBox()
{
   // Add items to the ListBox.
   listBox1.Items.Add("A");
   listBox1.Items.Add("C");
   listBox1.Items.Add("E");
   listBox1.Items.Add("F");
   listBox1.Items.Add("G");
   listBox1.Items.Add("D");
   listBox1.Items.Add("B");

   // Sort all items added previously.
   listBox1.Sorted = true;

   // Set the SelectionMode to select multiple items.
   listBox1.SelectionMode = SelectionMode.MultiExtended;

   // Select three initial items from the list.
   listBox1.SetSelected(0,true);
   listBox1.SetSelected(2,true);
   listBox1.SetSelected(4,true);

   // Force the ListBox to scroll back to the top of the list.
   listBox1.TopIndex=0;
}

private void InvertMySelection()
{
   // Loop through all items the ListBox.
   for (int x = 0; x < listBox1.Items.Count; x++)
   {
      // Determine if the item is selected.
      if(listBox1.GetSelected(x) == true)
         // Deselect all items that are selected.
         listBox1.SetSelected(x,false);      
      else
         // Select all items that are not selected.
         listBox1.SetSelected(x,true);
   }
   // Force the ListBox to scroll back to the top of the list.
   listBox1.TopIndex=0;
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:42,代码来源:ListBox.SelectionMode

示例2: ListBoxSelectionMode

//引入命名空间
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

public class ListBoxSelectionMode : Form
{
  ListBox lb;
  RadioButton rdoMultiExtended;
  RadioButton rdoMultiSimple;
  RadioButton rdoMultiOne;
  TextBox txtTop;
  Button btnTop;

  public ListBoxSelectionMode()
  {
    int xSize, ySize;

    Size = new Size(300,400);

    lb = new ListBox();
    lb.Parent = this;
    lb.Location = new Point(10,10);
    lb.Size = new Size(ClientSize.Width - 20, Height - 200);
    lb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
    lb.BorderStyle = BorderStyle.Fixed3D;
    lb.MultiColumn = true;
    lb.ScrollAlwaysVisible = true;

    GroupBox grpMulti = new GroupBox();
    grpMulti.Parent = this;
    grpMulti.Text = "MultiSelect";
    grpMulti.Location = new Point(lb.Left, lb.Bottom + 25);
    grpMulti.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;

    rdoMultiOne = new RadioButton();
    rdoMultiOne.Parent = grpMulti;
    rdoMultiOne.Text = "One";
    rdoMultiOne.Tag = SelectionMode.One;
    rdoMultiOne.Checked = true;
    rdoMultiOne.Location = new Point(10,15);
    rdoMultiOne.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);

    rdoMultiSimple = new RadioButton();
    rdoMultiSimple.Parent = grpMulti;
    rdoMultiSimple.Text = "Multi-Simple";
    rdoMultiSimple.Tag = SelectionMode.MultiSimple;
    rdoMultiSimple.Location = new Point(10, rdoMultiOne.Bottom);
    rdoMultiSimple.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);

    rdoMultiExtended = new RadioButton();
    rdoMultiExtended.Parent = grpMulti;
    rdoMultiExtended.Text = "Multi-Extended";
    rdoMultiExtended.Tag = SelectionMode.MultiExtended;
    rdoMultiExtended.Location = new Point(10, rdoMultiSimple.Bottom);
    rdoMultiExtended.CheckedChanged += new System.EventHandler(rdoMulti_CheckedChanged);

    xSize = (int)(Font.Height * .75) * rdoMultiExtended.Text.Length;
    ySize = ((int)rdoMultiOne.Height * 3) + 20;
    grpMulti.Size = new Size(xSize, ySize);

    Panel pnlTop = new Panel();
    pnlTop.Parent = this;
    pnlTop.Location = new Point(lb.Left, grpMulti.Bottom + 10);
    pnlTop.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;

    Label lblTop = new Label();
    lblTop.Parent = pnlTop;
    lblTop.Text = "TopIndex: ";
    xSize = ((int)(Font.Height * .5) * lblTop.Text.Length);
    lblTop.Size = new Size(xSize, Font.Height + 10);

    txtTop = new TextBox();
    txtTop.Parent = pnlTop;
    txtTop.Location = new Point(lblTop.Right, lblTop.Top);
    txtTop.Text = lb.TopIndex.ToString();
    txtTop.Size = new Size((int)(Font.Height * .75) * 3, 
                Font.Height + 10);

    btnTop = new Button();
    btnTop.Parent = pnlTop;
    btnTop.Text = "Update";
    btnTop.Location = new Point(txtTop.Right + 10, txtTop.Top);
    btnTop.Click += new System.EventHandler(btnTop_Click);


    lb.Items.Add("12345");
      lb.Items.Add("67890");      
      lb.Items.Add("7890");      
      lb.Items.Add("890");            
  } 

  static void Main() 
  {
    Application.Run(new ListBoxSelectionMode());
  }

  private void rdoMulti_CheckedChanged(object sender, EventArgs e)
  {
    RadioButton rdo = (RadioButton)sender;
    lb.SelectionMode = (SelectionMode)rdo.Tag;
  }

  private void btnTop_Click(object sender, EventArgs e)
  {
    txtTop.Text = lb.TopIndex.ToString();
  }
}
开发者ID:C#程序员,项目名称:System.Windows.Forms,代码行数:110,代码来源:ListBox.SelectionMode


注:本文中的System.Windows.Forms.ListBox.SelectionMode属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。