.NET自定义控件制作

1、新建一个窗体程序

2、在这个解决方案下,建一个用户控件

3、用户控件下的代码如下所示,因为控件都是字做的所以lable、textbox的名字都不一样

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace DetectionInterval
{
    public partial class XtraUserControl1 : DevExpress.XtraEditors.XtraUserControl
    {
        //private LabelControl la;
       
        public Label laplus1 = new Label();
        public Label laplus = new Label();
        public XtraUserControl1()
        {
            InitializeComponent();
             Control[] conTestItems = Testitems();
             this.Controls.Add(conTestItems[0]);
             this.Controls.Add(conTestItems[1]);
             this.Controls.Add(conTestItems[2]);
             this.Controls.Add(conTestItems[3]);
             this.Controls.Add(conTestItems[4]);
             this.Controls.Add(conTestItems[5]);
             this.Controls.Add(conTestItems[6]);
             this.Controls.Add(conTestItems[7]);
             this.Controls.Add(conTestItems[8]);
             laplus1.Click += new EventHandler(laplus1_Click);
             laplus.Click+=new EventHandler(laplus_Click);

        }
        public void laplus1_Click(object sender, EventArgs e)
        {
            if (laplus1.Text=="+")
            laplus1.Text = " - ";
            else if(laplus1.Text==" - ")
            laplus1.Text = "+";
        }
        public void laplus_Click(object sender, EventArgs e)
        {
            if (laplus.Text == "+")
                laplus.Text = "-";
            else if (laplus.Text == "-")
                laplus.Text = "+";
        }
        private void buttonEdit1_EditValueChanged(object sender, EventArgs e)
        {

        }
        private Control[] Testitems()
        {
            //************检测区间********
            Label  lajiance = new Label();
            lajiance.Top = this.Top + 50;
            lajiance.Left = this.Left + 10;
            lajiance.Width = 60;
            lajiance.Text = "检测区间";
         
            //****************************
            //************第一个K**********
            //Label lak = new Label();
            //lak.Top = lajiance.Top;
            //lak.Left = lajiance.Left +65;
            //lak.Width = 20;
            //lak.Text = "k:";
            ComboBoxEdit come = new ComboBoxEdit();
            come.Top = lajiance.Top-3 ;
            come.Left = lajiance.Left + 60;
            come.Width = 50;
            come.Text = "k";
            //****************************
            //***********第一个TextBox*****
            TextBox text1 = new TextBox();
            text1.Top = lajiance.Top - 3;
            text1.Left = come.Right + 10;
            text1.Width = 60;
            //****************************
            //*************+***************
            laplus = new Label();
            laplus.Top = text1.Top;
            laplus.Left = text1.Right + 10;
            laplus.Width =20;
            laplus.Text = "+";
            //****************************
            //************第二个TextBox****
            TextBox text2 = new TextBox();
            text2.Top = lajiance.Top - 3;
            text2.Left = laplus.Left +18;
            text2.Width = 60;
            //****************************
            //************~k**************
            //Label lak2 = new Label();
            //lak2.Top = come.Top;
            //lak2.Left = text2.Left + 57;
            //lak2.Width = 30;
            //lak2.Text = "~k:";
            ComboBoxEdit come1 = new ComboBoxEdit();
            come1.Top = lajiance.Top - 3;
            come1.Left = text2.Right + 10;
            come1.Width = 50;
            come1.Text = "k";
            //*****************************
            //************第三个TextBox******
            TextBox text3 = new TextBox();
            text3.Top = lajiance.Top - 3;
            text3.Left = come1.Right + 10;
            text3.Width = 60;
            //****************************
            //************第二个+**********
            laplus1 = new Label();
            laplus1.Top = come.Top;
            laplus1.Left = text3.Left + 60;
            laplus1.Width = 20;
            laplus1.Text = "+";
            //*************第四个TextBox****
            TextBox text4 = new TextBox();
            text4.Top = lajiance.Top - 3;
            text4.Left = laplus1.Left + 18;
            text4.Width = 60;
            //****************************
            Control[] testtrackbar = { lajiance, come, text1, laplus, text2, come1, text3, laplus1, text4 };
            return testtrackbar;
        }
    }
}
4、然后重新生成解决方案,在左边的工具栏就会找到自己做的控件

5、把这个控建拖进这个窗体就可以

6、运行截图如下

这个下拉框的功能未实现,当鼠标点击“+”时,会变成减号,当点击“-”号时会变成“+”号

原文地址:https://www.cnblogs.com/gouguo/p/2795844.html