小编Lan*_*way的帖子

用于 UserControl 的自定义 Text 属性的 C# 自定义 TextChanged 事件处理程序?

我在 C# 中创建了一个自定义用户控件,并为我的控件添加了一个自定义文本属性。不过,每当我的 Text 属性的值发生更改并且我想调用它时,我也想引发一个 Even TextChanged

这是我为用户控件创建的属性的代码:

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

namespace TestControls
{
    public partial class Box: UserControl
    {
        public Box()
        {
            InitializeComponent();
        }

        [Bindable(true)]
        [EditorBrowsable(EditorBrowsableState.Always)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        [Browsable(true)]
        [Category("Appearance")]
        public override string Text { get { return base.Text; } set { base.Text = value; this.Invalidate(); } }

        [Browsable(true)]
        public event EventHandler TextChanged;
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我已经创建了TextChanged事件处理程序,但我不知道如何将其链接到Text属性,以便当“文本”的值发生变化时,将引发事件。

请注意,我使用的是 Windows 窗体,而不是使用 WPF,并且我不想执行任何需要 …

c# user-controls event-handling winforms

3
推荐指数
1
解决办法
3369
查看次数

标签 统计

c# ×1

event-handling ×1

user-controls ×1

winforms ×1