CheckBox not work properly when clicked on quickly multiple times - vb.net

What this code do:
If parent node is checked/unchecked, also check/uncheck all child nodes.
If just one child node is checked, also check parent node.
Private Sub TreeView1_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterCheck
If updatingTreeView Then Return
updatingTreeView = True
CheckNode(e.Node, e.Node.Checked)
HasCheckedChildNode = 0
updatingTreeView = False
End Sub
Private Sub CheckNode(node As TreeNode, isChecked As Boolean)
If node.Parent IsNot Nothing Then
HasCheckedNode(node.Parent)
If Not isChecked And HasCheckedChildNode > 0 Then Return
node.Parent.Checked = isChecked
ElseIf node.Parent Is Nothing Then
For Each cn As TreeNode In node.Nodes
cn.Checked = isChecked
Next
End If
End Sub
Private Sub HasCheckedNode(node As TreeNode)
For Each cn As TreeNode In node.Nodes
If cn.Checked = True Then
HasCheckedChildNode += 1
ElseIf cn.Checked = False Then
HasCheckedChildNode -= 0
End If
Next
End Sub
This code works fine.
Problem:
When I clicks quickly some of the checkboxes are checked and some no
E.g. Sometimes I checked the parent node but all child nodes still remain unchecked. Sometimes the parent node is unchecked but its child nodes still checked.
Please check the example image:
How to solve this, is this a problem with my PC?

