Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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?

  • *Experts*
Posted

I'm sorry, but that's a button that really wants to do its job!!!!

 

give this a try:

       If MyBase.DesignMode = False Then
            Me.ParentForm.Close()
       End If

Member, in good standing, of the elite fraternity of mentally challenged programmers.

 

Dolphins Software

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...