none
Focus(?) issues RRS feed

  • Question

  • VB 2010 express: My application has a main form (form1 equivalent), with other forms popping up.  After disposing of these forms, sometimes the main form disappears (goes to minimize, it seems).  I added "mainform.activate()" just before the dispose() statements and additionally right after the showdialog() statements.  However, the issue (while less), still happens. Any idea?

    Peter Goossens

    Thursday, September 20, 2012 3:03 PM

Answers

  • Peter,

    OOP means working with objects, you don't create much, you create instances which are references to objects and you use properties which are mostly also references to objects.

    So don't care to much anymore about size like in past. A .Net program is (if you don't include things like many complete images in the assembly) mostly very small.


    Success
    Cor

    • Marked as answer by Peter Goossens Friday, September 21, 2012 4:24 PM
    Friday, September 21, 2012 3:56 PM
  • Hi Peter,

    Welcome to the MSDN forum.

    I will suggest you to set the FormWindowState.Normal to the Form.WindowState Property to force your main Form active. Here is a small sample:

    Public Class Form5A
        Private WithEvents fm5b As Form5B
        Private Sub Form5A_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            fm5b = New Form5B
            fm5b.Show()
        End Sub
        Public Sub done() Handles fm5b.ActiveForm5A
            Me.WindowState = FormWindowState.Normal
        End Sub
    End Class
    
    Public Class Form5B
        Public Event ActiveForm5A()
        Private Sub Form5B_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            RaiseEvent ActiveForm5A()
        End Sub
    End Class

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Peter Goossens Friday, November 22, 2013 2:59 PM
    Friday, September 21, 2012 5:52 AM
    Moderator

