Question Call inhereting class method from baseclass

aw48

Active member
Joined
Feb 9, 2013
Messages
36
Location
Germany
Programming Experience
10+
hi
I have : interface IApp, AppBase, App1, App2........Appn
with public void Appn() : AppBase,IApp { }
is it possible to have
Button.MouseUp += new System.Windows.Forms.MouseEventHandler(button_MouseUp);
in AppBase and button_MouseUp() in Appn ?
the corresponding program started out with two Apps and has since grown to 13 and with each new App I have to declare the event-handlers.
many thanks in advance
 
The derived class should override the protected OnEventName method, instead of handling the internal event. Handling and Raising Events
 
As a warning, in my experience, the WinForms Designer does not handle class hierarchies well. E.g. the following will lead to unexpected behavior:
C#:
class BaseForm : Form
{
    :
}

class ChildForm : BaseForm
{
    :
}
If you want to use something like above, you'll need to hand code your forms and not use the WinForms Designer.
 
So if the WinForms Designer doesn't handle class hierarchies well, how are you supposed to do multiple forms which more or less look like each other but with variations? Unfortunately, Microsoft's answer to that is to create a UserControl with the common elements in it, instead of building up a hierarchy of forms.

So instead of having something like this which a reasonable object oriented programmer would want:
C#:
class BaseUserForm : Form
{
    TextBox _txtFirstName;
    TextBox _txtLastName;
    CheckBox _chkActive;
    :
}

class CustomerForm : BaseUserForm
{
    ComboBox _cmbStatus;
    :
}

class SalesPersonForm : BaseUserForm
{
    TextBox _txtEmployeeId;
    :
}

Microsoft's wants you to do something like this instead:
C#:
class UserDetails : UserControl
{
    TextBox _txtFirstName;
    TextBox _txtLastName;
    CheckBox _chkActive;
    :
}

class CustomerForm : Form
{
    UserDetails _userDetails;
    ComboBox _cmbStatus;
    :
}

class SalesPersonForm : Form
{
    UserDetails _userDetails;
    TextBox _txtEmployeeId;
    :
}

Yes, it's still an object oriented principle being applied above, but not the one traditional 80's-90's would have chosen by default. Above aggregation is being used instead of inheritance. In a way, MS may have been ahead of its time, because it took a while for object oriented practitioners in the 2000's to pick up rule of thumb "favor aggregation over inheritance".
 
As a warning, in my experience, the WinForms Designer does not handle class hierarchies well. E.g. the following will lead to unexpected behavior:
C#:
class BaseForm : Form
{
    :
}

class ChildForm : BaseForm
{
    :
}
If you want to use something like above, you'll need to hand code your forms and not use the WinForms Designer.
I've never had such issues. You should use the Inherited Form item template rather than Windows Form and you won't be able to modify the inherited controls but, apart from that, everything has always worked as normal, in my experience.
 
thanks to all of you for answering my very incomplete question.

I didn't make it clear that the Apps are just classes, not forms or derived from form. Actually each class represents an item of the tabcontrol. the tabs controls are passed to the app or are created within the app itself.
sorry again for not expaining this correctly
 
Ah. Okay. It wasn't clear how you could do this:
C#:
Button.MouseUp += new System.Windows.Forms.MouseEventHandler(button_MouseUp);
In the AppBase without AppBase deriving from some kind of Form or Control ... unless you had something like this:
C#:
abstract class AppBase
{
    protected Button Button { get; private set; }

    protected AppBase(Control containerControl)
    {
        Button = containerControl.Controls["Name of wellknown control"];

        Button.MouseUp += new System.Windows.Forms.MouseEventHandler(button_MouseUp);
    }

    protected abstract void button_MouseUp(object sender, MouseEventArgs eventArgs);
}

class App1 : AppBase, IApp
{
    public App1(Control controlContainer)
        : base(controlContainer)
    {
    }

    protected override void button_MouseUp(object sender, MouseEventArgs eventArgs)
    {
        // implement for App1
    }
}
 
Last edited:
Back
Top Bottom