none
How to control what happens when a minimized form returns to normal after being clicked in the taskbar RRS feed

  • Question

  • My program's main form has a variable ("wb") represented an active Word application. When I minimize the form, I want Word to minimize; when I "normalize" the form, I want Word to return to the Normal windowstate. I think the LocationChanged event is activated when the form is minimized and then normalized, and it DOES when minimizing, but maybe not when normalizing.

    I tried this:

        Private Sub MainFrm_LocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged
            If Not IsWord() Then Exit Sub
    
            If Me.WindowState = FormWindowState.Minimized Then
                wd.WindowState = WdWindowState.wdWindowStateMinimize
            ElseIf Me.WindowState = FormWindowState.Normal And wd.WindowState = WdWindowState.wdWindowStateMinimize Then
                wd.WindowState = WdWindowState.wdWindowStateNormal
                Me.Activate()
            End If
        End Sub
    
    "IsWord" is my function to determine if Word is running. The above code runs when minimizing the Main form ("MainFrm"), but not when clicking on the app name in the Taskbar to return the form to Normal. In fact, I CAN'T return the form to Normal when Word is running -- clicking the button in the Taskbar doesn't seem to work at all. If it does, then what event should I use to control what happens then?


    Robert Homes

    Tuesday, December 19, 2017 2:34 AM

