启动时更改继承控件上的属性值 [英] Change property value on an Inherited Control at startup

查看:86
本文介绍了启动时更改继承控件上的属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我希望这不是一个真正愚蠢的问题,如果可以的话,请原谅我,但我似乎无法解决问题.

我通过从Button继承来创建控件.我想在使用Button的程序启动时设置Button.Text属性,但是我希望在启动时从自定义控件中设置文本.

但是,即使我在构造函数中将其更改为所需的文本,也总是获得在设计器"属性窗格中设置的默认文本.好像该控件在我尝试设置的文本之后加载了Designer文本.我想这与控件创建时事物发生的顺序有关.

我确信有人可以解释如何控制这种行为.非常感谢.

Hi there,

I hope that this is not a really stupid question, if so please forgive me but I can''t seem to solve the problem.

I am creating a control by inheriting from a Button. I want to set the Button.Text property when the program using the Button starts, but I want the text to be set from within the custom control at startup.

However, I always get the default text that is set in the Designer properties pane, even if I change it in the constructor to the one I want. It''s as if the control loads the Designer text after the one I''ve tried to set. I guess this is to do with the order that things happen when the control is created.

I am sure someone can explain how to control this behavior. Many Thanks.

推荐答案

这正是发生的事情和应该发生的事情.自己想想:设计器中用户定义的文本应该在这里,因为这是设计器的目的.构造函数总是在对象的生命周期中首先被调用的.不清楚如何看到其中的任何错误.顺便说一句,您始终可以在没有Designer的情况下工作.但是,例如,如果出售控件,则需要像用户期望的那样支持Designer.

如果您需要其他一些有实际意义的行为,请说明它的含义以及为什么需要它.但是首先,标记您正在使用的UI库.

—SA
This is exactly what happens and what should happen. Think by yourself: the text defined by a user in Designer should be there, because this is a purpose of the Designer. And the constructor is something which always is called first during lifetime of the object. Not clear how can you see anything wrong in it. By the way, you can always work without Designer. However, if you, say, sell your control, you will need to support the Designer as the users will expect it.

If you need some other behavior which makes some practical sense, please explain what is it and why do you need it. But first, tag the UI library you are using.

—SA




首先,您应该将System.Design dll添加到引用中,并且如果您按如下方式设计按钮代码,则在将按钮拖放到窗体上时,您将在按钮上看到文本.

Hi,

First of all you should add System.Design dll into your references and than if you design your button code as below,at the time you drag and drop your button on the form you will see your text on the button.

[Designer(typeof(MyButtonDesigner))]
   class MyButton : Button
   {


   }

   public class MyButtonDesigner : System.Windows.Forms.Design.ControlDesigner
   {
       public override void OnSetComponentDefaults()
       {

           base.OnSetComponentDefaults();
           Control.Text = "Demo Button";
       }
   }



谢谢



Thanks


这篇关于启动时更改继承控件上的属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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