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


C# ListControl.DataSource属性代码示例

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


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

示例1: ListBox

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

namespace MyListControlSample
{
    public class ListBoxSample3 : Form
    {
        private ListBox ListBox1 = new ListBox();
        private Label label1 = new Label();
        private TextBox textBox1 = new TextBox();

        [STAThread]
        static void Main()
        {
            Application.Run(new ListBoxSample3());
        }

        public ListBoxSample3()
        {
            this.ClientSize = new Size(307, 206);
            this.Text = "ListBox Sample3";

            ListBox1.Location = new Point(54, 16);
            ListBox1.Name = "ListBox1";
            ListBox1.Size = new Size(240, 130);

            label1.Location = new Point(14, 150);
            label1.Name = "label1";
            label1.Size = new Size(40, 24);
            label1.Text = "Value";

            textBox1.Location = new Point(54, 150);
            textBox1.Name = "textBox1";
            textBox1.Size = new Size(240, 24);
            
            this.Controls.AddRange(new Control[] { ListBox1, label1, textBox1 });

            // Populate the list box using an array as DataSource.
            ArrayList USStates = new ArrayList();
            USStates.Add(new USState("Alabama", "AL"));
            USStates.Add(new USState("Washington", "WA"));
            USStates.Add(new USState("West Virginia", "WV"));
            USStates.Add(new USState("Wisconsin", "WI"));
            USStates.Add(new USState("Wyoming", "WY"));
            ListBox1.DataSource = USStates;

            // Set the long name as the property to be displayed and the short
            // name as the value to be returned when a row is selected.  Here
            // these are properties; if we were binding to a database table or
            // query these could be column names.
            ListBox1.DisplayMember = "LongName";
            ListBox1.ValueMember = "ShortName";

            // Bind the SelectedValueChanged event to our handler for it.
            ListBox1.SelectedValueChanged += 
                new EventHandler(ListBox1_SelectedValueChanged);

            // Ensure the form opens with no rows selected.
            ListBox1.ClearSelected();
        }

        private void InitializeComponent()
        {
        }

        private void ListBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            if (ListBox1.SelectedIndex != -1)
            {
                textBox1.Text = ListBox1.SelectedValue.ToString();
                // If we also wanted to get the displayed text we could use
                // the SelectedItem item property:
                // string s = ((USState)ListBox1.SelectedItem).LongName;
            }
        }
    }

    public class USState
    {
        private string myShortName;
        private string myLongName;

        public USState(string strLongName, string strShortName)
        {

            this.myShortName = strShortName;
            this.myLongName = strLongName;
        }

        public string ShortName
        {
            get
            {
                return myShortName;
            }
        }

        public string LongName
        {

            get
            {
                return myLongName;
            }
        }
    }
}
开发者ID:.NET开发者,项目名称:System.Windows.Forms,代码行数:110,代码来源:ListControl.DataSource


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