All replies

  • When I minimize the form, I want Word to minimize; when I "normalize" the form, I want Word to return to the Normal windowstate.

    Use the SizeChanged event, and test for whether the form window state is now minimized or normal.
    https://msdn.microsoft.com/en-us/library/system.windows.forms.control.sizechanged(v=vs.110).aspx
    https://msdn.microsoft.com/en-us/library/system.windows.forms.form.windowstate(v=vs.110).aspx

    Tuesday, December 19, 2017 2:47 AM
  • Hi Robert,

    I cannot know what "Me" is. But if it is your Windows Form, you can use the event of "SizeChanged", instead of "LocationChanged".
     Private Sub MainFrm_LocationChanged(sender As Object, e As EventArgs) _
     Handles Me.SizeChanged
    I hope this will be helpful.

    Regards,

    Ashidacchi

    Tuesday, December 19, 2017 2:50 AM
  • Ashidacchi,

    Using the SizeChanged event didn't help. I still can't click the button on the minimized App in the Taskbar to return it to Normal. I did find, though, that I can right-click on the button to display a brief context menu, then click the App's name on the menu, and THAT does return the App to Normal.


    Robert Homes

    Tuesday, December 19, 2017 3:43 PM
  • I still can't click the button on the minimized App in the Taskbar to return it to Normal.

    Clicking on the minimized app to invoke a button on that app was not part of your original problem description. You can't click a button on a minimized form. Explorer is managing the task bar - use your standard task bar functions to restore your form, then click the button, or use the context menu provided in the task bar, as described.

    If your original problem "when I "normalize" the form, I want Word to return to the Normal windowstate" has now been resolved you should mark those responses were useful as answers.

    Tuesday, December 19, 2017 8:15 PM
  • Hi Robert,

    I'm afraid you don't know how to code in SizeChanged sub.
    Please provide your current code.

    Regards,

    Ashidacchi

    Tuesday, December 19, 2017 11:12 PM
  •  You will need to create and post a small example that will reproduce the problem and give us any details such as the properties of the form you have changed,  if you have not set them in the example code where we can see them.  Using the code below seems to work flawlessly for me and i can not reproduce the problem.

    Imports Microsoft.Office.Interop
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private wd As New Word.Application
        Private WordHandle As IntPtr = IntPtr.Zero
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            wd.Visible = True
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            If wd IsNot Nothing Then
                wd.Quit(True)
                Marshal.ReleaseComObject(wd)
                wd = Nothing
            End If
        End Sub
    
        Private Sub Form1_LocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged
            If Me.WindowState = FormWindowState.Minimized Then
                wd.WindowState = Word.WdWindowState.wdWindowStateMinimize
            ElseIf Me.WindowState = FormWindowState.Normal AndAlso wd.WindowState = Word.WdWindowState.wdWindowStateMinimize Then
                wd.WindowState = Word.WdWindowState.wdWindowStateNormal
                Me.Activate()
            End If
        End Sub
    End Class
    
    
     

     


    If you say it can`t be done then i`ll try it

    Wednesday, December 20, 2017 2:26 AM
  • Ashidacchi,

    Using the SizeChanged event didn't help. I still can't click the button on the minimized App in the Taskbar to return it to Normal. I did find, though, that I can right-click on the button to display a brief context menu, then click the App's name on the menu, and THAT does return the App to Normal.


    Robert Homes

    I think this can happen if there is no restore size and location saved for the window.

    What happens if instead of "normal" you maximize the form from the task bar pop up preview?

    How does the form get minimized?

    Also is the form borderless?

    PS Are you getting an error in the event when you set to restore?

    Also is this the main application form or a child form?

    Wednesday, December 20, 2017 2:49 AM
  • What does this mean/do and why is it needed?

    Marshal.ReleaseComObject(wd)

    My program does not allow itself to be maximized.

    When I create the Word application, I do it two ways, (1) create a new instance, or (2) grab an existing instance. I can show you all that code, and maybe you can help me refine it. It seems to work okay for me as it is, though.

    I'll have to get back to you re the rest of this, don't have time right now.

    One thing I could use help with is how to get my program to respond to Word events. Don't know how to do that.


    Robert Homes

    Wednesday, December 20, 2017 4:10 PM
  • Tommy:

    See my response just above to "IronRazerz". The form gets minimized by manually clicking on the "-" button at the upper right of the form. The form has a border. No error is raised, its just that nothing happens. The form is the Main form for the app.

    I'm afraid I might be doing something else with the relationship between my app and Word that is possibly affecting this. So far, I can't find anything like that, but I need to keep looking.

    I should say that I don't get the error unless my program has created or adopted an instance of Word. Word is minimized when the form is minimized, but then I can't restore the app from the taskbar; I CAN restore Word though.

    I'll get back to you after Christmas or maybe New Year.


    Robert Homes

    Wednesday, December 20, 2017 4:16 PM
  • When I create the Word application, I do it two ways, (1) create a new instance, or (2) grab an existing instance. I can show you all that code, and maybe you can help me refine it. It seems to work okay for me as it is, though.

    I'll have to get back to you re the rest of this, don't have time right now.

    One thing I could use help with is how to get my program to respond to Word events. Don't know how to do that.


    Robert Homes

    Like to see how you create the word application, both ways.

    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it" - MSDN User JohnWein

    Wednesday, December 20, 2017 4:42 PM
  •  Rob,

     When you use the Microsoft.Office.Interop to create a Word application,  it is a Com Object.  To properly release any references to Com Objects in your managed .Net application,  you Marshal.ReleaseComObject Method.  Read the Remarks section at that link to find more detailed information.

     You can use the WithEvents keyword when you declare the Word application.  This will give you access to the events shown below.

     

     This seems to work when creating a new instance of a Word application,  or when i attach to an existing Word application.  It uses the WithEvents keyword as mentioned above too.

    Imports Microsoft.Office.Interop
    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Private WithEvents wd As Word.Application = Nothing
        Private WordHandle As IntPtr = IntPtr.Zero
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            CloseAndReleaseWordComObject()
        End Sub
    
        Private Sub MainFrm_LocationChanged(sender As Object, e As EventArgs) Handles Me.LocationChanged
            If wd IsNot Nothing Then
                If Me.WindowState = FormWindowState.Minimized Then
                    Try
                        wd.WindowState = Word.WdWindowState.wdWindowStateMinimize
                    Catch ex As Exception
                        CloseAndReleaseWordComObject()
                    End Try
                ElseIf Me.WindowState = FormWindowState.Normal Then
                    Try
                        wd.WindowState = Word.WdWindowState.wdWindowStateNormal
                        Me.Activate()
                    Catch ex As Exception
                        CloseAndReleaseWordComObject()
                    End Try
                End If
            End If
        End Sub
    
        'creates a new Word application
        Private Sub Button_CreateNew_Click(sender As Object, e As EventArgs) Handles Button_CreateNew.Click
            CloseAndReleaseWordComObject()
            wd = New Word.Application
            wd.Visible = True
        End Sub
    
        'attaches to an existing word application
        Private Sub Button_AttachToExisting_Click(sender As Object, e As EventArgs) Handles Button_AttachToExisting.Click
            CloseAndReleaseWordComObject()
            Try
                wd = CType(Marshal.GetActiveObject("Word.Application"), Word.Application)
            Catch ex As Exception
                MessageBox.Show("No Active Word Application Was Available To Attach To.")
            End Try
        End Sub
    
        Private Sub CloseAndReleaseWordComObject()
            If wd IsNot Nothing Then
                Try
                    wd.Quit(True) 'close the Word application and save any changes.
                Catch ex As Exception
                End Try
                Marshal.ReleaseComObject(wd) 'release the Word Com Object
                wd = Nothing
            End If
        End Sub
    End Class
    
    


    If you say it can`t be done then i`ll try it

    Wednesday, December 20, 2017 7:34 PM