Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
336 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

926 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