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
329 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can my static class become undefined?? 8 47
Why does my excel data start at B1 instead of A1. 9 80
SQL Help 27 40
VB.NET 2008 - SQL Timeout 9 22
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

815 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

9 Experts available now in Live!

Get 1:1 Help Now