0

I've created a form. As an example, some of the form event methods look like this:

private void BtnClose_Click(object sender, EventArgs e)
{
    this.Close();
}

private void BtnRun_Click(object sender, EventArgs e)
{
    this.Run();
}

private void BtnReset_Click(object sender, EventArgs e)
{
    using (SqlConnection conn = new SqlConnection(Program.ConnStr))
    {
        using (SqlCommand cmd = conn.CreateCommand())
        {
            conn.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "rt_sp_testAOFreset";
            result = cmd.ExecuteNonQuery();
        }
    }
}

Seems simple and straightforward, but I'm actually inheriting the form into another class.

   public class Simulate : TableForm

So, when I instantiate the Simulate class...

    Simulate sim = new Simulate();
    Application.Run(sim);

How do I run a method that is from the Simulate class, when the buttons are from the TableForm class? E.g. The form has a "Run" button where the event method is contained in the TableForm class, but the run method is actually in the Simulate class.

Can the methods be moved to the Simulate class? Or can the Simulate class be called by the TableForm class being the base class?

addohm
  • 2,248
  • 3
  • 14
  • 40
  • Have you considered creating and using a Custom Control? Maybe it will fit well for your purposes. – Tony Nov 12 '18 at 13:03
  • Use the `virtual` keyword so the derived class can override Run(). An event would be another way, meh. – Hans Passant Nov 12 '18 at 13:06
  • You could make the BtnReset_Click public or some other better suited modifier. Or (recommended): You can let it call a public function which you can then override or/and extend in the inherited classes.. – TaW Nov 12 '18 at 13:07

1 Answers1

0

The TableForm class presumably adds those buttons to the form in its InitializeComponent() method. Assuming that the constructor for the derived Simulate class invokes the constructor of its base class, those same controls will be added to your dervied form.

public class Simulate: TableForm{
    ...
    public Simulate() : base() {
        InitializeComponent();
    }
    ...
}

I'm not sure where your event handlers are added off hand, but so long as you are properly invoking the base class constructor and load routines, those same event handlers will be setup (unless you have some other place you setup your controls). If you want to override the functionality of those routines, make them protected virtual.

Better yet, add protected virtual OnRun and OnReset methods that can be overriden (there are existing protected virtual routines that you can override for OnClose and OnClosing).

NOTE: I am assuming you are using WinForms

EDIT: As requested, here is some example code. Setup TableForm like so:

private void BtnClose_Click(object sender, EventArgs e)
{
    this.Close();
}

private void BtnRun_Click(object sender, EventArgs e)
{
    this.Run();
} 

protected virtual void Run() {
    // do stuff
}

private void BtnReset_Click(object sender, EventArgs e)
{
    Reset();
}

protected virtual void Reset() {
    using (SqlConnection conn = new SqlConnection(Program.ConnStr))
    {
        using (SqlCommand cmd = conn.CreateCommand())
        {
            conn.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "rt_sp_testAOFreset";
            result = cmd.ExecuteNonQuery();
        }
    }
}

Now in your derived Simulate class you can do this:

protected override void Run() {
    base.Run(); 
    // additional stuff
} 

protected override void Reset() {
    base.Reset();
    // additional stuff
}

protected override void OnFormClosing(System.Windows.Forms.FormClosingEventArgs e) {
    // handle the form closing event
}
  • Is this what you mean? https://stackoverflow.com/questions/756237/c-raising-an-inherited-event#756249 – addohm Nov 12 '18 at 13:59
  • I guess I need an example. I don't understand how this flows. When I click the button which originates in the TableForm class, I need it to run a method in the Simulate class. – addohm Nov 12 '18 at 14:13
  • Makes a lot more sense now. I'll give it a whirl and play a bit. Thanks. – addohm Nov 12 '18 at 15:12
  • I guess it seems no matter what the code that I want to run HAS to be in the TableForm class. – addohm Nov 12 '18 at 15:19
  • @Jaberwocky that is incorrect. You need your inheritance setup properly so that your overridden functions are invoked by the base class – Ryan Pierce Williams Nov 12 '18 at 16:07
  • Ahh, I see. I was too quick to add that comment when I saw the `protected virtual void Reset()` snippet. :P – addohm Nov 13 '18 at 01:06