Click here to Skip to main content
15,890,741 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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.
Posted
Comments
Sergey Alexandrovich Kryukov 10-Oct-12 13:15pm    
First of all, you need to tag your application type or UI library you use. What is it? WPF? Forms? Metro? ASP.NET? Silverlight?
--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
 
Share this answer
 
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.

C#
[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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900