Reputation: 1413
In WindowsForms.
I have a custom control that contains only one control: a PictureBox.
public partial class VarIllumButton : UserControl
It uses the OnLoad method:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e); // Handle the default function
pictureBox.Image = image0;
}
Since the custom control only has 1 control, I wish to change it to:
public partial class VarIllumButton : PictureBox
But then I get an error
'System.Windows.Forms.PictureBox' does not contain a definition for 'OnLoad'
Yet, I see that the Control class does have an OnLoad method:
Can you think of why I can access it from a UserControl, but not from a Control?
Is there another method that I can use that is called when the Control finishes loading?
Upvotes: 4
Views: 3817
Reputation: 5477
As I mentioned in the comments, the MSDN documentation with the OnLoad
mention is for the Web UI controls, not the WinForms one. The WinForms does not have an OnLoad
method.
The best place would be to assign the image in the constructor, but unfortunately you mention that is not possible. Perhaps you can handle the OnCreateControl
/CreateControl
event, or you should let the user of the control assign the image at the correct time.
I usually suggest avoiding using the Load
event of a form/control for doing stuff, because there is an issue with exceptions being swallowed with 32 bits programs on 64 bit windows [SO Q&A, article].
Upvotes: 5
Reputation: 1709
UserControl
has a load event protected virtual void OnLoad(EventArgs e)
[System.Windows.Forms.UserControl.Load
] and as it virtual, it can be overridden as you've done.
But PictureBox
doen't have any such event, so you can't override. Instead it has a public method load public void Load()
and event's like public event AsyncCompletedEventHandler LoadCompleted
, public event ProgressChangedEventHandler LoadProgressChanged
etc. That's the reason you get the error.
Inheritance hierarchy for UserControl
UserControl -> ContainerControl -> ScrollableControl -> Control and for PictureBox
it's PictureBox -> Control, public void Load() is just a public method in PictureBox class.
Upvotes: 0
Reputation: 96551
(assuming this is an ASP.NET question)
It seems you have an incorrect using directive in your source code, e.g:
using System.Windows.Forms;
Therefore your control is deriving from System.Windows.Forms.Control
, which doesn't have an OnLoad
method.
Replace that using directive with using System.Web.UI;
, and it should compile (although there is no PictureBox
control in System.Web.UI
).
Upvotes: 0