I find the easiest work around to exposing your bases methods for events is to write a wrapper.
Delcare the method with the same name in your top class using the new keyword.
In this method call the base.MethodName with the same arguments.
Now you can link your objects events through your wrapped method to call the base method that is inherited.
EXAMPLE: Wrapping the ContentsResized event to a base class method to automatically re-size RichTextBox controls.
This is the base class that inherits a Form control. Copy this code and paste it into a new class file.
using System.Drawing;
using System.Windows.Forms;
namespace PcSanity.Interfaces
{
public class ActionReportDialog : Form
{
protected int WidestControl;
protected void ResizeRichTextBox(object sender, ContentsResizedEventArgs e)
{
Control ControlToResize = (Control)sender;
ControlToResize.Width = e.NewRectangle.Width;
// used to process dialog width
if (ControlToResize.Right > WidestControl)
WidestControl = ControlToResize.Right;
}
}
}
Next you need to create a new Form within your project.
Now View Code to show the Forms class. You should have something like this.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PcSanity
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Now you have update the inheritance of the form to inherit the base class we made earlier.
The result is something like this.
public partial class Form1 : PcSanity.Interfaces.ActionReportDialog
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
Now you need to add a copy of the event handler method declaring it with the new keyword and then make it call the base class.
public partial class Form1 : PcSanity.Interfaces.ActionReportDialog
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
protected void ResizeRichTextBox(object sender, ContentsResizedEventArgs e)
{
base.ResizeRichTextBox(sender, e);
}
}
Add some RichTextBox controls to this Form.
Select all the RichTextBox controls you added and view the properties.
Set multiline to each control to false.
Goto the Events pane.
Browse and find 'ContentsResized'
Click the drop down arrow and click to add the event wrapper you added to the top level class.
Now if you add some code to write text to the RichTextBox controls you added the will resize on the form automatically and you have access to widest control to resize the Form if required =]
public partial class Form1 : PcSanity.Interfaces.ActionReportDialog
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.Text = "short text";
richTextBox2.Text = "Alot more text... dsgsfdgihsdighdishgishdighsihgihsighids";
richTextBox3.Text = "Alot more text... dsgsfdgihsdighdishgishdigsdghdsgdsgsdgdsgsdgdsgdsgsdgdsgsdgsdhsihgihsighids";
this.Width = base.WidestControl + 15;
}
protected void ResizeRichTextBox(object sender, ContentsResizedEventArgs e)
{
base.ResizeRichTextBox(sender, e);
}
}
Now if you show this dialog. The load event will reset the text of all the RichTextBox controls you added.
After you set the text the RichTextBox control will fire ResizeRichTextBox method. Which in turn fires the base method.
The base method will resize the RichTextBox control based of the size of it's own contents and reset the WidestControl value if it is bigger.
The last thing we do is reset the Width property for the Form control to the widest controls RichTextBox controls calculated right + 15.
Boom