[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1222
  • Last Modified:

OnLoad works but OnShown doesn't

Hi

I try this ...

#region EVENT HANDLERS

        protected override void OnLoad(EventArgs e)
        {
            MessageBox.Show("this is a test1");
        }

        protected void OnShown(EventArgs e)
        {
            MessageBox.Show("this is a test2");
        }

And the OnLoad Event gets called but the OnShown doesn't.

When I go to the properties and click events It has Load but not Shown.

Any ideas?
thanks
Paul
0
paulwhelan
Asked:
paulwhelan
  • 3
  • 3
1 Solution
 
paulwhelanAuthor Commented:
Also when I try to do

protected override void OnLoad(EventArgs e)
        {
            MessageBox.Show("this is a test1");
        }

        protected override void OnShown(EventArgs e)
        {
            MessageBox.Show("this is a test2");
        }

It says

Error      22      'DefaultControlsPlugin.Design.ControlPropertyPages.ElementLabelControlPropertyPage.OnShown(System.EventArgs)': no suitable method found to override      


And in this code theres aLoad but no Show

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Runtime.InteropServices;

namespace System.Windows.Forms
{
    // Summary:
    //     Provides an empty control that can be used to create other controls.
    [Designer("System.Windows.Forms.Design.ControlDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
    [ComVisible(true)]
    [ClassInterface(1)]
    [Designer("System.Windows.Forms.Design.UserControlDocumentDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", typeof(IRootDesigner))]
    [DefaultEvent("Load")]
    [DesignerCategory("UserControl")]
    public class UserControl : ContainerControl
    {
        // Summary:
        //     Initializes a new instance of the System.Windows.Forms.UserControl class.
        public UserControl();


.....

//
        // Summary:
        //     Raises the System.Windows.Forms.UserControl.Load event.
        //
        // Parameters:
        //   e:
        //     An System.EventArgs that contains the event data.
        [EditorBrowsable(2)]
        protected virtual void OnLoad(EventArgs e);
        [EditorBrowsable(2)]
0
 
dstanley9Commented:
OnShown is a method for .NET 2.0 windows Forms classes (not UserControls).
0
 
paulwhelanAuthor Commented:
Hi dstanley9

Thanks for that.

Can you explain a little more?
What do you mean that its a method for .NET 2.0 windows forms classes?

Can I now use it like OnLoad?

What method should be used to show the message box when the page is shown?

Thanks
Paul
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
dstanley9Commented:
Add the event handler to the form, not the user control (I'm assuming that's where it is based on the code that you posted and the error you received.

Also, it''s better to ADD event handlers rather than OVERRIDE them.  That way you don't kill any existing event handlers.

Here's a few tutorials on event handlers in general:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkEventsTutorial.asp
http://www.c-sharpcorner.com/Code/2002/Mar/EventHandlingInNetUsingCSDD001.asp

e.g.
    // in form or control constructor...
    this.OnLoad += new EventHandler(this_Load);
    // Assign the Shown event handler to the Shown event of the FORM
    this.ParentForm.Shown += new EventHandler(form_Shown);


#region EVENT HANDLERS

        private void this_Load(object sender, EventArgs e)
        {
            MessageBox.Show("this is a test1");
        }

        protected void form_Shown(object sender, EventArgs e)
        {
            MessageBox.Show("this is a test2");
        }
0
 
paulwhelanAuthor Commented:
Thanks.

I get an error when I try to do

this.OnLoad += new System.EventHandler(this.this_Load);

Error      24      Cannot assign to 'OnLoad' because it is a 'method group'      

Paul
0
 
dstanley9Commented:
Sorry, that should be

this.Load += new System.EventHandler(this.this_Load);
0

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now