That happens because the TreeView by default doesn't toggle the Check property of the TreeNode objects on mouse double click over the check box area. You need to intercept the WM_LBUTTONDBLCLK messages, get TreeViewHitTestInfo of the double clicked point, and toggle the Check property if the double clicked point is over the check box.
Here's a custom TreeView for that, also it solves the main issue, checking/unchecking the parent and child nodes of the branch, just enable the AutoCheckParents and/or AutoCheckChildren properties for that.
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
<DesignerCategory("Code")>
Public Class DoubleClickCheckTreeView
Inherits TreeView
#Region "Properties"
<Category("Behavior"),
DefaultValue(False)>
Public Property AutoCheckParents As Boolean = False
<Category("Behavior"),
DefaultValue(False)>
Public Property AutoCheckChildren As Boolean = False
#End Region
#Region "Overrides"
'Enable DoubleBuffered to reduce the flickering.
Protected Overrides Sub OnHandleCreated(e As EventArgs)
SendMessage(Handle,
TVM_SETEXTENDEDSTYLE,
IntPtr.op_Explicit(TVS_EX_DOUBLEBUFFER),
IntPtr.op_Explicit(TVS_EX_DOUBLEBUFFER))
MyBase.OnHandleCreated(e)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_LBUTTONDBLCLK AndAlso CheckBoxes Then
Dim x As Integer = m.LParam.ToInt32() And &HFFFF
Dim y As Integer = (m.LParam.ToInt32 >> 16) And &HFFFF
Dim ht As TreeViewHitTestInfo = HitTest(x, y)
If ht.Node IsNot Nothing AndAlso
ht.Location = TreeViewHitTestLocations.StateImage Then
OnBeforeCheck(New TreeViewCancelEventArgs(ht.Node,
False,
TreeViewAction.ByMouse))
ht.Node.Checked = Not ht.Node.Checked
OnAfterCheck(New TreeViewEventArgs(ht.Node, TreeViewAction.ByMouse))
m.Result = IntPtr.Zero
Return
End If
End If
MyBase.WndProc(m)
End Sub
Protected Overrides Sub OnAfterCheck(e As TreeViewEventArgs)
MyBase.OnAfterCheck(e)
If e.Action = TreeViewAction.Unknown OrElse
Not CheckBoxes Then Return
If AutoCheckParents Then
Dim p = e.Node.Parent
While p IsNot Nothing
p.Checked = p.Nodes.Cast(Of TreeNode).Any(Function(x) x.Checked)
p = p.Parent
End While
End If
If AutoCheckChildren Then
For Each tn As TreeNode In GetNodes(e.Node)
tn.Checked = e.Node.Checked
Next
End If
End Sub
#End Region
#Region "Private Methods"
Private Iterator Function GetNodes(node As TreeNode) As IEnumerable(Of TreeNode)
For Each n As TreeNode In node.Nodes
Yield n
For Each c As TreeNode In GetNodes(n)
Yield c
Next
Next
End Function
#End Region
#Region "API"
Private Const TVM_SETEXTENDEDSTYLE As Integer = &H1100 + 44
Private Const TVM_GETEXTENDEDSTYLE As Integer = &H1100 + 45
Private Const TVS_EX_DOUBLEBUFFER As Integer = &H4
Private Const WM_LBUTTONDBLCLK As Integer = &H203
<DllImport("user32.dll")>
Private Shared Function SendMessage(ByVal hWnd As IntPtr,
ByVal msg As Integer,
ByVal wp As IntPtr,
ByVal lp As IntPtr) As IntPtr
End Function
#End Region
End Class
Add a new class to your project and paste this code.
Rebuild.
Drop an instance of the DoubleClickCheckTreeView or change the type of the existing default TreeView in the Designer.
Related
◉ AfterCheck and AfterSelect events in TreeView, Windows Forms (c#)

I would imagine you would have to handle your double click event when its fired, and I suspect your code for that is going to look very similar to your single click event (but who knows)

Related

How can I scroll a ListView by one row instead of the default three using the mouse wheel?

I have a modified ListView. When I scroll using the mouse wheel, it scrolls three rows.
I want it to scroll one row at a time using the mouse wheel.
Public Class listviewEx
Inherits ListView
Private Declare Function ShowScrollBar Lib "user32" (ByVal hwnd As IntPtr, ByVal wBar As Integer,
ByVal bShow As Boolean) As Integer
' Constants
Private Const SB_HORZ As Integer = 0
Private Const WM_HSCROLL As Integer = &H114
Private Const WM_VSCROLL As Integer = &H115
Public Event Scroll As ScrollEventHandler
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
ShowScrollBar(MyBase.Handle, SB_HORZ, False)
If m.Msg = &H115 Then
' Trap WM_VSCROLL
End If
End Sub
Public Sub New()
MyBase.New()
Me.SetStyle(ControlStyles.Opaque, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True)
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Me.SetStyle(ControlStyles.AllPaintingInWmPaint, True)
Me.SetStyle(ControlStyles.EnableNotifyMessage, True)
End Sub
End Class
You can add a behavior to your ListView, to make it scroll one row instead of the default three, using the Ctrl key (as this modifier is often used to change this kind of behaviors) in combination with the mouse wheel.
You can then have the standard three-rows scroll when Ctrl is not pressed and one-row scroll behavior when it's pressed.
Override WndProc (as you're already doing), to handle WM_MOUSEWHEEL and verify that the Ctrl key is pressed, checking whether the low-word of WParam is MK_CONTROL = &H08.
When it's pressed, determine whether the delta is positive or negative and increment the value returned by ListView.TopItem.Index to then set the TopItem based on the calculated offset (adding a minimum/maximum check to avoid overflows):
Imports System.Windows.Forms
Public Class ListViewEx
Inherits ListView
Private Const WM_MOUSEWHEEL As Integer = &H20A
Private Const MK_CONTROL As Integer = &H8
Public Sub New()
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Select Case m.Msg
Case WM_MOUSEWHEEL
If Items.Count > 0 AndAlso (m.WParam.ToInt64() And &HFF) = MK_CONTROL Then
Dim offset = If((m.WParam.ToInt64() >> 16) > 0, -1, 1) + TopItem.Index
offset = Math.Max(Math.Min(offset, Items.Count - 1), 0)
TopItem = Items(offset)
m.Result = IntPtr.Zero
End If
End Select
End Sub
End Class
Someone named Viorel did it:
Public Class listviewEx
Inherits ListView
Protected Overrides Sub WndProc(ByRef m As Message)
Const WM_MOUSEWHEEL = &H20A
Select Case m.Msg
Case WM_MOUSEWHEEL
If TopItem IsNot Nothing Then
Dim d As Int16 = (m.WParam.ToInt32 >> 16)
Dim i As Integer
If d > 0 Then
i = Math.Max(TopItem.Index - 1, 0)
Else
i = Math.Min(TopItem.Index + 1, Items.Count - 1)
End If
TopItem = Items(i)
End If
m.Result = IntPtr.Zero
Return
End Select
MyBase.WndProc(m)
End Sub
End Class

Radio button on the ContextMenu or ContextMenuStrip [winforms]

I am searching for the way to implement a context menu item with radio button appearance like this: Windows 7's ContextMenu with RadioButton menu item
I have search through Google and SO, this post Adding RadioButtons to ContextMenu is close, but it's related to Java, and I am looking for a control or renderer in .NET for winforms.
Any solution or suggestion would be great help. Thank you.
Option buttons, also known as radio buttons, are similar to check boxes except that users can select only one at a time. Although by default the ToolStripMenuItem class does not provide option-button behavior, the class does provide check-box behavior that you can customize to implement option-button behavior for menu items in a MenuStrip control.
When the CheckOnClick property of a menu item is true, users can click the item to toggle the display of a check mark. The Checked property indicates the current state of the item. To implement basic option-button behavior, you must ensure that when an item is selected, you set the Checked property for the previously selected item to false.
The following procedures describe how to implement this and additional functionality in a class that inherits the ToolStripMenuItem class. The ToolStripRadioButtonMenuItem class overrides members such as OnCheckedChanged and OnPaint to provide the selection behavior and appearance of option buttons. Additionally, this class overrides the Enabled property so that options on a submenu are disabled unless the parent item is selected.
First Create a Class for RadioButton
This is combination of RadioButton and ToggleButton.
Public Class ToolStripRadioButtonMenuItem
Inherits ToolStripMenuItem
Public Sub New()
MyBase.New()
Initialize()
End Sub
Public Sub New(ByVal text As String)
MyBase.New(text, Nothing, CType(Nothing, EventHandler))
Initialize()
End Sub
Public Sub New(ByVal image As Image)
MyBase.New(Nothing, image, CType(Nothing, EventHandler))
Initialize()
End Sub
Public Sub New(ByVal text As String, ByVal image As Image)
MyBase.New(text, image, CType(Nothing, EventHandler))
Initialize()
End Sub
Public Sub New(ByVal text As String, _
ByVal image As Image, ByVal onClick As EventHandler)
MyBase.New(text, image, onClick)
Initialize()
End Sub
Public Sub New(ByVal text As String, ByVal image As Image, _
ByVal onClick As EventHandler, ByVal name As String)
MyBase.New(text, image, onClick, name)
Initialize()
End Sub
Public Sub New(ByVal text As String, ByVal image As Image, _
ByVal ParamArray dropDownItems() As ToolStripItem)
MyBase.New(text, image, dropDownItems)
Initialize()
End Sub
Public Sub New(ByVal text As String, ByVal image As Image, _
ByVal onClick As EventHandler, ByVal shortcutKeys As Keys)
MyBase.New(text, image, onClick)
Initialize()
Me.ShortcutKeys = shortcutKeys
End Sub
' Called by all constructors to initialize CheckOnClick.
Private Sub Initialize()
CheckOnClick = True
End Sub
Protected Overrides Sub OnCheckedChanged(ByVal e As EventArgs)
MyBase.OnCheckedChanged(e)
' If this item is no longer in the checked state, do nothing.
If Not Checked Then Return
' Clear the checked state for all siblings.
For Each item As ToolStripItem In Parent.Items
Dim radioItem As ToolStripRadioButtonMenuItem = _
TryCast(item, ToolStripRadioButtonMenuItem)
If radioItem IsNot Nothing AndAlso _
radioItem IsNot Me AndAlso _
radioItem.Checked Then
radioItem.Checked = False
' Only one item can be selected at a time,
' so there is no need to continue.
Return
End If
Next
End Sub
Protected Overrides Sub OnClick(ByVal e As EventArgs)
' If the item is already in the checked state, do not call
' the base method, which would toggle the value.
If Checked Then Return
MyBase.OnClick(e)
End Sub
' Let the item paint itself, and then paint the RadioButton
' where the check mark is displayed, covering the check mark
' if it is present.
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
' If the client sets the Image property, the selection behavior
' remains unchanged, but the RadioButton is not displayed and the
' selection is indicated only by the selection rectangle.
If Image IsNot Nothing Then Return
' Determine the correct state of the RadioButton.
Dim buttonState As RadioButtonState = RadioButtonState.UncheckedNormal
If Enabled Then
If mouseDownState Then
If Checked Then
buttonState = RadioButtonState.CheckedPressed
Else
buttonState = RadioButtonState.UncheckedPressed
End If
ElseIf mouseHoverState Then
If Checked Then
buttonState = RadioButtonState.CheckedHot
Else
buttonState = RadioButtonState.UncheckedHot
End If
Else
If Checked Then buttonState = RadioButtonState.CheckedNormal
End If
Else
If Checked Then
buttonState = RadioButtonState.CheckedDisabled
Else
buttonState = RadioButtonState.UncheckedDisabled
End If
End If
' Calculate the position at which to display the RadioButton.
Dim offset As Int32 = CInt((ContentRectangle.Height - _
RadioButtonRenderer.GetGlyphSize( _
e.Graphics, buttonState).Height) / 2)
Dim imageLocation As Point = New Point( _
ContentRectangle.Location.X + 4, _
ContentRectangle.Location.Y + offset)
' If the item is selected and the RadioButton paints with partial
' transparency, such as when theming is enabled, the check mark
' shows through the RadioButton image. In this case, paint a
' non-transparent background first to cover the check mark.
If Checked AndAlso RadioButtonRenderer _
.IsBackgroundPartiallyTransparent(buttonState) Then
Dim glyphSize As Size = RadioButtonRenderer _
.GetGlyphSize(e.Graphics, buttonState)
glyphSize.Height -= 1
glyphSize.Width -= 1
Dim backgroundRectangle As _
New Rectangle(imageLocation, glyphSize)
e.Graphics.FillEllipse( _
SystemBrushes.Control, backgroundRectangle)
End If
RadioButtonRenderer.DrawRadioButton( _
e.Graphics, imageLocation, buttonState)
End Sub
Private mouseHoverState As Boolean = False
Protected Overrides Sub OnMouseEnter(ByVal e As EventArgs)
mouseHoverState = True
' Force the item to repaint with the new RadioButton state.
Invalidate()
MyBase.OnMouseEnter(e)
End Sub
Protected Overrides Sub OnMouseLeave(ByVal e As EventArgs)
mouseHoverState = False
MyBase.OnMouseLeave(e)
End Sub
Private mouseDownState As Boolean = False
Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
mouseDownState = True
' Force the item to repaint with the new RadioButton state.
Invalidate()
MyBase.OnMouseDown(e)
End Sub
Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
mouseDownState = False
MyBase.OnMouseUp(e)
End Sub
' Enable the item only if its parent item is in the checked state
' and its Enabled property has not been explicitly set to false.
Public Overrides Property Enabled() As Boolean
Get
Dim ownerMenuItem As ToolStripMenuItem = _
TryCast(OwnerItem, ToolStripMenuItem)
' Use the base value in design mode to prevent the designer
' from setting the base value to the calculated value.
If Not DesignMode AndAlso _
ownerMenuItem IsNot Nothing AndAlso _
ownerMenuItem.CheckOnClick Then
Return MyBase.Enabled AndAlso ownerMenuItem.Checked
Else
Return MyBase.Enabled
End If
End Get
Set(ByVal value As Boolean)
MyBase.Enabled = value
End Set
End Property
' When OwnerItem becomes available, if it is a ToolStripMenuItem
' with a CheckOnClick property value of true, subscribe to its
' CheckedChanged event.
Protected Overrides Sub OnOwnerChanged(ByVal e As EventArgs)
Dim ownerMenuItem As ToolStripMenuItem = _
TryCast(OwnerItem, ToolStripMenuItem)
If ownerMenuItem IsNot Nothing AndAlso _
ownerMenuItem.CheckOnClick Then
AddHandler ownerMenuItem.CheckedChanged, New _
EventHandler(AddressOf OwnerMenuItem_CheckedChanged)
End If
MyBase.OnOwnerChanged(e)
End Sub
' When the checked state of the parent item changes,
' repaint the item so that the new Enabled state is displayed.
Private Sub OwnerMenuItem_CheckedChanged( _
ByVal sender As Object, ByVal e As EventArgs)
Invalidate()
End Sub
End Class
Second Create a Class of Form1
Public Class Form1
Inherits Form
Private sample As New MenuStrip()
Private mainToolStripMenuItem As New ToolStripMenuItem()
Private toolStripMenuItem1 As New ToolStripMenuItem()
Private toolStripRadioButtonMenuItem1 As New ToolStripRadioButtonMenuItem()
Private toolStripRadioButtonMenuItem2 As New ToolStripRadioButtonMenuItem()
Private toolStripRadioButtonMenuItem3 As New ToolStripRadioButtonMenuItem()
Private toolStripRadioButtonMenuItem4 As New ToolStripRadioButtonMenuItem()
Private toolStripRadioButtonMenuItem5 As New ToolStripRadioButtonMenuItem()
Private toolStripRadioButtonMenuItem6 As New ToolStripRadioButtonMenuItem()
Public Sub New()
Me.mainToolStripMenuItem.Text = "main"
toolStripRadioButtonMenuItem1.Text = "option 1"
toolStripRadioButtonMenuItem2.Text = "option 2"
toolStripRadioButtonMenuItem3.Text = "option 2-1"
toolStripRadioButtonMenuItem4.Text = "option 2-2"
toolStripRadioButtonMenuItem5.Text = "option 3-1"
toolStripRadioButtonMenuItem6.Text = "option 3-2"
toolStripMenuItem1.Text = "toggle"
toolStripMenuItem1.CheckOnClick = True
mainToolStripMenuItem.DropDownItems.AddRange(New ToolStripItem() { _
toolStripRadioButtonMenuItem1, toolStripRadioButtonMenuItem2, _
toolStripMenuItem1})
toolStripRadioButtonMenuItem2.DropDownItems.AddRange( _
New ToolStripItem() {toolStripRadioButtonMenuItem3, _
toolStripRadioButtonMenuItem4})
toolStripMenuItem1.DropDownItems.AddRange(New ToolStripItem() { _
toolStripRadioButtonMenuItem5, toolStripRadioButtonMenuItem6})
sample.Items.AddRange(New ToolStripItem() {mainToolStripMenuItem})
Controls.Add(sample)
MainMenuStrip = sample
Text = "ToolStripRadioButtonMenuItem demo"
End Sub
End Class
Last is Create a Class for Program
Public Class Program
<STAThread()> Public Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New Form1())
End Sub
End Class
Screenshot
.
Credits to Karl Erickson and to his blog about RadioButton and Menustrip.
Check his blog here.

Hiding up/down buttons on NumericUpDown control

I am trying to subclass NumericUpDown in several ways to get better functionality and appearance.
Since NUD is construct of two controls I would like to hide up/down buttons in cases where property "Increment" is set to 0.
This code is in subclass:
Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As System.EventArgs)
Controls(0).Hide()
End Sub
... and it work OK.
But in that function I cannot check a value of Increment property like this:
Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As System.EventArgs)
If Me.Increment = 0 Then
Controls(0).Hide()
End if
End Sub
In scope of this function Me is not reachable.
I am also try with using local variables but can't find which event is fired before OnTextBoxResize to read value of Increment property.
What to do in such case to get desired functionality?
This seems to work fairly well. It Shadows the Increment property in order to set the visibility of the spinner controls when the Increment value is being changed. There is an underlying private method the base control calls called PositionControls which you cannot stop — that method could create some flicker, but on my test, it didn't.
Public Class MyNumBox
Inherits NumericUpDown
Shadows Property Increment As Decimal
Get
Return MyBase.Increment
End Get
Set(value As Decimal)
MyBase.Increment = value
OnTextBoxResize(Me, EventArgs.Empty)
End Set
End Property
Protected Overrides Sub OnHandleCreated(e As EventArgs)
MyBase.OnHandleCreated(e)
OnTextBoxResize(Me, EventArgs.Empty)
End Sub
Protected Overrides Sub OnTextBoxResize(source As Object, e As EventArgs)
If Me.IsHandleCreated Then
Me.Height = Me.PreferredHeight
Me.Controls(0).Visible = (MyBase.Increment > 0)
Dim borderWidth As Integer = 0
If Me.BorderStyle > BorderStyle.None Then
borderWidth = SystemInformation.Border3DSize.Width
End If
Dim textWidth As Integer
If Me.Increment = 0 Then
textWidth = Me.ClientSize.Width - (borderWidth * 2)
Else
textWidth = Me.ClientSize.Width - Me.Controls(0).Width - (borderWidth * 2)
End If
If Me.UpDownAlign = LeftRightAlignment.Left Then
If Me.Increment = 0 Then
Me.Controls(1).SetBounds(borderWidth, borderWidth, _
textWidth, Me.Controls(1).Height)
Else
Me.Controls(1).SetBounds(borderWidth + Me.Controls(0).Width, _
Me.Controls(1).Top, textWidth, Me.Controls(1).Height)
End If
Else
Me.Controls(1).SetBounds(borderWidth, Me.Controls(1).Top, _
textWidth, Me.Controls(1).Height)
End If
Me.Refresh()
End If
End Sub
End Class
In the OnTextBoxResize override, I am re-positioning the controls into their proper place, and this version does account for the UpDownAlign property.
If you can, read this thread over at EE where I answered a similar question. It resizes the edit portion so that the control is redrawn correctly when the buttons have been hidden and the control is resized. *Otherwise the portion of the control where the buttons used to be leaves ghosts behind.
One solution to your specific problem is to wait for the VisibleChanged() event and check the Increment() property from there. Here is a conversion of my previous answer with some minor changes:
Public Class NoArrowNumericUpDown
Inherits NumericUpDown
Private itb As InnerTextBox = Nothing
Protected Overrides Sub OnVisibleChanged(e As System.EventArgs)
If Me.Visible Then
If Me.Increment = 0 AndAlso IsNothing(itb) Then
Dim ctl As Control = Me.Controls(0) ' get the spinners
Me.Controls.Remove(ctl) ' remove the spinners
ctl = Me.Controls(0) ' get the edit control
itb = New InnerTextBox(Me, ctl)
End If
End If
MyBase.OnVisibleChanged(e)
End Sub
Public Class InnerTextBox
Inherits NativeWindow
Private parentControl As Control = Nothing
Const WM_WINDOWPOSCHANGING As Integer = &H46
Public Sub New(parentControl As Control, InnerTextBox As Control)
Me.parentControl = parentControl
Me.AssignHandle(InnerTextBox.Handle)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case WM_WINDOWPOSCHANGING
Dim wp As WindowPos = CType(System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, GetType(WindowPos)), WindowPos)
If Me.parentControl IsNot Nothing Then
wp.cx = Me.parentControl.ClientSize.Width - 2 * wp.x
wp.cy = Me.parentControl.ClientSize.Height
System.Runtime.InteropServices.Marshal.StructureToPtr(wp, m.LParam, True)
End If
Exit Select
End Select
MyBase.WndProc(m)
End Sub
Public Structure WindowPos
Public hwnd As IntPtr
Public hwndInsertAfter As IntPtr
Public x As Integer
Public y As Integer
Public cx As Integer
Public cy As Integer
Public flags As UInteger
End Structure
End Class
End Class
EDIT: You could just enclose your code in a Try/Catch block?
Public Class NoArrowNumericUpDown
Inherits NumericUpDown
Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As System.EventArgs)
Try
If Me.Increment = 0 Then
Controls(0).Hide()
End If
Catch ex As Exception
End Try
End Sub
End Class

