.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/archive/2012/11/30/2795844.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值