zoukankan      html  css  js  c++  java
  • [C#][SAMPLE][CODE][Control]ListBox和CheckListBox

    [示例出处]:本示例来自《C#入门经典》第三版中文版,P349-P353
    [示例涉及]:
    1、ListBox
    2、CheckListBox
    [示例代码]:2文件(其余默认)
    Form1.Designer.cs
     1namespace WA_Lists
     2{
     3    partial class Form1
     4    {
     5        /// <summary>
     6        /// 必需的设计器变量。
     7        /// </summary>

     8        private System.ComponentModel.IContainer components = null;
     9
    10        /// <summary>
    11        /// 清理所有正在使用的资源。
    12        /// </summary>
    13        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>

    14        protected override void Dispose(bool disposing)
    15        {
    16            if (disposing && (components != null))
    17            {
    18                components.Dispose();
    19            }

    20            base.Dispose(disposing);
    21        }

    22
    23        Windows 窗体设计器生成的代码
    90
    91        private System.Windows.Forms.ListBox listBoxSelected;
    92        private System.Windows.Forms.CheckedListBox checkedListBoxPossibleValue;
    93        private System.Windows.Forms.Button buttonMove;
    94    }

    95}

    96
    97

    Form1.cs
     1using System;
     2using System.Collections.Generic;
     3using System.ComponentModel;
     4using System.Data;
     5using System.Drawing;
     6using System.Text;
     7using System.Windows.Forms;
     8
     9namespace WA_Lists
    10{
    11    public partial class Form1 : Form
    12    {
    13        public Form1()
    14        {
    15            InitializeComponent();
    16            this.checkedListBoxPossibleValue.Items.Add("Eleven");
    17            this.checkedListBoxPossibleValue.Items.AddRange(new object[] {
    18            "beautiful",
    19            "girl",
    20            "string",
    21            "object"}
    );
    22            this.checkedListBoxPossibleValue.SetItemChecked(0true);   //设置初始打勾的选项
    23            this.checkedListBoxPossibleValue.SetItemChecked(3true);   //设置初始打勾的选项
    24            this.checkedListBoxPossibleValue.SetItemChecked(6true);   //设置初始打勾的选项
    25            //否则要点两次才会打上勾,默认:第一次Click为选中,第二次Click为打勾/取消打勾
    26        }

    27
    28        private void buttonMove_Click(object sender, EventArgs e)
    29        {
    30            //检查是否在CheckListBox中有任何选中的选项
    31            this.listBoxSelected.Items.Clear();
    32
    33            //循环在CheckListBox中选中的选项,把它们添加到ListBoxSelected中
    34            foreach (string item in this.checkedListBoxPossibleValue.CheckedItems)
    35            {
    36                this.listBoxSelected.Items.Add(item.ToString());
    37            }

    38
    39            //清除所有在CheckListBox中选中的选项
    40            for (int i = 0; i < this.checkedListBoxPossibleValue.Items.Count; i++)
    41            {
    42                this.checkedListBoxPossibleValue.SetItemChecked(i, false);
    43            }

    44        }

    45    }

    46}


    [示例说明]:
    1、开发语言:C#
    2、开发环境:Visual Studio.Net 2005 Team suite
    3、开发模板:C#.net项目->Windows应用程序
  • 相关阅读:
    session绑定javaBean
    Some code changes cannot be hot swapped into a running virtual machine,
    抓包及WireShark工具介绍
    Jquery选择器特殊字符问题
    win7,win8 64位 VS2010/VS2013调试报错
    win7x64 连接oracle 客户端 VS2010调试 提示ORA-12154 TNS
    WebService本地VS运行正常,发布到IIS异常
    SQL语句增、删、改
    vb 去掉html中的回车和tab;转换部分html大写标签为小写
    C#语句——循环语句(for循环与for循环嵌套)
  • 原文地址:https://www.cnblogs.com/volnet/p/575600.html
Copyright © 2011-2022 走看看