UserControl closes form in design view

HardCode

Freshman
Joined
Apr 2, 2004
Messages
49
I created a simple user control for Windows Forms for some learning experience, but I will use it in a production program. The control is basically a Button control dragged into the UserControl draw area, with one event:

Visual Basic:
Public Class ucCloseButton
    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

#Region "Events"

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
               Handles btnClose.Click
        Me.ParentForm.Close()
    End Sub

#End Region

End Class

I can drop this UC on my other forms, and the button will close the form that it is on. I figured I can avoid having to code the btnClose event in every form that I create by just using this control.

However! The button actually closes the form in DESIGN time as well as runtime. If I am designing the form and I click the control, the form disappears from the design window! Ack! Is there a way that I can tell my control to only be active during run-time to fix this bug I have created? Do UCs have a different conception of run-time?
 
I'm sorry, but that's a button that really wants to do its job!!!!

give this a try:
Visual Basic:
        If MyBase.DesignMode = False Then
             Me.ParentForm.Close()
        End If
 
Thanks for the tip on determining DesignMode.

Interestingly, this behavior seems to be a bug with the Panel control. Let me explain ...

I added a Panel at the bottom of my form to contain the Close UC that was already on the form. So, I dragged the Close control into the Panel, and voila! the behavior started. By deleting the control and re-adding it from the tool box directly onto the Panel, the behavior went away. Strange! Someone at XTreme VB Talk replicated this odd behavior on their system too (this is VS.NET 2003) and commented that nested controls in VB6 worked the same way. She was surprised the behavior "carried over" into .NET.

However, seems with that code snippet you provided I can be sure that it won't happen again! Thanks.
 
Back
Top