gpt4 book ai didi

c# - 设计带有文本框的 UserControl 和带有网格属性的标签

转载 作者:行者123 更新时间:2023-11-30 20:30:01 24 4
gpt4 key购买 nike

Check the image of form and see the below code to get that functionality这是我完成的一些代码。我正在处理用户控件库项目,我在上面拖了一个标签和文本框。

Please check the code 
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 CustomControls
{
public partial class CustomTextbox: UserControl
{

public enum Directions
{
Left, Right, Top, Bottom
}

[Description("Define the Text Property of label")]
public string Description {
get
{
return label1.Text;
}

set
{
label1.Text = value;
}
}

[Description("Define the location of label")]
public Point LabelLocation
{
get
{
return label1.Location;
}
set
{
label1.Location = value;
}
}

[Description("Define the location of Textbox")]
public Point TextboxLocation
{
get
{
return textBox1.Location;
}
set
{
textBox1.Location = value;
}
}
[Description("Set Password Character Input in Textbox")]
public char PasswordChar
{
get
{
return textBox1.PasswordChar;
}

set
{
textBox1.PasswordChar = value;
}
}

[Description("Set the Multiline feature of Textbox")]
public bool MultiLine
{
get
{
return textBox1.Multiline;
}
set
{
textBox1.Multiline = value;
}
}

public CustomTextbox()
{
InitializeComponent();
}
}
}

我已经声明了一个枚举名称方向,这样我就可以根据在属性网格中选择的值(左、右、下、上)更改标签控件的位置,并且根据选择的值标签应该在我使用的项目中对齐控制dll。同样,我还想为文本框创建事件,例如文本验证和控件的其他重要事件。

我该怎么做。请推荐?

最佳答案

据我了解,您需要来自用户控件的自定义事件。

首先在您的用户控件中定义委托(delegate)和事件,如下所示。

public delegate void TextChangeDelegate(object obj, string str);
public event TextChangeDelegate TextChanged;

现在在您的用户控件中,您需要从您的自定义条件中引发此事件。

if(this.TextChanged != null)
{
this.TextChanged.Invoke(this, textBox1.Text);
}

在您使用它的地方使用它,如下所示。

userControl.TextChanged += UserControl_TextChanged;

关于c# - 设计带有文本框的 UserControl 和带有网格属性的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45430060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com