4

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?

Davide Andrea
  • 1,357
  • 2
  • 15
  • 39

3 Answers3

4

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].

Community
  • 1
  • 1
MicroVirus
  • 5,324
  • 2
  • 28
  • 53
0

(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).

M4N
  • 94,805
  • 45
  • 217
  • 260
  • "an incorrect using directive"... "using System.Windows.Forms;" . I wouldn't consider it incorrect. But, yes, you are right, I am using Windows Forms. – Davide Andrea Nov 22 '15 at 19:05
0

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.

Sudipta Kumar Maiti
  • 1,669
  • 1
  • 12
  • 18
  • public void Load() https://msdn.microsoft.com/en-us/library/zyc6weyd%28v=vs.110%29.aspx. No, that's for when an _image_ has loaded, not for when the _control_ has loaded. Same for the other ones you mentioned, – Davide Andrea Nov 22 '15 at 18:58
  • Inheritance hierarchy is something like this UserControl -> ContainerControl -> ScrollableControl -> Control and for PictureBox it's like PictureBox -> Control, public void Load() is just a public method in PictureBox class. I'm explaining that only. – Sudipta Kumar Maiti Nov 22 '15 at 19:14