在 C#.net 上扩展复选框控件

发布于 2024-10-01 05:28:38 字数 89 浏览 0 评论 0原文

我想扩展可以在每个表单上使用的复选框控件。

如果选中复选框,则返回“Y”,否则返回“N”。

我如何扩展复选框控件并以其他形式使用它?

I want to extend checkbox control which can use on every form.

If check box is checked than it will return "Y" otherwise "N".

How may i extend check box control and use it in other forms ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嗳卜坏 2024-10-08 05:28:38

扩展方法既快速又简单,但正如您在标签中提到的那样,您不能在 .NET 2.0 中做到这一点。

您必须创建一个新的用户控件,并在其上放置一个 CheckBox
然后,您必须对最常见的事情做出响应,以使其像 CheckBox 一样工作。
例如,您必须处理控件的 Resize() 事件,并将所做的任何更改相应地应用到其中的 CheckBox
完成所有这些操作后,您现在就拥有了一个由您自己创建的功能齐全的 CheckBox 控件,该控件非常模仿原始控件。

只有这样你才能像这样创建一个公共获取属性:

public bool CheckedText
{
     get
     {
         return (CheckBox1.Checked ? "Y" : "N");
     }
}

就是这样。
但如果我是你,我就不会经历这些麻烦。
我会在写入数据库之前检查自己。
老实说,为这样一个微小的功能扩展现有的用户控件是不值得的。

Extension method is quick and easy, but you can't do that in .NET 2.0, as you mentioned in your tags.

You would have to create a new user control, and place a CheckBox on it.
You would then have to respond to the most common things to make it act like a CheckBox.
For example, you would have to handle your control's Resize() event, and apply whatever change made accordingly to the CheckBox within.
After all that is done, you now have a fully-functional CheckBox control of your own creation that very much mimics that of the original one.

Only then you can create a public get property like so:

public bool CheckedText
{
     get
     {
         return (CheckBox1.Checked ? "Y" : "N");
     }
}

And that is it.
But if I were you, I wouldn't go through all that hassle.
I would just do that check myself right before I write to the database.
Extending an existing user control for such a minute feature is not worth it, honestly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文