I created an OnPaint event of my button,which I lately tried to override,but I failed
My code:
protected override void button1_Paint(PaintEventArgs e)
{
}
I get this error: "no suitable method found to override".
What should I do to make the error dissapear,but keep the method as override?
If the method is not virtual, you can't override it. If you can't override it, there is no point in trying to keep the override keyword.
If you want to shadow the method, you use the new keyword instead of override.
The method that you want to override is probably called OnPaint, not button1_Paint. Change the method declaration so it looks like this instead:
protected override void OnPaint(PaintEventArgs e) { }
Note though that this code should be in a subclass of the class from which you want to override the method. If you place this method in a form, it will handle that form's painting.
In your base-class, you need to declare the method as virtual
example:
public class Person
{
public virtual void DoSomething()
{
// do something here
}
}
public class Employee : Person
{
public override void DoSomething()
{
base.DoSomething();
}
}
Edit:
Perhaps this can help you out?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
}
private void button1_Paint(object sender, PaintEventArgs e)
{
}
}
You should declare the method in the base class as virtual in order to be able to override it.
you override methods coming from other classes. To keep the method you'll have to put it in a child class and call it from there.
do you have the override in the same class as the original method? if so, just merge the functions and remove the override.
I guess you have something like this now:
public class MyButton : Button {
public MyButton() {
this.Paint += new PaintEventHandler(MyButton_Paint);
}
void MyButton_Paint(object sender, PaintEventArgs e) {
//your code
}
}
If you inherited from a button you should use this code:
public class MyButton : Button {
protected override void OnPaint(PaintEventArgs pevent) {
base.OnPaint(pevent);
}
}
Related
Im trying to write abstract class for different reports.
I have a method
protected Tuple<byte[], string, string> RenderReport()
which has such lines
var localReport = new LocalReport { ReportPath = _reportLocalFullName };
...
localReport.SubreportProcessing += localReport_SubreportProcessing;
Derived class must write own code in the localReport_SubreportProcessing.
I'm not sure how to make inheritance here. Can someone help ?
Rather than having a method:
private void localReport_SubreportProcessing(...) {...}
consider instead:
protected virtual void OnSubreportProcessing(...) {...}
Now your subclasses can simply use:
protected override void OnSubreportProcessing(...) {...}
You can call a common method, which you override in your base class.
So in localReport_SubreportProcessing, call ProcessSubreport
private void localReport_SubreportProcessing(object sender, EventArgs e)
{
this.ProcessSubreport();
}
protected virtual void ProcessSubreport()
{ }
And override it in your deriving class:
protected override void ProcessSubreport()
{ }
Try like below.
public abstract class BaseReport
{
......
protected Tuple<byte[], string, string> RenderReport()
{
var localReport = new LocalReport { ReportPath = _reportLocalFullName };
...
localReport.SubreportProcessing += localReport_SubreportProcessing;
...
}
protected abstract void LocalReport_SubreportProcessing(object sender, EventArgs e);
}
public class DerivedReport1 : BaseReport
{
protected override void LocalReport_SubreportProcessing(object sender, EventArgs e)
{
// Report generation logic for report1.
}
}
public class DerivedReport2 : BaseReport
{
protected override void LocalReport_SubreportProcessing(object sender, EventArgs e)
{
// Report generation logic for report2.
}
}
I have several controls that inherit BaseUserControl. BaseUserControl inherits System.Web.UI.UserControl.
I want to override OnLoad event like this:
public partial class MyControl1 : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
This works great, the only problem is that I have to copy this piece of code across 3 controls, which I do not like. (I do not have access to Base class, since it is inherited by 100s of controls.)
So, my result currently looks like this:
public partial class MyControl2 : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
public partial class MyControl3 : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
What are the good ways to refactor this? One way is to extract
this.Value = myCustomService.GetBoolValue();
///More code here...
to a separate method, but I was wondering if there's a way that will allow us specify override event only once?
You could create an extra base class for those controls sharing functionality and make this class inherits from BaseUserControl
// Change YourBaseControl by a meaningful name
public partial class YourBaseControl : BaseUserControl
{
protected override void OnLoad(EventArgs e)
{
this.Value = myCustomService.GetBoolValue();
///More code here...
base.OnLoad(e);
}
}
public partial class MyControl2 : YourBaseControl
{
...
}
public partial class MyControl3 : YourBaseControl
{
...
}
Here's a portion of the standard Label control in WinForms:
public class Label : Control
{
protected override void OnTextChanged(EventArgs e)
{
...
}
}
I'd like to override the OnTextChanged event but I'm not sure of the best way.
Should I derive a subclass from Label class and then override the function like this?
public class Class1 : Label
{
protected override void OnTextChanged(EventArgs e)
{
MessageBox.Show("S");
}
}
If so, how and where should I add this class?
If not, how can I override functions which are defined inside a control?
This is the way you can override the method for control. As you have done is absolutely right but detailed implementation is here.
This is the form part
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class TestForm : Form
{
MyLabel newLable;
public TestForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
newLable = new MyLabel();
newLable.Height = 30;
newLable.Width = 40;
newLable.Text = "hello";
this.Controls.Add(newLable);
}
}
}
You can use MyLabel from toolbox also.
And MyLabel class is
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class MyLabel:Label
{
public MyLabel()
{
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
MessageBox.Show("Label Clicked");
}
}
}
I have been trying to find a good answer to this question, but can't seem to find one. I have an ASP.NET page that derives from a base page, like this:
public partial class MainPage : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime(); // This works fine
}
}
And the base page:
public partial class MyBasePage: Page
{
}
protected DateTime GetLoginTime()
{
// Do stuff
return loginTime;
}
Now I have a user control on that page that needs to call my method...Like this:
public partial class TimeClock : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime(); // This does not work!
}
}
As you can see, I cannot call my base method, for obvious reasons. My question is, how can I call this method from my user control? One work around I've found is like this:
var page = Parent as MyBasePage;
page.GetLoginTime(); // This works IF I make GetLoginTime() a public method
This works, if I make my function public instead of protected. Doing this doesn't seem like a very OOP way to tackle this solution, so if someone can offer me a better solution, I'd appreciate it!
TimeClock inherits from UserControl, not from MyBasePage so why should TimeClock see the Method GetLoginTime()?
You should keep your UserControl out of your Page stuff. It should be decoupled in OOP speak. Add properties to set values and delegates to hook into events:
public partial class TimeClock : UserControl
{
public DateTime LoginTime{ get; set; }
public event UserControlActionHandler ActionEvent;
public delegate void UserControlActionHandler (object sender, EventArgs e);
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button_Click(object sender, EventArgs e)
{
if (this.ActionEvent!= null)
{
this.ActionEvent(sender, e);
}
}
}
Page
public partial class MainPage : MyBasePage
{
protected void Page_Load(object sender, EventArgs e)
{
var loginTime = GetLoginTime();
TimeClock1.LoginTime = loginTime;
TimeClock1.ActionEvent += [tab][tab]...
}
}
(this.Page as BasePage).MethodName()
I have done a base form for my window app but not sure is this correct or is there a better way to improve it.
My purpose:
Simplify my code at UI form
Create CRUD button to be inherited.
Question:
How do I implement interface with the 4 button events at the UserForm.cs by right clicking the UserForm:BaseForm. Currently I have to manually write all 4 button event to override.
My BaseForm seem to be empty. Can I put the messageBox.show("succcess") at my baseForm? Meaning to say that after run the UserForm.cs, it will run back to BaseForm. This can save up a few lines of code at userform.
Should I put my logger at the baseForm too?
My baseForm.cs
public BaseForm()
{
InitializeComponent();
}
public virtual void btnSave_Click(object sender, EventArgs e)
{
}
public virtual void btnDelete_Click(object sender, EventArgs e)
{
}
public virtual void btnNew_Click(object sender, EventArgs e)
{
}
public virtual void btnReset_Click(object sender, EventArgs e)
{
}
My UserForm.cs inherit baseForm
public override void btnSave_Click(object sender, EventArgs e)
{
if (!IsValidated()) return;
BindValueToObject();
try
{
user.Add();
bindingSource.Add(user);
MessageBox.Show("Success");
}
catch(Exception ex)
{
MessageBox.Show("failed");
Logger.Error(typeof(UserForm), ex.ToString());
return;
}
}
public override void btnDelete_Click(object sender, EventArgs e)
{
base.btnDelete_Click(sender, e);
}
public override void btnNew_Click(object sender, EventArgs e)
{
base.btnNew_Click(sender, e);
}
public override void btnReset_Click(object sender, EventArgs e)
{
base.btnReset_Click(sender, e);
}
1 - At the moment the methods in your base form is declared as virtual. This means that it is optional for any derived classes to override the methods. Therefore you won't be notified asking to override them. If this is your desired behavior then you will have to manually override the methods by either writing the code yourself or getting your IDE to generate the methods for you. However, if this is not the desired behavior and you would in fact like to force all derived classes to implement these methods, then you should declare them as abstract.
2 - Any calls to those methods will trigger the overridden methods in the derived class (if it exists) and thus the code in your base class will never get executed. If you're interested in both executing the code in the derived- and the base class then you should issue a call to the base method like this
public override void btnReset_Click(object sender, EventArgs e)
{
// do form specific stuff here
someButton.Text = String.Empty;
// then invoke base method
base.btnReset_Click(sender, e);
}
3 - If you're going to do general logging for the form, then putting it in the base class would be a good idea. However, remember always to invoke the base method call in any overridden method.