CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Form's Events

  1. #1
    Join Date
    Jun 2006
    Posts
    143

    Form's Events

    I have written an application in vb.net 2005. I need to execute some code every time a particular form in my app is activated.By 'activating' I mean whenever the form has to be repainted. Lets make it simple as follows:
    I need to display the message 'Hello' every time the form is repainted. Accordingly I coded the MessageBox.Show() method in the Activated() event. But whenever the form is activated the message box appears several times.ie.it reappears every time you press the OK button on the box and stops after some time. ie. the event is called several times.I tried coding it in the Paint() event.But again the same problem is there.I need to display the message box only once when the form is activated. Why is it so? Is that way the event is supposed to work? Paint() works as expected in vb.net 2003. The GotFocus() method does not seem to work at all.How can I do it in vb.net 2005 so that the code is executed ony once? Thanks in adavance...

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: Form's Events

    The problem is that by showing the messagebox over the form your causing the event(s) to be fired again when you close the messagebox.

  3. #3
    Join Date
    Jun 2006
    Posts
    143

    Re: Form's Events

    If that were the case, then why does the message box disappear after some time? If the event is fired again and again, the message box will keep appearing idenfeinitely,no?

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: Form's Events

    DSJ is correct. Let's go through it step by step.

    Activated event
    The form gets displayed - Activated
    The Messagebox gets shown - Deactivated
    The Messagebox gets closed - Activated

    GotFocus event
    It does work.
    The form gets displayed - Got focus
    The Messagebox gets shown - Lost Focus
    The Messagebox gets closed - Got focus

    etc. etc.

    I'd put in a Boolean flag, and then after the Messagebox is shown, just set it to true, something like :

    Form2
    Code:
    Public Class Form2
        Public Boxshown As Boolean
    
        Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
            If Not Boxshown Then
                Boxshown = True
                MessageBox.Show("test")
    
            End If
        End Sub
    
    End Class
    And because we want this box to show when Form2 gets activated ( meaning another form gets deactivated ), I'm setting the Boolean variable to False, in the particular form that gets Deactivated 's Deactivated event :

    Form1
    Code:
    Public Class Form1
        Private f2 As New Form2
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            f2.Show()
        End Sub
    
        Private Sub Form1_Deactivate(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Deactivate
            f2.Boxshown = False
        End Sub
    End Class
    Would you be happy with that ¿

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

Featured