如果设置表单的AcceptButton,我如何自定义按钮? [英] How can i customize the button if it set a AcceptButton of the form?

查看:130
本文介绍了如果设置表单的AcceptButton,我如何自定义按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经实现了自定义按钮控件以及一些其他选项。并将我的自定义按钮添加到表单中。当我设置Form.Acception是添加的自定义按钮,然后我想在Button中进行一些自定义。我想在自定义按钮类实现中自定义按钮
外观,当它标记为Form的AcceptButton时。

I have implemented the custom button control with some additional options. And added my custom button into the form. When i set the Form.Acception is the added custom button, then I want to do some customization in the Button. I want to customize the button appearance in the Custom button class implementation, when it marked as AcceptButton of the Form.


任何人都建议我怎么知道按钮被标记作为Button类中窗体的AcceptButton?

Anyone suggest how can i know the button is marked as AcceptButton of the form in Button class?

推荐答案

AcceptButton位于表单级别,您的按钮甚至可能无法附加到该表单级别。默认情况下,带有DialogResult of OK的按钮会自动设置为AcceptButton(至少在设计器中)。我个人建议您只需在按钮的DialogResult上查看
并使用它而不是试图弄清楚它是否是表单的按钮。

The AcceptButton is at the form level which your button may not even be attached to. By default the button with the DialogResult of OK is automatically set as the AcceptButton (at least in the designer). Personally I would recommend that you simply look at the DialogResult of the button and use that rather than trying to figure out if it is the button for the form.

但是如果你真的很希望你能解决一些问题。简单的部分是找到表格。您可以查看按钮的父级,但父级可以(并且将)更改,因为它不会被设置,直到按钮实际附加到父级
,因此您需要处理ParentChanged事件(或方法)。当父母改变时,你需要重新评估一切。

But if you really want to you're going to be in for some issues. The easy part is finding the form. You can look at the Parent of the button but the parent can (and will) change since it doesn't get set until the button is actually attached to the parent so you'll need to handle the ParentChanged event (or method). When the parent changes you'll need to reevaluate everything.

由于您的按钮可能位于一个容器中,该容器本身是表单的父级,因此您必须将父链返回,直到找到该表单。一旦找到它,您就可以访问AcceptButton。但请注意,父母的父母可以更改,而您的
将不会收到通知,因此可以将其添加到容器中,然后更改容器的父级更改。

Since your button may be in a container that itself is parented by the form you'll have to walk the Parent chain back until you find the form. Once you've found it you have access to the AcceptButton. But note that the parent's parent can change and you won't get notified so it is possible to get added to a container and then the container's parent change.

另一个问题是当AcceptButton更改AFAIK时没有引发事件。因此,当表单设置时,您将不会收到通知。这可能发生在按钮作为表格的父级之后。

The other issue is that there is no event raised when AcceptButton changes AFAIK. So when that is set by the form you won't get notified. That will likely occur after the button is parented to the form.

可能最安全的路线是每次绘制控件时都要做出此决定。因此,当您获得绘制事件时,找到父窗体,然后查看其AcceptButton。这可确保您始终获得最新价值。但是,
也意味着如果AcceptButton属性被更改,则在下次绘制按钮之前,您不会更新以反映它。

Probably the safest route to go would be to make this determination each time you paint the control. So when you get the paint event, find the parent form and then look at its AcceptButton. This ensures you are always getting the latest value. However it also means that if the AcceptButton property is changed you won't update to reflect it until the button is painted the next time.

Michael Taylor

http://www.michaeltaylorp3.net

Michael Taylor
http://www.michaeltaylorp3.net


这篇关于如果设置表单的AcceptButton,我如何自定义按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