Results 1 to 6 of 6

Thread: Cannot Maximize form after it has been Minimized

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    144

    Cannot Maximize form after it has been Minimized

    After I minimize the main form, the form will stay minimized no matter what I do. I need to right click the program icon in the task bar and close it to restart the program. After some troubleshooting and research in this forum I found the code that is causing the problem. It is shown below in blue. If I comment out this code the problem is gone.

    When I change the focus away from this form, like clicking on a browser, this code works as expected and finds all the objects I need in ManagementObjectSearcher. But, when the form is minimized, it will stay minimized unless I close the program. This should be an easy fix, but I don't know what it is. The only thing that comes to mind is to use a BackGroundWorker. Any ideas? Thanks for reading.

    Code:
        Private Sub MainForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
    
            Try
               'SOME CODE HERE ......
    
                Dim DummyString08 As String = ""
    
                Dim searcher As New ManagementObjectSearcher(
               "root\cimv2",
               "SELECT * FROM Win32_PnPEntity")
    
                For Each queryObj As ManagementObject In searcher.Get()
                    If Len(queryObj("Name")) > 10 Then
                        DummyString08 = Trim(LSet(queryObj("Name"), 14))
                        If LSet(DummyString08, 10) = "MyBoard" Then
                            lbxPairedBoards.Items.Add(DummyString08)
                        End If
                    End If
                Next
    
            Catch err As ManagementException
                Dim el As New ErrorLogger
                el.WriteToErrorLog(err.Message, err.StackTrace, "Error")
            End Try
    
        End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,502

    Re: Cannot Maximize form after it has been Minimized

    Does the code get executed as expected when the form is minimised? You need to find out what is actually happening.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    144

    Re: Cannot Maximize form after it has been Minimized

    Quote Originally Posted by jmcilhinney View Post
    Does the code get executed as expected when the form is minimised? You need to find out what is actually happening.
    This code writes certain devices detected by the computer in a ListBox, so when I minimize the window I can't see if it worked. If I change the focus away from the program and go back to it, this code works. This code takes some time, probably a couple of seconds, to detect all the devices. I think that this delay might be the cause of the problem.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,502

    Re: Cannot Maximize form after it has been Minimized

    Quote Originally Posted by VB-MCU-User View Post
    This code writes certain devices detected by the computer in a ListBox, so when I minimize the window I can't see if it worked.
    Then it's a good thing that developers have additional tools available to them that regular users do not, the most important being the debugger built into VS.
    Quote Originally Posted by VB-MCU-User View Post
    This code takes some time, probably a couple of seconds, to detect all the devices. I think that this delay might be the cause of the problem.
    Then you probably shouldn't be doing it on the UI thread regardless. You could use Async/Await or, if you feel more comfortable, you could use a BackgroundWorker. If you do the latter, you would gather the data in the DoWork event handler and then either update the UI incrementally as you are but in the ProgressChanged event handler, or gather all the data first and then update the UI in the RunWorkerCompleted event handler. You would also want to check the IsBusy property first and only kick off the process if its not already in progress.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    144

    Re: Cannot Maximize form after it has been Minimized

    Quote Originally Posted by jmcilhinney View Post
    Then it's a good thing that developers have additional tools available to them that regular users do not, the most important being the debugger built into VS.
    Yes, I know. I'm learning.

    Quote Originally Posted by jmcilhinney View Post
    Then you probably shouldn't be doing it on the UI thread regardless. You could use Async/Await or, if you feel more comfortable, you could use a BackgroundWorker. If you do the latter, you would gather the data in the DoWork event handler and then either update the UI incrementally as you are but in the ProgressChanged event handler, or gather all the data first and then update the UI in the RunWorkerCompleted event handler. You would also want to check the IsBusy property first and only kick off the process if its not already in progress.
    Thanks, I used the BackgroundWorker and it worked. I used the DoWork event handler to update the UI incrementally. Below is my modified code. I used the INVOKE method to write to a ListBox because the ListBox was created in the UI thread. Is this correct?

    Code:
        Private Sub MainForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
    
            Try
                'SOME CODE HERE ......
    
                BackgroundWorker1.RunWorkerAsync()
    
            Catch err As ManagementException
                Dim el As New ErrorLogger
                el.WriteToErrorLog(err.Message, err.StackTrace, "Error")
            End Try
    
        End Sub
    
        Private Sub BackgroundWorker1_DoWork(sender As Object, e As ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    
                Dim DummyString08 As String = ""
    
                Dim searcher As New ManagementObjectSearcher(
               "root\cimv2",
               "SELECT * FROM Win32_PnPEntity")
    
                For Each queryObj As ManagementObject In searcher.Get()
                    If Len(queryObj("Name")) > 10 Then
                        DummyString08 = Trim(LSet(queryObj("Name"), 14))
                        If LSet(DummyString08, 10) = "MyBoard" Then
                            'lbxPairedBoards.Items.Add(DummyString08)
    
                        lbxPairedBoards.Invoke(Sub()
                                                   lbxPairedBoards.Items.Add(DummyString08)
                                               End Sub)
    
                        End If
                    End If
                Next
    
        End Sub

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,502

    Re: Cannot Maximize form after it has been Minimized

    It's not incorrect but the whole point of the BackgroundWorker is that you can call methods and handle events like normal and not have to invoke any delegates directly. If only I told you exactly what to do in my previous post:
    update the UI incrementally as you are but in the ProgressChanged event handler
    Did you bother to investigate that at all? I'm guessing not. Follow the CodeBank link in my signature below and check out my thread on Using The BackgroundWorker for some explanation and examples.

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