Double click form event VB.NET

I'm using VB.NET and would like to know how I can double click a form to minimize it.
I have the following code but it does'nt work:
Private Sub openPROJECTS_MouseDoubleClick(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDoubleClick
'Minimize the form
Me.WindowState = FormWindowState.Minimized
End Sub
I'm using VB.NET.
The problem is that every control has it's own double click event, so if you double click e.g. a panel on your form, your form's double click event won't get fired.
You could, however, add a MessageFilter, e.g.
Public Class Minimizer
Implements IMessageFilter
Private Const WM_LBUTTONDBLCLK As Integer = &H203
ReadOnly _form As Form
Public Sub New(f As Form)
_form = f
End Sub
Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
If m.Msg = WM_LBUTTONDBLCLK Then
_form.WindowState = FormWindowState.Minimized
End If
Return False
End Function
End Class
And a little test:
Private Sub Main()
Dim f = New Form()
f.Controls.Add(New Panel() With { .Dock = DockStyle.Fill })
Application.AddMessageFilter(New Minimizer(f))
f.ShowDialog()
End Sub
But I think it would be a strange user experience if double clicking anywhere minimizes the form...

How can I extend a DataGridView ComboBox column's edit control by a second tiny button?

I insert DataGridViewComboBoxColumn columns in a DataViewGrid. That works fine.
Now, I want the user to be able to not only select one list item, but to "drilldown" into the list that is behind the combobox, allowing him to edit (insert/delete/update) the list.
I think it would be a good idea to display a ".." button right behind the dropdown button of the combobox. Pushing it leads to a dialog where the list can be maintained.
What I am stumbling upon is:
How would I create such a custom combobox? Is ComboBox (which is the base for the combo box that the combo box column creates as its edit control) open enough to accommodate such an additional button? What would be the container of the tiny buttoon -- the ComboBox descendant?
How I would make the grid create and handle such a custom combobox?
I currently try to solve this by subclassing DataGridViewComboBoxColum, using a DataGridViewComboBoxCell descendent in its CellTemplate assignment, and overriding PositionWEditingPanel and PositionEditingControl to manipulate the sizes of the panel and the combobox so I'd have space for the tiny button.
Is that the correct way?
Or would I have to create a DataGridViewColumn descendant which creates a Panel containing a DataGridView combobox edit control and the tiny button? How would I make sure the column keeps care of the combo box so it has the correct items etc?
Maybe I sound confused, but I probably am after weeks of VB code (doh)....
Here's the code I came up with. Only thing that's missing is the button press event handler.
Improvements welcome!
#Region "Custom column, cell and edit control for Combobox-with-a-'..'-Button"
Public Class DataGridViewComboBoxExColumn
Inherits DataGridViewComboBoxColumn
Public Sub New()
MyBase.New()
CellTemplate = New DataGridViewComboBoxExCell()
End Sub
Public Overrides Property CellTemplate As DataGridViewCell
Get
Return MyBase.CellTemplate
End Get
Set(ByVal value As DataGridViewCell)
If (value IsNot Nothing) AndAlso Not value.GetType().IsAssignableFrom(GetType(DataGridViewComboBoxExCell)) Then
Throw New InvalidCastException("Must be a DataGridViewComboBoxExCell")
End If
MyBase.CellTemplate = value
End Set
End Property
End Class
Public Class DataGridViewComboBoxExCell
Inherits DataGridViewComboBoxCell
Dim HostingPanel As Panel
Public Sub New()
MyBase.New()
Dim TheButton As Button
HostingPanel = New Panel
HostingPanel.BorderStyle = BorderStyle.Fixed3D
HostingPanel.Padding = New Padding(0, 0, 0, 0)
HostingPanel.BackColor = Color.FromKnownColor(KnownColor.Control)
'HostingPanel.ForeColor = Color.Red ' Color.FromKnownColor(KnownColor.ButtonFace)
TheButton = New Button
TheButton.Text = ""
TheButton.BackColor = Color.FromKnownColor(KnownColor.ButtonFace)
TheButton.ImageList = DaCorFredProtMainForm.MainImageList
TheButton.ImageKey = "table_edit.png"
TheButton.Dock = DockStyle.Fill
HostingPanel.Controls.Add(TheButton)
End Sub
Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
If Not Me.DataGridView.EditingPanel.Controls.Contains(HostingPanel) Then ' Should always be true
Me.DataGridView.EditingPanel.Controls.Add(HostingPanel)
End If
End Sub
Public Overrides Sub DetachEditingControl()
If Me.DataGridView.EditingPanel.Controls.Contains(HostingPanel) Then ' Should always be true
Me.DataGridView.EditingPanel.Controls.Remove(HostingPanel)
End If
MyBase.DetachEditingControl()
End Sub
Public Overrides ReadOnly Property EditType As Type
Get
Return MyBase.EditType
End Get
End Property
Public Overrides ReadOnly Property ValueType As Type
Get
Return MyBase.ValueType
End Get
End Property
Public Overrides Function PositionEditingPanel(ByVal cellBounds As System.Drawing.Rectangle, ByVal cellClip As System.Drawing.Rectangle, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal singleVerticalBorderAdded As Boolean, ByVal singleHorizontalBorderAdded As Boolean, ByVal isFirstDisplayedColumn As Boolean, ByVal isFirstDisplayedRow As Boolean) As System.Drawing.Rectangle
cellBounds.Width += cellBounds.Height
cellClip.Width += cellClip.Height
Return MyBase.PositionEditingPanel(cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow)
End Function
Public Overrides Sub PositionEditingControl(ByVal setLocation As Boolean, ByVal setSize As Boolean, ByVal cellBounds As System.Drawing.Rectangle, ByVal cellClip As System.Drawing.Rectangle, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal singleVerticalBorderAdded As Boolean, ByVal singleHorizontalBorderAdded As Boolean, ByVal isFirstDisplayedColumn As Boolean, ByVal isFirstDisplayedRow As Boolean)
MyBase.PositionEditingControl(setLocation, setSize, cellBounds, cellClip, cellStyle, singleVerticalBorderAdded, singleHorizontalBorderAdded, isFirstDisplayedColumn, isFirstDisplayedRow)
Me.DataGridView.EditingControl.Width -= Me.DataGridView.EditingPanel.Height
HostingPanel.Width = Me.DataGridView.EditingPanel.Height
HostingPanel.Height = Me.DataGridView.EditingPanel.Height
HostingPanel.Location = New Point(DataGridView.EditingPanel.Size.Width - DataGridView.EditingPanel.Size.Height, 0)
End Sub
End Class
#End Region