[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How do you pass a reference of the Form when calling a EnumWindows

Posted on 2004-11-03
2
Medium Priority
?
235 Views
Last Modified: 2010-04-23
I'm trying to Write to a ListBox Form during a EnumWindows Call Back.

My research point towards using a reference/pointer to access the Form from the Call Back

Below is a copy of the code is below.

###### Module Code:
Module Module1

    Public Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32

    Public Declare Function EnumWindows Lib "user32.dll" _
            (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32

    Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" _
        (ByVal hwnd As IntPtr) As Int32

    Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _
        (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32

    'Callback function to enum windows
    Public Function EnumWindowsCallBack(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32
        Dim sSave As String

        'Get the windowtext length
        sSave = Space(GetWindowTextLength(hwnd) + 1)

        'get the window text
        GetWindowText(hwnd, sSave, Len(sSave))

        'remove the last Chr(0)
        sSave = Microsoft.VisualBasic.Left(sSave, Len(sSave) - 1)

        'Error below: Reference to a non-shared member requires an Object Reference
        Form1.ListBox1.Items.Add(sSave)


        If sSave.Trim <> "" Then
            Debug.WriteLine(sSave)
        End If

        Return 1 'continue enumeration        
    End Function

End Module

###### Form Code:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Public WithEvents ListBox1 As System.Windows.Forms.ListBox
    Public WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(40, 184)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(200, 56)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(32, 24)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(216, 134)
        Me.ListBox1.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListBox1, Me.Button1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        EnumWindows(AddressOf EnumWindowsCallBack, 0)
    End Sub

    Public Sub AddProcess(ByVal strName As String)
        Me.ListBox1.Items.Add(strName)
    End Sub

    ' Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '
    '  End Sub
End Class
0
Comment
Question by:vaultworld
2 Comments
 
LVL 19

Accepted Solution

by:
arif_eqbal earned 2000 total points
ID: 12490468
Declare a variabl at Module Level

Public Lst as ListBox

Then on the form on Button Click you are writing

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        EnumWindows(AddressOf EnumWindowsCallBack, 0)
    End Sub

Now add one more line before call to EnumWindows

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Lst=ListBox1      '--------> ADD THIS LINE
        EnumWindows(AddressOf EnumWindowsCallBack, 0)
    End Sub

Then in the EnumWindows function

'Error below: Reference to a non-shared member requires an Object Reference
        Form1.ListBox1.Items.Add(sSave)

instead of this line use

    Lst.Items.Add(sSave)
0
 
LVL 1

Author Comment

by:vaultworld
ID: 12493597
Here's the Final Answer

Thanks for all the help.

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'ListBox1
        '
        Me.ListBox1.Location = New System.Drawing.Point(32, 16)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(240, 147)
        Me.ListBox1.TabIndex = 0
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(32, 200)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(240, 64)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.ListBox1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Lst = ListBox1
        EnumWindows(AddressOf EnumWindowsCallBack, 0)
    End Sub

End Class

Module Module1

    Public Lst As ListBox

    Public Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32

    Public Declare Function EnumWindows Lib "user32.dll" _
            (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32

    Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" _
        (ByVal hwnd As IntPtr) As Int32

    Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _
        (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32

    'Callback function to enum windows
    Public Function EnumWindowsCallBack(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32
        Dim sSave As String

        'Get the windowtext length
        sSave = Space(GetWindowTextLength(hwnd) + 1)

        'get the window text
        GetWindowText(hwnd, sSave, Len(sSave))

        'remove the last Chr(0)
        sSave = Microsoft.VisualBasic.Left(sSave, Len(sSave) - 1)

        'Error below: Reference to a non-shared member requires an Object Reference
        Lst.Items.Add(sSave)


        If sSave.Trim <> "" Then
            Debug.WriteLine(sSave)
        End If

        Return 1 'continue enumeration        
    End Function

End Module
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

825 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