• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 370
  • Last Modified:

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.

http://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21829411.html

0
paulwhelan
Asked:
paulwhelan
  • 7
  • 3
  • 2
  • +1
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
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...
0
 
paulwhelanAuthor Commented:
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
0
 
paulwhelanAuthor Commented:
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.



           
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AGBrownCommented:
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
0
 
pauljk1619Commented:
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?
0
 
AGBrownCommented:
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
0
 
pauljk1619Commented:
//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.
0
 
paulwhelanAuthor Commented:
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
0
 
paulwhelanAuthor Commented:
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
           

0
 
paulwhelanAuthor Commented:
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
0
 
paulwhelanAuthor Commented:
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.
0
 
AGBrownCommented:
Hi Paul,

Just to make sure I have the structure of the classes and the actual forms right (otherwise my upcoming comments could be completely misleading), can you confirm:

1) You have a control names PropertyPage which inherits from UserControl
2) This control is being used on a Form
3) You want to, in the code for the form, create an event handler for the PropertyPage

Because, at the moment, it seems like you are trying to create an event handler for the Shown event _inside_ the PropertyPage control - is that right? I'm just confused about where the following code is located:
this.Shown += new System.EventHandler(this.ElementLabelControlPropertyPage_Shown);

private void ElementLabelControlPropertyPage_Shown(object sender, EventArgs e)
        {
            MessageBox.Show("Shown");
        }
0
 
paulwhelanAuthor Commented:
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
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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