Patrick Steele's .NET Blog

Implements ICodeWithDotNet

  • Static Local Variables in VB.NET

    VB.NET has support for "local static variables". These are variables local to a method, but retain their method call between invocations of the method. The CLR does not support this, so how does VB.NET do it if it runs under the CLR? Just some simple compiler tricks!

  • Catching Windows messages

    I needed to capture mouse clicks before my windows app got a hold of them.  Seeing that the Form class has a PreProcessMessage method I thought I was all set.  As the documentation stated:

  • Moving a form by clicking anywhere on it.

    You've seen applications that allow you to move the form around by simply clicking anywhere on the form (not just the caption bar). Can you do this in .NET? Yes! And it's very easy. All you have to do is handle the proper windows message and the rest is easy.

    Option Strict On
    Option Explicit On
    
    Imports System Imports System.Windows.Forms Imports System.Drawing
    Public Class Form1 Inherits Form
    Private Declare Function ReleaseCapture Lib "user32" () As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Private Const WM_NCLBUTTONDOWN As Integer = &HA1 Private Const HTCAPTION As Integer = 2
    Public Sub New() Me.Text = "Drag anywhere to move" AddHandler Me.MouseDown, AddressOf frmMain_MouseDown End Sub
    <STAThread()> _ Shared Sub Main() Application.Run(New Form1()) End Sub
    Private Sub frmMain_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) 'Don't drag the sceen if it is the right button or the wheel. If e.Button = MouseButtons.Left Then ReleaseCapture() SendMessage(Me.Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0) End If
    End Sub
    End Class

  • Browsing for a folder (1.0)

    Still using the .NET 1.0 framework and need a "browse for folder" dialog? You'll need to resort to platform invoke (p/invoke) using the Win32 API's. Here's a class that encapsulates all of it.

  • Pulling a bitmap off the clipboard.

    Here's an example of pulling a bitmap off the clipboard and displaying it in a windows form.

    Option Strict On
    Option
     Explicit On 

    Imports System
    Imports System.Windows.Forms
    Imports System.Drawing

    Public Class WinApp
        
    Inherits System.Windows.Forms.Form

        
    Private m_PB As PictureBox

        <STAThread()> _
        
    Shared Sub Main()
            Application.Run(
    New WinApp())
        
    End Sub

        Public Sub New()
            
    Me.Text = "This is my form"
            m_PB = New PictureBox()
            m_PB.Location = 
    New Point(0, 0)
            m_PB.Size = 
    Me.Size

            m_PB.Image = GetImageFromClipboard()
            
    If Not m_PB.Image Is Nothing Then
                Me.ClientSize = New Size(m_PB.Image.Width, m_PB.Image.Height)
                m_PB.Size = 
    Me.ClientSize
            
    End If

            Me.Controls.Add(m_PB)
        
    End Sub

        Public Function GetImageFromClipboard() As Image
            
    If Not Clipboard.GetDataObject() Is Nothing Then
                Dim dobj As IDataObject = Clipboard.GetDataObject()
                
    If dobj.GetDataPresent(DataFormats.Bitmap) Then
                    Dim img_obj As Object = dobj.GetData(DataFormats.Bitmap)
                    
    Return CType(img_obj, Bitmap)
                
    End If
            End If
        End Function

    End
     Class

  • Adjusting to life without ItemData

    In VB6, the ListBox and ComboBox controls had the ItemData property. This was a “companion” list of long integers which could be used to store additional information related to an Item. For example, you could display a list of individuals in a ListBox and keep their age in the ItemData property: