使用按钮的简单C#用户控件 [英] A simple C# Usercontrol using buttons

查看:223
本文介绍了使用按钮的简单C#用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
问候

我正在创建一个简单的用户控件,其中有5个按钮.我正在开发此程序只是为了学习如何创建用户控件.在运行时,我是5个按钮,同时显示到用户控件列表.我将其添加到列表中,以便可以使用属性网格来编辑这些按钮.代码如下


Hi All
Greetings

I am creating a simple usercontrol which has 5 buttons in it. I am developing this just to learn how to create a usercontrol. I am 5 button during runtime to a list and simultaneously to the usercontrol. I am adding it to the list so that i can edit those buttons using property grid. the code is as follows


private List<Button> bttns = new List<Button>();

[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor(typeof(BlitzHeaderPanelCollectionEditor),typeof(UITypeEditor))]
public List<Button> Bttns
{
    get{return bttns;}
}


int initilisecomponent 功能我以编程方式添加了5个按钮.

现在我的问题是我能够在Propertygrid中将这5个按钮视为集合
如果我编辑它们,更改将在设计时发生,但是当我运行程序时,更改将被删除.为什么会这样呢?请帮助我的专家


int the initilisecomponent function i am adding 5 buttons programatically.

Now my issue is i am able to see those 5 buttons as collections in propertygrid
if i edit them the changes take place during design time but when i run the program the changes are wiped off to the defualt. Why is this happening. Please help me experts

推荐答案

如果您要创建动态按钮,然后将其添加到列表中,则它们将不会使用您在设计时设置的属性.不同的实例.

为此,您将需要在运行时设置按钮的属性,例如btn1.Background = Brushes.Pink;
If you are creating dynamic buttons and then adding them to the list, they will not use the properties that you set at design time because these are different instances.

For this, you will need to set the properties of the button at run-time e.g. btn1.Background = Brushes.Pink;




发生的情况是父控件(主窗体)包含在设计时已应用于用户控件属性的新值.这些设置是在用户控件初始化之前应用的,因此用户控件的默认状态将优先.

如果您的控件实现了ISupportInitialize接口,则在完成控件属性的设置后,父控件(即主窗体)将调用EndInit方法.然后,您可以在EndInit中进行初始化.

要看到这一点,请检查主窗体上的InitializeComponent代码,以验证是否为您的控件调用了Infact EndInit.
Hi,

What happens is that the parent control, the main form, contains the new values that you have applied to the user control''s properties during design time. Those settings are applied before your user control has initialized, and therefore user control''s default state will take precedence.

If your control implements the ISupportInitialize interface, the EndInit method will by called by the parent control, the main form, once it has finished setting your control''s properties. You can then do your initialization within EndInit.

To see this, inspect the InitializeComponent code on the main form to verify that infact EndInit is being called for your control.


在代码项目中,此权限的完美示例……

组件和ISupportInitialize:快速指南 [
There is a perfect example of this right here on codeproject...

Component and ISupportInitialize: A Quick Guide[^]


这篇关于使用按钮的简单C#用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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