Results 1 to 10 of 10

Thread: [RESOLVED] Creating A Component WithEvents In Code

  1. #1

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Resolved [RESOLVED] Creating A Component WithEvents In Code

    So I have a custom control that when added to a form will create two child controls inside it. The custom control's designer handles the creation of these. The problem I am having is that when the designer generates code for these controls only the parent control is declared "WithEvents". So my question is: how do I get my designer to create a component that will be declared WithEvents?

    Here is an example, if you add a standard TabControl onto your form and take a look at the designer code you will see them declared like this:

    vb.net Code:
    1. Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    2. Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    3. Friend WithEvents TabPage2 As System.Windows.Forms.TabPage

    The problem I am basically having is that the TabPages would be declared without "WithEvents":

    vb.net Code:
    1. Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    2. Friend TabPage1 As System.Windows.Forms.TabPage
    3. Friend TabPage2 As System.Windows.Forms.TabPage

    Any thoughts?

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Creating A Component WithEvents In Code

    How is your designer creating the components? In my wizard control (which creates WizardPages, much like TabPages), the WizardPages are declared WithEvents.

    My designer creates them like this:
    vb.net Code:
    1. Friend Sub OnAddPanel(ByVal sender As Object, ByVal e As EventArgs)
    2.         'Store oldpages for 'undo'
    3.         Dim oldPages As Control.ControlCollection = HostControl.Controls
    4.  
    5.         RaiseComponentChanging(TypeDescriptor.GetProperties(HostControl)("Pages"))
    6.  
    7.         'Create page using DesignerHost
    8.         Dim P As WizardPage = DirectCast(DesignerHost.CreateComponent(GetType(WizardPage)), WizardPage)
    9.         P.Title = P.Name
    10.         P.IsWelcomePage = False
    11.         P.IsLastPage = False
    12.         HostControl.Pages.Add(P)
    13.  
    14.         RaiseComponentChanged(TypeDescriptor.GetProperties(HostControl)("Pages"), oldPages, HostControl.Pages)
    15.         HostControl.SelectedPage = P
    16.  
    17.         SetVerbs()
    18.  
    19.     End Sub
    where DesignerHost is:
    vb.net Code:
    1. Public ReadOnly Property DesignerHost() As IDesignerHost
    2.         Get
    3.             If m_DesignerHost Is Nothing Then
    4.                 m_DesignerHost = DirectCast(GetService(GetType(IDesignerHost)), IDesignerHost)
    5.             End If
    6.             Return m_DesignerHost
    7.         End Get
    8.     End Property

    I tried changing the whole 'CreateComponent' stuff to simply creating a new instance (Dim P As New WizardPage) but that screws the whole control over; it seems it doesn't create the component at all then...

  3. #3

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Creating A Component WithEvents In Code

    Nick we literally do it the exact same way:

    vb.net Code:
    1. Private Sub AddTab()
    2.     Dim oldTabs As Control.ControlCollection = Me.HostControl.Controls
    3.  
    4.     Me.RaiseComponentChanging(TypeDescriptor.GetProperties(Me.HostControl)(VisualStudiosTabControlResources.TabPagesPropertyName))
    5.  
    6.     Dim tab As VisualStudiosTabPage = DirectCast(Me.DesignerHost.CreateComponent(GetType(VisualStudiosTabPage)), VisualStudiosTabPage)
    7.  
    8.     tab.Text = tab.Name.Replace(GetType(VisualStudiosTabPage).Name, "TabPage")
    9.     tab.Size = Me.HostControl.DefaultChildSize
    10.  
    11.     Me.HostControl.TabPages.Add(tab)
    12.  
    13.     Me.RaiseComponentChanged(TypeDescriptor.GetProperties(Me.HostControl)(VisualStudiosTabControlResources.TabPagesPropertyName), oldTabs, Me.HostControl.TabPages)
    14.     Me.HostControl.SelectedTabPage = tab
    15.  
    16.     Me.SetVerbs()
    17.     Me.SelectionService.SetSelectedComponents(New IComponent() {Me.Component})
    18. End Sub

  4. #4

  5. #5

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Creating A Component WithEvents In Code

    Mine inherits ScrollableControl. Would you mind switching yours to that to test if it still generates properly?

    Edit: If I switch to Panel it still doesn't work. I'm at a complete loss here...

  6. #6

  7. #7

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Creating A Component WithEvents In Code

    Quote Originally Posted by NickThissen View Post
    ScrollableControl works fine too with me. I've no idea what it could be then. I could ask someone who helped me with my control, perhaps they know a bit more...
    I would appreciate that Nick, thanks!

  8. #8
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Creating A Component WithEvents In Code

    According to the author of the PanelManager, the problem is that you nested your classes.

    Quote from email:
    There is a fair amount of reworking to do in the code to correct the issue.

    Nested Classes is what's causing the problems. It might look tidier, but VS does not like them.

    There will be some Friend modifiers needed in order to keep the majority of the code working, but once there are no more nested classes the WithEvents will work just as expected.
    Can you try that? That is indeed a difference between our versions so it could well be the problem (although I would find it a little strange..).

  9. #9

    Thread Starter
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Creating A Component WithEvents In Code

    I can't even believe it but that just worked. Nick - thank you man! Tell your buddy thanks too. If you don't mind, can you ask him how on earth he knew that?

  10. #10
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [RESOLVED] Creating A Component WithEvents In Code

    I asked him Wish he would join the forum, he knows everything about these designer related issues, while there seem to be very few members here (perhaps even none) with much knowledge of designers... By the way, the 'buddy' is just the author of the PanelManager as I said, got his email from his website which I'm sure you know.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width