Solved

What event fires when "Normal" ControlBox is clicked on a form?

Posted on 2014-01-24
7
490 Views
Last Modified: 2014-01-27
I need to check when a parent or child form's "normal" controlbox is clicked and I'm not sure which event to check. Is this possible?

Thanks!
0
Comment
Question by:BlakeMcKenna
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 40
ID: 39807787
If by "normal" controlbox you mean the little X at the upper right side of the window, the event is FormClosing. But that event can also be called by something else than the Control Box, so if you want to be specific, you need to look at the parameters passed to the event to determine what is causing the Close:
	Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing

		If e.CloseReason = CloseReason.UserClosing Then
			'ControlBox
		Else
			'Something else
		End If

	End Sub

Open in new window

0
 

Author Comment

by:BlakeMcKenna
ID: 39807807
Actually it's the Maximize/Minimize button (the middle button) that I'm try to capture.
0
 
LVL 20

Accepted Solution

by:
hes earned 500 total points
ID: 39809133
Private Sub Form_Resize()
    if Me.WindowState=vbMinimized Then
        'it was just minimized
    end if
End Sub
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 40
ID: 39809369
If Blake is on the right track about what you meant "normal" ControlBox, then I need to change my answer.

And sorry to correct Blake, what you meant is more the Restore button. The Maximize and Minimize buttons (plural) are 2 different buttons that appear when the form is neither maximized or minimized. When the form is Maximized, the Maximize button is useless and replaced by a Restore button. However, some might see it as an alternate state of the Maximize button.

Anyway, there is a little problem. This one looks like a simple one, but when you start looking at it carefully, it's not so simple. Maybe someone will have a better idea than mine.

The resizing events (ResizeBegin, Resize and ResizeEnd) can be triggered in different ways:

1. Clicking either the Minimize, Maximize or Restore button.
2. Programmatically changing the Size property of the Form.
3. User resizing the Form by dragging its border.

There is no built-in way that I know to determine which of these 3 triggered the resizing, since these events do not pass any useful parameter.

A moderately complex algorithm could be written, but I started working on it and hit a problem after a few seconds, and do not have time to go further with it right now. So I'm just pushing my thoughts about it in case somebody has the time to work on it.

It would involve, between others, a variable that is incremented on each pass in the Resize event and a Timer that would be created on the first call. After a short while, the timer would look at the variable to determine if the event is called only once or repetitively. If called only once, then we eliminated item 3 in my list of triggers for the event.

The first problem I hit is to find a way to make the difference between a Restore by the user and a resize by the code.

The second one is that in order to know if the unique event call could be triggered as well by a Maximize, a Minimize or a Restore, which brings us back almost to square one. Knowing that the Restore button is available only when the form is Maximized, we would need to keep a constant status of the WindowState in order to know that the Resize started in that state.

There might be other hiccups I did not see.

@hes

Your answer shows your VB6 background, if I am not mislead.

Resize has its use sometimes, but most of the time I see it used in VB.NET, the wrong event was chosen. This probably due to the fact that this was the only resizing event in VB6, and "old hands" are used to it.

The problem is that it triggers many times when the user is resizing a form. In most situations where you want to react to a resizing, you do not need to "follow" the operation, you usually need to react once, either before the resizing begin, most often once the resizing is done. For that purpose the "new" (well, they've been there for 12 years but when you hear of them for the first time, they're kind of new) ResizeBegin and ResizeEnd as most of the time a better choice than Resize.

In your specific example, if the user was resizing manually, the If would be called repetitively without ever being useful, because you cannot minimize a Form while dragging it's border. Sure it would work if the user hit the Minimize button, but would be useless for a manual resize. And in some circumstances, if the code in the If is important, it make the manual resizing very jerky (not sure if it is the right word in English, excuse my French).
0
 
LVL 27

Expert Comment

by:Ark
ID: 39809750
Public Class Form1
    Private Const WM_NCLBUTTONDOWN = &HA1
    Private Enum HitTestResult
        HTERROR = (-2)
        HTTRANSPARENT = (-1)
        HTNOWHERE = 0
        HTCLIENT = 1
        HTCAPTION = 2
        HTSYSMENU = 3
        HTGROWBOX = 4
        HTMENU = 5
        HTHSCROLL = 6
        HTVSCROLL = 7
        HTMINBUTTON = 8
        HTREDUCE = HTMINBUTTON
        HTMAXBUTTON = 9
        HTZOOM = HTMAXBUTTON
        HTLEFT = 10
        HTRIGHT = 11
        HTTOP = 12
        HTTOPLEFT = 13
        HTTOPRIGHT = 14
        HTBOTTOM = 15
        HTBOTTOMLEFT = 16
        HTBOTTOMRIGHT = 17
        HTBORDER = 18
        HTOBJECT = 19
        HTCLOSE = 20
        HTHELP = 21
    End Enum

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        If m.Msg = WM_NCLBUTTONDOWN Then
            Call ControlBoxClicked(m)
        End If
        MyBase.WndProc(m)
    End Sub

    Private Sub ControlBoxClicked(ByVal m As System.Windows.Forms.Message)
        Dim hitTest As HitTestResult = m.WParam
        Select Case hitTest
            Case HitTestResult.HTMINBUTTON
                Debug.Print("Min button down")
            Case HitTestResult.HTMAXBUTTON
                If Me.WindowState = FormWindowState.Maximized Then
                    Debug.Print("Restore button down")
                Else
                    Debug.Print("Max button down")
                End If
            Case HitTestResult.HTCLOSE
                Debug.Print("Close button down")
        End Select
    End Sub
End Class

Open in new window

0
 
LVL 20

Expert Comment

by:hes
ID: 39810749
<< if I am not mislead.>> Yes you are
0
 

Author Comment

by:BlakeMcKenna
ID: 39812037
Hes,

You were the closest. I actually put the code in the Resize() event. Looks like this.

    Private Sub frmMain_Resize(sender As Object, e As EventArgs) Handles Me.Resize
        If Me.WindowState = FormWindowState.Normal Then
            ResizeMDIParent(Me)
        End If
    End Sub

Open in new window


Wow, I don't why I made this so hard.

Thanks for the help guys!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now