天天看点

C#的自定义控件

C#的UserControl让用户自定义控件容易了很多,创建WindowsFormsControlLibrary工程,自定义UserControl,生成的.dll加入到任何新工程,就能在新工程中使用自定义的UserControl。

这里主要以一个例子大致记录如何使用C#里的自定义控件。

1. 新建一个WindowsFormsControlLibrary工程,以VS2010为例。

C#的自定义控件
C#的自定义控件

VS已经为我们生成了一个UserControl1.cs。

UserControl1.cs有两种打开方式,一种是View Code进行Code编辑,另一种是View Designer进行界面上的编辑。ViewDesigner方式打开UserControl1,往里面添加任何东东,VS都能检测到UserControl1界面的变化,导致UserControl1下面新增一个UserControl1.resx的文件。.resx文件貌似就在VS2010里面才有,vs2013里木有发现。

以View Designer的方式打开UserControl1进行编辑,往界面里添加的任何东东包括排版,都反应在UserControl1.Designer.cd里了。所以,一般而言,我们不用管UserControl1.Designer.cs文件。 

所以,我们只需要理会UserControl1.cs,以View Designer方式打开它编辑好界面,然后以ViewCode的方式打开它,实现你想要的功能即可。

对UserControl的View的编辑是,往里面添加一个PictureBox和2个Lable。

C#的自定义控件

UserControl1的Code编辑如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsControlLibraryVS2010
{
    public partial class Nail : UserControl
    {
        public Nail(string ImagePath)
        {
            InitializeComponent();
            FileInfo finfo = new FileInfo(ImagePath);
            Image img = Image.FromFile(ImagePath);
            pbImage.Image = img.GetThumbnailImage(100, 100, null, new IntPtr());
            lblName.Text = Path.GetFileName(ImagePath);
            lblSize.Text = finfo.Length.ToString("N0") + " bytes";
        }

        public void Close()
        {
            this.Close();
        }
    } 
}
           

将WindowsFormsControlLibraryVS2010工程Build一遍,我们在工程的bin目录里得到了.dll文件。至此,我们的自定义控件做好了。

下面将自定义控件用起来。

在同一个Solution里新添加一个WindowsFormsApplication工程。

C#的自定义控件

Form1.cs类似于上文的UserControl1.cs,有View Code和View Designer两种打开方式。Form1.Designer.cs对应View Designer方式打开Form.cs的界面编辑。

我们往Form1.cs的界面里添加了:1个FlowLayoutPanel, 1个PictureBox, 2个Button。

C#的自定义控件

Form1.cs的代码编辑如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading.Tasks;
using WindowsFormsControlLibraryVS2010;

namespace WindowsFormsApplication
{
    public partial class Form1 : Form
    {
        private delegate void FlowOutPopulate(WindowsFormsControlLibraryVS2010.Nail tn);
        private static long ControlCounter = 0;
        private static long TotalImages = 0;
        private static int MaxControls = 25;
        private static long NextStop = MaxControls;
        
        public Form1()
        {
            InitializeComponent();
            btnNext.Enabled = false;
            btnPrevious.Enabled = true;       
            var t = Task.Factory.StartNew(() => Populate());
        }

        private void Populate()
        {
            string myPics = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
            string[] Images = System.IO.Directory.GetFiles(myPics, "*.jpg", System.IO.SearchOption.AllDirectories);
            TotalImages = Images.Count();

            for (long idx = ControlCounter; idx < NextStop; idx++)
            {
                try
                {
                    WindowsFormsControlLibraryVS2010.Nail tn = new Nail(Images[idx].ToString());
                    this.Populate(tn);
                    ControlCounter = idx + 1;
                }
                catch (System.Exception ex)
                {
                	
                }
            }
        }

        private void Populate(WindowsFormsControlLibraryVS2010.Nail tn)
        {
            if (!this.flowLayoutPanel1.InvokeRequired)
            {
                flowLayoutPanel1.Controls.Add(tn);
                this.Text = (ControlCounter + 1).ToString("N0") + " Nail controls added";
                if (ControlCounter > MaxControls)
                {
                    btnPrevious.Enabled = true;
                }
                else
                {
                    btnPrevious.Enabled = false;
                }

                if (ControlCounter > 0 && ControlCounter < TotalImages)
                {
                    btnNext.Enabled = true;
                }
                else
                {
                    btnNext.Enabled = false;
                }
            }
            else
            {
                FlowOutPopulate d = new FlowOutPopulate(Populate);
                this.Invoke(d, new object[] { tn });
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            NextStop += MaxControls;
            flowLayoutPanel1.Controls.Clear();
            var t = Task.Factory.StartNew(() => Populate());
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            NextStop -= MaxControls;
            ControlCounter -= MaxControls * 2;
            flowLayoutPanel1.Controls.Clear();
            var t = Task.Factory.StartNew(() => Populate());
        }

        
    }
}
           

运行结果如下:

C#的自定义控件

ps: 这个例子应该是借鉴了msdn上的某个sample,抱歉已经找不到原链接了……

将整个工程上传吧,下载链接:http://download.csdn.net/detail/u010153703/7723819