0

I have found that there is no base class that directly underneath:

Textbox, Label and Button

These are their definitions:

public class TextBox : TextBoxBase
public abstract class TextBoxBase : Control

public class Button : ButtonBase, IButtonControl
public abstract class ButtonBase : Control

public class Label : Control

in .net #region Assembly System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.5\System.Windows.Forms.dll

I needed to override the refresh button and also add some custom properties and some custom event handlers for every control. Therefore I decided to create a custom Control. But that was before I realized that not every class is directly extended from Control. I know I can use an Interface to at least enforce that the methods/properties are in the contract but I want to be able to write the code for these 'custom' methods once not every time I extend the interface.

Is there another way?

This is the custom Control class and the desired functionality I want inherited. The fantasy is that I would do something like this:

public class TextBox : Sgctrl (of course fantasy as it is not possible my way as shown below)

public class SGctrl : Control
{

    public String MySystem_SourceField { get; set; } 

    protected Core ctrlCore { get; set; }


    protected MyForm.Forms.Location.FormLoc callingForm;
    // the delegate
    public delegate void Published_DELEGATE_TYPE_MySysConnectedControlRefresh(object aSGctrl, SGRefreshHandler sgr);

    // This event instance of the delegate provides a hook for .NET as it makes it show up in the properties->events
    // and by doubleclicking it allows other specific (textbox/label) visibility;enabled properties to be set
    // 
    public event Published_DELEGATE_TYPE_MySysConnectedControlRefresh MySysConnectedControlRefresh_handler;


    //protected virtual void OnMySysSGcontrolRefresh(SGRefreshHandler e)
    protected virtual void OnMySysSGcontrolRefresh(SGRefreshHandler e)
    {
        if (MySysConnectedControlRefresh_handler != null)
                                             //signature SGctrl , eventhandler object
        { MySysConnectedControlRefresh_handler(this, e); }
    }

    //public SGctrl(bool refreshable,Form SGcallingForm)
    public SGctrl()
    {

    }

    // will do it in the refresh method


    public override void Refresh()
    {
        base.Refresh();

        // if calling from and core reference are not yet initialized do so
        if (this.callingForm == null)
            this.callingForm = (MyForm.Forms.Location.FormLoc)this.TopLevelControl;

        if (this.ctrlCore == null)
            this.ctrlCore = this.callingForm.getCoreRef();

        // pass to suscriber
        //core.getField("cllabloc1");    
        //sgctrl.Text = sgctrl.core.getField(sgctrl.MySystem_SourceField) // get MySys data

        if (this.GetType()==typeof(TextBox)) // if need to know type of control
        { }

        this.Text=this.ctrlCore.getField(this.MySystem_SourceField);
        SGRefreshHandler SGRefrshrobj = new SGRefreshHandler(this);
        OnMySysSGcontrolRefresh(SGRefrshrobj);
    }
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
c-sharp
  • 1
  • 1

2 Answers2

0

You need to inherit from the controls you need to extend:

public class SGTextBox : TextBox, ISGctrl 
{
}

Now, ISGctrl is an interface that defines the custom properties and methods to either add or override in SGTextBox.

The Sharp Ninja
  • 1,041
  • 9
  • 18
  • Yes but by using an interface I will have to rewrite my handlers on every custom control. I want to write the body of the code once. – c-sharp Dec 09 '15 at 19:47
  • I guess what I am trying to achieve is to write the custom code ("handler") once for all the basic controls mentioned. – c-sharp Dec 09 '15 at 19:52
  • Through an interface I would have to rewrite the code for every control. The only gain is that the inheritance keeps me in check to be sure that I included my handler and my .refresh override and my custom properties. – c-sharp Dec 09 '15 at 19:54
  • With the interface you can write extension methods to add the custom code one time. – The Sharp Ninja Dec 09 '15 at 20:08
  • Thanks Sharp Ninja. The only problem I am having with the extension methods is that some are delegates and I get the following as an error: as I can't make this the first parameter public static delegate void Published_DELEGATE_TYPE_MySysConnectedControlRefresh(object aSGctrl, SGRefreshHandler sgr); – c-sharp Dec 09 '15 at 20:36
  • Why would you create a delegate for each class? Why not just a single delegate and define the event in the interface, then the only implementation you have to do is declaring the event in the custom class. – The Sharp Ninja Dec 09 '15 at 20:52
0

Also you can create generic

public class SGctrl<T> : where T:Control
{
//extencion go here
}
skalinkin
  • 1,024
  • 9
  • 19