[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:
Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
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:
Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
Friend TabPage1 As System.Windows.Forms.TabPage
Friend TabPage2 As System.Windows.Forms.TabPage
Any thoughts?
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:
Friend Sub OnAddPanel(ByVal sender As Object, ByVal e As EventArgs)
'Store oldpages for 'undo'
Dim oldPages As Control.ControlCollection = HostControl.Controls
RaiseComponentChanging(TypeDescriptor.GetProperties(HostControl)("Pages"))
'Create page using DesignerHost
Dim P As WizardPage = DirectCast(DesignerHost.CreateComponent(GetType(WizardPage)), WizardPage)
P.Title = P.Name
P.IsWelcomePage = False
P.IsLastPage = False
HostControl.Pages.Add(P)
RaiseComponentChanged(TypeDescriptor.GetProperties(HostControl)("Pages"), oldPages, HostControl.Pages)
HostControl.SelectedPage = P
SetVerbs()
End Sub
where DesignerHost is:
vb.net Code:
Public ReadOnly Property DesignerHost() As IDesignerHost
Get
If m_DesignerHost Is Nothing Then
m_DesignerHost = DirectCast(GetService(GetType(IDesignerHost)), IDesignerHost)
End If
Return m_DesignerHost
End Get
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...
Re: Creating A Component WithEvents In Code
Nick we literally do it the exact same way:
vb.net Code:
Private Sub AddTab()
Dim oldTabs As Control.ControlCollection = Me.HostControl.Controls
Me.RaiseComponentChanging(TypeDescriptor.GetProperties(Me.HostControl)(VisualStudiosTabControlResources.TabPagesPropertyName))
Dim tab As VisualStudiosTabPage = DirectCast(Me.DesignerHost.CreateComponent(GetType(VisualStudiosTabPage)), VisualStudiosTabPage)
tab.Text = tab.Name.Replace(GetType(VisualStudiosTabPage).Name, "TabPage")
tab.Size = Me.HostControl.DefaultChildSize
Me.HostControl.TabPages.Add(tab)
Me.RaiseComponentChanged(TypeDescriptor.GetProperties(Me.HostControl)(VisualStudiosTabControlResources.TabPagesPropertyName), oldTabs, Me.HostControl.TabPages)
Me.HostControl.SelectedTabPage = tab
Me.SetVerbs()
Me.SelectionService.SetSelectedComponents(New IComponent() {Me.Component})
End Sub
Re: Creating A Component WithEvents In Code
Then I truly have no idea. What is the VisualStudiosTabPage class inheriting? My WizardPage inherits Panel... Perhaps the difference lies there?
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...
Re: Creating A Component WithEvents In Code
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...
Re: Creating A Component WithEvents In Code
Quote:
Originally Posted by
NickThissen
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!
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:
Quote:
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..).
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?
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.