Link to home
Start Free TrialLog in
Avatar of paulwhelan
paulwhelan

asked on

this.shown not allowed?

Hi

Im trying to add an event handler to show a message box when a property page is first opened.
But the Shown Event Hanlder doesnt appear in the property pages.



            //
            // ElementLabelControlPropertyPage
            //
            this.BackColor = System.Drawing.Color.Transparent;
            this.Controls.Add(this._oElementLabelDescriptionRadioButton);
            this.Controls.Add(this._oElementLabelNameRadioButton);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label2);
            this.Controls.Add(this._oElementLabelNameTextBox);
            this.Controls.Add(this._cInsertElementReferenceButton);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.label7);
            this.Location = new System.Drawing.Point(13, 13);
            this.Name = "ElementLabelControlPropertyPage";
            this.Size = new System.Drawing.Size(380, 291);
           


            //this.shown
            //only this.show comes up with intellisense


            this.Enter += new System.EventHandler(this.ElementLabelControlPropertyPage_Enter);
            this.VisibleChanged += new System.EventHandler(this.ElementLabelControlPropertyPage_VisibleChanged);
            this.Load += new System.EventHandler(this.ElementLabelControlPropertyPage_Load);
            this.ResumeLayout(false);
            this.PerformLayout();


Thanks
Paul


More info here.

https://www.experts-exchange.com/questions/21829411/this-ParentForm-Shown.html

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

isn't this event the one you are looking for?
 this.VisibleChanged += new System.EventHandler(this.ElementLabelControlPropertyPage_VisibleChanged);

and once you got into that event, you could remove the event handler...
Avatar of paulwhelan
paulwhelan

ASKER

angel

when I do

this.VisibleChanged += new System.EventHandler(this.ElementLabelControlPropertyPage_VisibleChanged);
           

and

private void ElementLabelControlPropertyPage_VisibleChanged(object sender, EventArgs e)
        {
            MessageBox.Show("test");
        }

It shows the messagebox twice!
Then the Property Page.



Thsi might help too (Im trying to do this within ElementLabelControlPropertyPage)

public class ElementLabelControlPropertyPage : PropertyPage

and

public class PropertyPage : UserControl
   
(UserControl is System.Windows.Forms.UserControl)

Thanks
If anyone is listening ;)

I added the Shown Event Handler to my Property Page

-------

    public class PropertyPage : UserControl
    {

        ...
        #region EVENT HANDLERS
        //
        // Summary:
        //     Occurs whenever the form is first displayed.
        public event EventHandler Shown;

        #endregion

        #region METHODS

        public virtual void OnApply() { }

       ...



-------

Now within
public class ElementLabelControlPropertyPage : PropertyPage
I can do
this.Shown += new System.EventHandler(this.ElementLabelControlPropertyPage_Shown);
and
private void ElementLabelControlPropertyPage_Shown(object sender, EventArgs e)
        {
            MessageBox.Show("Shown");
        }

But it doesn't show the messagebox at all.



           
Paul,

As this is your own custom event, where is the code that fires it? I take it somewhere you are calling something like:
if (this.Shown != null)
    this.Shown(this, new System.EventArgs());

Andy
Aren't you dropping your ElementLabelControlPropertyPage UserControl onto a Form?  If you are the Form will have the Shown event which is also when the UserControl is shown.  I'm not absolutely sure what you are trying to do.   Do you want your UserControl to realize when the main form it's on is shown?
Paul,

I think you may have been through a fair number of thought processes with this, so I'll just walk through what I would do to add a Shown event to the PropertyPage control and consume it from a form:
1) Add the Shown event to the PropertyPage control, as you have done
2) Create a private OnShown method to raise that event inside the PropertyPage control:
private void OnShown()
{
    if (this.Shown != null)
        this.Shown(this, new System.EventArgs());
}
3) Call OnShown from inside the PropertyPage control, as you were trying to do initially with the Shown event. However, I wouldn't do this inside InitializeComponent. Instead I would do it inside the Load event handler (double click on the usercontrol IDE surface to create an eventhandler for Load):

    private void ElementLabelControlPropertyPage_Load(object sender, EventArgs e)
    {
        ...
        this.OnShown();
        ...
    }
