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
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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