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


C# BindingList<T>类代码示例

本文整理汇总了C#中System.ComponentModel.BindingList<T>的典型用法代码示例。如果您正苦于以下问题:C# BindingList<T>类的具体用法?C# BindingList<T>怎么用?C# BindingList<T>使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: Random

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

namespace BindingListOfTExamples
{
    public partial class Form1 : Form
    {
        private TextBox textBox2;
        private ListBox listBox1;
        private Button button1;
        private TextBox textBox1;
        Random randomNumber = new Random();
    
        public Form1()
        {
           this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
           this.textBox1 = new System.Windows.Forms.TextBox();
           this.textBox2 = new System.Windows.Forms.TextBox();
           this.listBox1 = new System.Windows.Forms.ListBox();
           this.button1 = new System.Windows.Forms.Button();
           this.textBox1.Location = new System.Drawing.Point(169, 26);
           this.textBox1.Size = new System.Drawing.Size(100, 20);
           this.textBox1.Text = "Bracket";
           this.textBox2.Location = new System.Drawing.Point(169, 57);
           this.textBox2.ReadOnly = true;
           this.textBox2.Size = new System.Drawing.Size(100, 20);
           this.textBox2.Text = "4343";
           this.listBox1.FormattingEnabled = true;
           this.listBox1.Location = new System.Drawing.Point(12, 12);
           this.listBox1.Size = new System.Drawing.Size(120, 95);
           this.button1.Location = new System.Drawing.Point(180, 83);
           this.button1.Size = new System.Drawing.Size(75, 23);
           this.button1.Text = "Add New Item";
           this.button1.Click += new System.EventHandler(this.button1_Click);
           this.ClientSize = new System.Drawing.Size(292, 266);
           this.Controls.Add(this.button1);
           this.Controls.Add(this.listBox1);
           this.Controls.Add(this.textBox2);
           this.Controls.Add(this.textBox1);
           this.Text = "Parts Form";
           this.Load += new EventHandler(Form1_Load);
        }
    
        void Form1_Load(object sender, EventArgs e)
        {
            InitializeListOfParts();
            listBox1.DataSource = listOfParts;
            listBox1.DisplayMember = "PartName";
            listOfParts.AddingNew += new AddingNewEventHandler(listOfParts_AddingNew);
            listOfParts.ListChanged += new ListChangedEventHandler(listOfParts_ListChanged);
        }

        // Declare a new BindingListOfT with the Part business object.
        BindingList<Part> listOfParts; 
        private void InitializeListOfParts()
        {
            // Create the new BindingList of Part type.
            listOfParts = new BindingList<Part>();
    
            // Allow new parts to be added, but not removed once committed.        
            listOfParts.AllowNew = true;
            listOfParts.AllowRemove = false;

            // Raise ListChanged events when new parts are added.
            listOfParts.RaiseListChangedEvents = true;

            // Do not allow parts to be edited.
            listOfParts.AllowEdit = false;
            
            // Add a couple of parts to the list.
            listOfParts.Add(new Part("Widget", 1234));
            listOfParts.Add(new Part("Gadget", 5647));
        }

        // Create a new part from the text in the two text boxes.
        void listOfParts_AddingNew(object sender, AddingNewEventArgs e)
        {
            e.NewObject = new Part(textBox1.Text, int.Parse(textBox2.Text));
        }

        // Add the new part unless the part number contains
        // spaces. In that case cancel the add.
        private void button1_Click(object sender, EventArgs e)
        {
            Part newPart = listOfParts.AddNew();

            if (newPart.PartName.Contains(" "))
            {
                MessageBox.Show("Part names cannot contain spaces.");
                listOfParts.CancelNew(listOfParts.IndexOf(newPart));
            }
            else
            {
                textBox2.Text = randomNumber.Next(9999).ToString();
                textBox1.Text = "Enter part name";
            }
        }

        void listOfParts_ListChanged(object sender, ListChangedEventArgs e)
        {
            MessageBox.Show(e.ListChangedType.ToString());
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.Run(new Form1());
        }
    }
    
    // A simple business object for example purposes.
    public class Part
    {
        private string name;
        private int number;
        public Part() { }
        public Part(string nameForPart, int numberForPart)
        {
            PartName = nameForPart;
            PartNumber = numberForPart;
        }

        public string PartName
        {
            get { return name; }
            set { name = value; }
        }

        public int PartNumber
        {
            get { return number; }
            set { number = value; }
        }
    }
}
开发者ID:.NET开发者,项目名称:System.ComponentModel,代码行数:141,代码来源:BindingList


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