4) Consume the event from inside the form. You can use the property pane on the IDE to do this once you have dropped the control onto the form surface, or just add the appropriate "this.myElementLabelControlPropertyPage.Shown += blah" into your code.

As pauljk1619 says, I'm not exactly sure what you are trying to do with this, but that would be how I would do it. If you can tell us more about what you want to do we can probably comment further.

Andy
//Put this above your class declaration...
public delegate void PropertyGridShownEventHandler(System.Object sender, PropertyPage.PropertyGridShownEventArgs  e);  


//In you PropertyPageClass...

public event PropertyGridShownEventHandler PropertyGridShown;

public class PropertyGridShownEventArgs : System.EventArgs
   {
      // Any properties you want to show up in the event args.
      public string ShownOn;
   }

protected virtual void OnShown()
   {
      if(PropertyGridShown != null)
      {
         PropertyGridShownEventArgs e = PropertyGridShownEventArgs();
           e.ShownOn = System.DateTime.Now;
         PropertyGridShown(this,e);
      }

   }



//Then you call OnShown in your control where ever you want the event raised.
Thanks guys. Some good points and Ill work through them now.

I was wondering what made each of the Event Handlers unique.

I mean apart from the name not much seems to differentiate them

If I do

private void InitializeComponent()
        etc etc
            this.Enter += new System.EventHandler(this.ElementLabelControlPropertyPage_Enter);
            this.VisibleChanged += new System.EventHandler(this.ElementLabelControlPropertyPage_VisibleChanged);
            this.Load += new System.EventHandler(this.ElementLabelControlPropertyPage_Load);
            this.Shown += new EventHandler(this.ElementLabelControlPropertyPage_Shown);
           
and then

private void ElementLabelControlPropertyPage_Enter(object sender, EventArgs e)
        {
            MessageBox.Show("Enter");
        }
private void ElementLabelControlPropertyPage_VisibleChanged(object sender, EventArgs e)
        {
            MessageBox.Show("VisibleChanged");
        }
etc etc

Where is the actual difference?

Thanks
Paul
AGBrown

In PropertyPage I added

public event EventHandler Shown;

and

private void OnShown()
        {
            if (this.Shown != null)
                this.Shown(this, new EventArgs());
        }

Then in ElementLabelControlPropertyPage I did

this.Shown += new System.EventHandler(this.ElementLabelControlPropertyPage_Shown);

private void ElementLabelControlPropertyPage_Shown(object sender, EventArgs e)
        {
            MessageBox.Show("Shown");
        }


But it never loads up the messagebox?

Any ideas?

I'm working through the other suggestions
Thanks
Paul
           

Ok sorry for numerous post but ...

Now it goes into

private void ElementLabelControlPropertyPage_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Load");

            this.OnShown();

        }

and into

protected void OnShown()
        {
            if (this.Shown != null)
                this.Shown(this, new EventArgs());
        }

where this.Shown is null.

Is that correct?
It still doesn't show the messagebox after the page is drawn.

Thanks
Paul
AGBrown

I didnt understand this part

4) Consume the event from inside the form. You can use the property pane on the IDE to do this once you have dropped the control onto the form surface, or just add the appropriate "this.myElementLabelControlPropertyPage.Shown += blah" into your code.
ASKER CERTIFIED SOLUTION
Avatar of AGBrown
AGBrown
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi AGBrown,

I really want to create an eventhandler just for the
ElementLabelControlPropertyPage

The odd thing is is that if I put an Onload EventHandler into the page ElementLabelControlPropertyPage it shows before the page loads. I don't think thats correct.

The problem is that ElementLabelControlPropertyPage inherits from PropertyPage and not Form.

When I inherit from Form it allows me to add a Shown EventHandler.

So then I added a shown to the PropertyPage and tried to use that but to no avail.

Thanks
Paul