• Dot Net WinForm 控件开发 (七) 为属性提下拉式属性编辑器


    1、先画出用于下拉编辑器UI的用户控件

    代码:
     1using System;
     2using System.Collections.Generic;
     3using System.ComponentModel;
     4using System.Drawing;
     5using System.Data;
     6using System.Text;
     7using System.Windows.Forms;
     8
     9namespace CustomControlSample
    10{
    11    [DesignTimeVisible(false)] //不显示在ToolBox中
    12    public partial class UcSimpleCustomTypeDropDownEditor : UserControl
    13    {
    14        private SimpleCustomType _oldSCT;
    15        private SimpleCustomType _newSCT;
    16        private bool canceling = false// 按Esc 键,关闭下拉菜单,不更新值。
    17
    18        public SimpleCustomType SCT
    19        {
    20            get return _newSCT; }
    21        }

    22
    23        public UcSimpleCustomTypeDropDownEditor(SimpleCustomType sct)
    24        {
    25            _oldSCT = sct;
    26            _newSCT = sct;
    27            InitializeComponent();
    28        }

    29
    30        private void UcSimpleCustomTypeDropDownEditor_Load(object sender, EventArgs e)
    31        {
    32            this.textBox1.Text = _oldSCT.Min.ToString();
    33            this.textBox2.Text = _oldSCT.Max.ToString();
    34        }

    35
    36        private void textBox1_Validating(object sender, CancelEventArgs e)
    37        {
    38            try
    39            {
    40                int.Parse(textBox1.Text);
    41            }

    42            catch (FormatException)
    43            {
    44                e.Cancel = true;
    45                MessageBox.Show("无效的值。""验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
    46            }

    47        }

    48
    49        private void textBox2_Validating(object sender, CancelEventArgs e)
    50        {
    51            try
    52            {
    53                int.Parse(textBox2.Text);
    54            }

    55            catch (FormatException)
    56            {
    57                e.Cancel = true;
    58                MessageBox.Show("无效的值。""验证错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
    59            }

    60        }

    61
    62        protected override bool ProcessDialogKey(Keys keyData)
    63        {
    64            if (keyData == Keys.Escape)
    65            {
    66                canceling = true;
    67            }

    68            return base.ProcessDialogKey(keyData);
    69        }

    70
    71        private void UcSimpleCustomTypeDropDownEditor_Leave(object sender, EventArgs e)
    72        {
    73            if (!canceling)
    74            {
    75                SCT.Min = int.Parse(this.textBox1.Text);
    76                SCT.Max = int.Parse(this.textBox2.Text);
    77            }

    78        }

    79    }

    80}

    81

    2、再编写属性值编辑器类
     1SimpleCustomTypeDropDownEditor

    3、再添加一个属性,并用EditorAttribute 指定相应的编辑器类
     1下拉属性值编辑对话框的属性

    The end.

  • 相关阅读:
    vertical-align
    剑指offer刷题
    ES6 promise
    wangyi准备
    spring定时器
    xshell下linux常用操作
    HSSFWorkbook生成excel文件
    梳理并分解继承体系
    JSON格式数据转换
    部署项目
  • 原文地址:https://www.cnblogs.com/luqingfei/p/674912.html
Copyright © 2020-2023  润新知