Solved

How can I predefine the text property in a class button derived from the class Windows.Forms Button?

Posted on 2014-03-12
2
328 Views
Last Modified: 2014-03-12
hello guys...

I want to predefine the "text" property of a class button derived from the class Button of system.windows.forms!

I want to drag and drop the button and instead of the text cBtnFirst1, I want a predefined text like "First".

thanks in advance..!!
1.png
2.png
0
Comment
Question by:Conrado ZAVALA
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 39924072
The solution is quite involved for such a minor thing but, if you really want to, then you need to override the Site property of the base class and intercept the OnComponentChanged event relating to the Text property.

An example would be something like:
using System;
using System.ComponentModel;
using System.ComponentModel.Design;

namespace WindowsFormsApplication1
{
    class cbFirst : System.Windows.Forms.Button
    {
        public cbFirst()
        { }

        private IComponentChangeService _changeService;

        public override System.ComponentModel.ISite Site
        {
            get
            {
                return base.Site;
            }
            set
            {
                _changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
                if (_changeService != null)
                    _changeService.ComponentChanged -= new ComponentChangedEventHandler(OnComponentChanged);
                base.Site = value;
                if (!DesignMode)
                    return;
                _changeService = (IComponentChangeService)GetService(typeof(IComponentChangeService));
                if (_changeService != null)
                    _changeService.ComponentChanged += new ComponentChangedEventHandler(OnComponentChanged);
            }
        }

        private void OnComponentChanged(object sender, ComponentChangedEventArgs ce)
        {
            cbFirst btn = ce.Component as cbFirst;
            if (btn == null || !btn.DesignMode)
                return;
            if (((IComponent)ce.Component).Site == null || ce.Member == null || ce.Member.Name != "Text")
                return;

            btn.Text = "First";
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:Conrado ZAVALA
ID: 39924160
Excellent...
Thank you very much..!!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do ASP.NET and MVC work together? 4 29
Syntax error 9 45
Reset asp.net login password 4 32
WCF Service Application cannot connect from TCP terminal 1 14
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now