All replies

  • Peter,

    provide your complete code!


    Pl. Mark/Proposed as Answer if found your solution Vote as Helpful if related to your topic. Always Motivate others by performing this Action.

    Thursday, September 20, 2012 3:14 PM
  • Wow, that would be big!  Pasting it into this window is not practical.  There must be something like 20,000 lines of code if not more.  Any other way I acn get that to you?

    Peter Goossens

    Thursday, September 20, 2012 4:33 PM
  • okay, 

    paste the error proposed area, for which you discussing in your question  - 

     I added "mainform.activate()" just before the dispose() statements and additionally right after the showdialog() statements. 


    Pl. Mark/Proposed as Answer if found your solution Vote as Helpful if related to your topic. Always Motivate others by performing this Action.

    Thursday, September 20, 2012 5:05 PM
  • Ok, appreciate the effort.  Let me figure out how to do that in a meaningfull and I will get back to you.  Thanks,


    Peter Goossens

    Thursday, September 20, 2012 6:24 PM
  • Hi Peter,

    Welcome to the MSDN forum.

    I will suggest you to set the FormWindowState.Normal to the Form.WindowState Property to force your main Form active. Here is a small sample:

    Public Class Form5A
        Private WithEvents fm5b As Form5B
        Private Sub Form5A_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            fm5b = New Form5B
            fm5b.Show()
        End Sub
        Public Sub done() Handles fm5b.ActiveForm5A
            Me.WindowState = FormWindowState.Normal
        End Sub
    End Class
    
    Public Class Form5B
        Public Event ActiveForm5A()
        Private Sub Form5B_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            RaiseEvent ActiveForm5A()
        End Sub
    End Class

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Peter Goossens Friday, November 22, 2013 2:59 PM
    Friday, September 21, 2012 5:52 AM
    Moderator
  • Peter,

    In what what way are you disposing the forms, be aware the meaning of the dispose method is not disposing an object, but removing unmanaged resources from an object and make it unusable (nothing more). Often the side effects of this are worst than the cure. 

    In past some developers (also at Microsoft) have overriden the dispose method to add obscure code and wrote then that it was good practise to use that method always when it did exist. However that is not in the forms class and is currently seldom done anymore.


    Success
    Cor

    Friday, September 21, 2012 6:11 AM
  • Thanks, Mark.  A follow-up question:

    If I have, let's say, 20 forms that can be called from "Form1", that would mean I need 20 statement like "Private WithEvents f2 as Form2"  

    If so:

    "The private widthevents f2 as form2" (* 20),  does that add significantly to the overall size of the application at runtime?  Does it create an addional instant of each?  (Each of these 20 forms has its own code etc)  In fact some forms can be called both from form1 as well as from any of the other forms.  So the number is bigger than 20.

    If not, it would be interesting to try.



    Peter Goossens

    Friday, September 21, 2012 1:58 PM
  • Peter,

    OOP means working with objects, you don't create much, you create instances which are references to objects and you use properties which are mostly also references to objects.

    So don't care to much anymore about size like in past. A .Net program is (if you don't include things like many complete images in the assembly) mostly very small.


    Success
    Cor

    • Marked as answer by Peter Goossens Friday, September 21, 2012 4:24 PM
    Friday, September 21, 2012 3:56 PM
  • Thanks.  Appreciate it!

    Peter Goossens

    Friday, September 21, 2012 4:27 PM
  • Interesting!! I wrote a little application trying to generate the same problems I have in my other one (see above and see my other thread).

    I pasted it below.  The culprit seems to be me.dispose() (as Cor Lighert suggested).

    If one changes all "Me.dispose()" to "Me.close()", it works perfectly and I don't see a difference in the "windows state" statements.  "Me.Dispose() runs into all kind of runtime issues.

    Public Class Class1
    
        Public Shared Sub DoShowing()
            Form2.ShowDialog()
            Form1.WindowState = FormWindowState.Normal
    
            Graphic1()
    
    
            Form3.ShowDialog()
            Form1.WindowState = FormWindowState.Normal
            Form4.ShowDialog()
            Form1.WindowState = FormWindowState.Normal
            Form5.ShowDialog()
            Form1.WindowState = FormWindowState.Normal
    
            Graphic2()
    
            Form6.ShowDialog()
            Form1.WindowState = FormWindowState.Normal
            Form7.ShowDialog()
            Form1.WindowState = FormWindowState.Normal
    
            Graphic3()
    
    
            Form8.ShowDialog()
            Form1.WindowState = FormWindowState.Normal
    
            Form1.Close()
        End Sub
    
        Private Shared Sub Graphic1()
            Form1.R.X = Form1.ClientRectangle.X + 300
            Form1.R.Y = Form1.ClientRectangle.Y + 200
            Form1.R.Width = 400
            Form1.R.Height = 400
            Dim MyBrush As New SolidBrush(Color.FromArgb(255, 53, 42, 13))
    
            Dim Color1 = Color.LightBlue
            Dim Color2 = Color.White
    
          
    
            Form1.GGG.FillRectangle(MyBrush, Form1.R)
    
            Dim Rnd = New Random()
            Dim Cter As Integer = 0
            Dim RR As Rectangle
    
          
            For I As Integer = 1 To 100000
                Dim X1 As Integer = Rnd.Next(Form1.R.X + 1, Form1.R.X + Form1.R.Width - 1)
                Dim Y1 As Integer = Rnd.Next(Form1.R.Y + 1, Form1.R.Y + Form1.R.Height - 1)
                RR.X = X1
                RR.Y = Y1
                RR.Width = 4
                RR.Height = 4
                Dim myBrush2 = New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(RR.X + 2, RR.Y + 2, RR.Width - 2, RR.Height - 2), Color1, Color2, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
                Form1.GGG.FillRectangle(myBrush2, RR)
            Next
    
        End Sub
    
        Private Shared Sub Graphic2()
            Form1.R.X = Form1.ClientRectangle.X + 300
            Form1.R.Y = Form1.ClientRectangle.Y + 200
            Form1.R.Width = 400
            Form1.R.Height = 400
            Dim MyBrush As New SolidBrush(Color.FromArgb(255, 53, 42, 13))
    
            Dim Color1 = Color.Brown
            Dim Color2 = Color.Red
    
    
    
            'Form1.GGG.FillRectangle(MyBrush, Form1.R)
    
            Dim Rnd = New Random()
            Dim Cter As Integer = 0
            Dim RR As Rectangle
    
    
            For I As Integer = 1 To 100000
                Dim X1 As Integer = Rnd.Next(Form1.R.X + 1, Form1.R.X + Form1.R.Width - 1)
                Dim Y1 As Integer = Rnd.Next(Form1.R.Y + 1, Form1.R.Y + Form1.R.Height - 1)
                RR.X = X1
                RR.Y = Y1
                RR.Width = 4
                RR.Height = 4
                Dim myBrush2 = New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(RR.X + 2, RR.Y + 2, RR.Width - 2, RR.Height - 2), Color1, Color2, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
                Form1.GGG.FillRectangle(myBrush2, RR)
            Next
    
    
    
    
    
    
        End Sub
    
        Private Shared Sub Graphic3()
            Form1.R.X = Form1.ClientRectangle.X + 300
            Form1.R.Y = Form1.ClientRectangle.Y + 200
            Form1.R.Width = 400
            Form1.R.Height = 400
            Dim MyBrush As New SolidBrush(Color.FromArgb(255, 53, 42, 13))
    
            Dim Color1 = Color.Brown
            Dim Color2 = Color.Red
    
    
    
            'Form1.GGG.FillRectangle(MyBrush, Form1.R)
    
            Dim Rnd = New Random()
            Dim Cter As Integer = 0
            Dim RR As Rectangle
    
    
            For I As Integer = 1 To 100000
                Dim X1 As Integer = Rnd.Next(Form1.R.X + 1, Form1.R.X + Form1.R.Width - 1)
                Dim Y1 As Integer = Rnd.Next(Form1.R.Y + 1, Form1.R.Y + Form1.R.Height - 1)
                RR.X = X1
                RR.Y = Y1
                RR.Width = 4
                RR.Height = 4
                Form1.GGG.FillRectangle(MyBrush, RR)
            Next
        End Sub
    
    End Class
    
    Public Class Class2
    
        Public Shared Function PathManagement(ByVal Mode As Integer, ByVal Form As Integer) As Integer ' Mode = 1 then Add  Mode = 2 Then substract  Mode = 3 Then reinitialize
            Dim Flag As Boolean = False
            Dim Result As Integer = 0
            If Mode = 1 Then
                For I As Integer = 2 To 10
                    If Form1.Indicator(I) = 0 Then
                        Form1.Indicator(I) = Form
                        Flag = True
                        Exit For
                    End If
                Next
                If Not Flag Then Result = -1
            End If
            If Mode = 2 Then
                For I As Integer = 10 To 2 Step -1
                    If Form1.Indicator(I) <> 0 Then
                        Result = Form1.Indicator(I - 1)
                        Flag = True
                        Form1.Indicator(I) = 0
                        Exit For
                    End If
                Next
                If Not Flag Then
                    Result = -1
                Else
                    If Result = 1 Then Form1.WindowState = FormWindowState.Normal
                    If Result = 2 Then Form2.WindowState = FormWindowState.Normal
                    If Result = 3 Then Form3.WindowState = FormWindowState.Normal
                    If Result = 4 Then Form4.WindowState = FormWindowState.Normal
                    If Result = 5 Then Form5.WindowState = FormWindowState.Normal
                    If Result = 6 Then Form6.WindowState = FormWindowState.Normal
                    If Result = 7 Then Form7.WindowState = FormWindowState.Normal
                    If Result = 8 Then Form8.WindowState = FormWindowState.Normal
                End If
            End If
            If Mode = 3 Then
                For I As Integer = 2 To 10
                    Form1.Indicator(I) = 0
                Next
                Form1.Indicator(1) = 1
            End If
            Return Result
        End Function
    
    End Class
    
    
    Public Class Form1
        Public Shared GGG As Graphics
        Public Shared R As Rectangle
    
        Public Shared Indicator(10) As Integer
    
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            For I As Integer = 1 To 10
                Indicator(I) = 0
            Next
            Indicator(1) = 1
    
            Me.Size = New System.Drawing.Size(1200, 800)
            Me.BackColor = Color.Wheat
            GGG = Me.CreateGraphics()
    
        End Sub
    
        Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
    
        End Sub
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Call Class1.DoShowing()
        End Sub
    End Class
    
    Public Class Form2
    
        Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim Ind As Integer = Class2.PathManagement(1, 2)
        End Sub
    
    
    
    
        Private Sub Form5B_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            Dim Ind = Class2.PathManagement(2, 0)
            Me.Dispose()
        End Sub
    
        Private Sub Form2_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
        End Sub
    End Class
    
    Public Class Form3
    
        'Public Event ActiveForm1()
        'Public Event ActiveForm7()
    
        Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim Ind As Integer = Class2.PathManagement(1, 3)
        End Sub
    
    
    
        Private Sub Form5B_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            Dim Ind = Class2.PathManagement(2, 0)
        End Sub
    
        Private Sub Form3_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Form7.ShowDialog()
            Me.Dispose()
        End Sub
    End Class
    
    Public Class Form4
    
    
        'Public Event ActiveForm1()
        'Public Event ActiveForm7()
    
        Private Sub Form4_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim Ind As Integer = Class2.PathManagement(1, 4)
        End Sub
    
    
        Private Sub Form5B_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            'If Form1.Indicator2 = 1 Then RaiseEvent ActiveForm1()
            'If Form1.Indicator2 = 2 Then RaiseEvent ActiveForm7()
            Dim Ind As Integer = Class2.PathManagement(2, 0)
            Me.Dispose()
        End Sub
       
    End Class
    
    Public Class Form5
    
    
        'Public Event ActiveForm1()
        'Private WithEvents Form6 As New Form6
    
        Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim Ind As Integer = Class2.PathManagement(1, 5)
        End Sub
    
    
        'Public Sub done() Handles Form6.ActiveForm5
        '    Me.WindowState = FormWindowState.Normal
        'End Sub
    
        Private Sub Form5B_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            Dim Ind As Integer = Class2.PathManagement(2, 0)
            Me.Dispose()
        End Sub
       
        Private Sub Form5_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
           
        End Sub
    End Class
    
    Public Class Form6
    
    
        'Public Event ActiveForm5()
        'Public Event Activeform1()
    
        Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.Size = New System.Drawing.Size(800, 300)
            Me.BackColor = Color.Wheat
            Dim Ind As Integer = Class2.PathManagement(1, 6)
    
        End Sub
    
    
    
    
    
        Private Sub Form5B_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            Dim Ind As Integer = Class2.PathManagement(2, 0)
            Me.dispose()
            'If Form1.Indicator = 1 Then RaiseEvent ActiveForm5()
            'If Form1.Indicator = 2 Then RaiseEvent Activeform1()
        End Sub
    
        Private Sub Form6_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            Dim RR As Rectangle
            RR.X = 100
            RR.Y = 50
            RR.Width = 500
            RR.Height = 150
            e.Graphics.DrawRectangle(Pens.Black, RR)
    
            Dim MyBrush As New SolidBrush(Color.FromArgb(255, 53, 42, 13))
            Dim Penp As New Pen(Color.FromArgb(255, 53, 42, 13))
    
            Dim Rnd = New Random()
            Dim Cter As Integer = 0
    
            For I As Integer = 1 To 50000
                Dim X1 As Integer = Rnd.Next(RR.X + 1, RR.X + RR.Width - 1)
                Dim Y1 As Integer = Rnd.Next(RR.Y + 1, RR.Y + RR.Height - 1)
                Dim x2 As Integer = Rnd.Next(RR.Y + 1, RR.Y + RR.Height - 1)
                Dim y2 As Integer = Rnd.Next(RR.Y + 1, RR.Y + RR.Height - 1)
                e.Graphics.DrawLine(Penp, X1, Y1, x2, y2)
            Next
    
    
        End Sub
    
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Me.dispose()
        End Sub
    End Class
    
    Public Class Form7
    
        ' Public Event ActiveForm3()
        '  Private WithEvents Form4 As New Form4
    
        Private Sub Form7_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.Size = New System.Drawing.Size(600, 300)
            Me.BackColor = Color.Blue
            Dim Ind As Integer = Class2.PathManagement(1, 7)
        End Sub
    
        'Public Sub done() Handles Form4.ActiveForm7
        '    Me.WindowState = FormWindowState.Normal
        'End Sub
    
    
        Private Sub Form5B_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            Dim Ind As Integer = Class2.PathManagement(2, 0)
            Me.dispose()
            ' RaiseEvent ActiveForm3()
        End Sub
    
        Private Sub Form7_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Form4.ShowDialog()
        End Sub
    End Class
    
    Public Class Form8
    
        Private Sub Form8_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            Dim Ind As Integer = Class2.PathManagement(2, 0)
            Me.dispose()
        End Sub
    
        Private Sub Form8_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim Ind As Integer = Class2.PathManagement(1, 8)
        End Sub
    
    
    End Class
    
    
    


    Peter Goossens

    Sunday, September 23, 2012 2:35 PM
  • A question:  If I were to not use "form.dispose()" but "Form.close() instead that means that I cannot dispose of the resources (listboxes, etc).  Does that then mean that the all stay current and is that then going to create other type of problems?


    Peter Goossens

    Sunday, September 23, 2012 8:36 PM
  • But then what?  I am (was) disposing of forms through me.dispose()


    Peter Goossens

    Monday, September 24, 2012 4:06 PM