populate menu at run time

  • Follow


I'm using VB4-32 and want to populate a menu listing COM ports after
enumerating the ports at run time.

How do I do this?
0
Reply nobody9919 (41) 1/16/2006 3:09:16 PM

Dave -

I only have access to VB6, not VB4, but if memory serves me, I believe the 
menus could be created dynamically in that version too.  Below is some code 
you could use to add COM ports dynamically.  I didn't include code to 
enumerate the COM ports, I just hardcoded the values; I am assuming you have 
that part of the code already.  If you need help with that, defnitely post 
back.

First, you need to create a top-level menu.  Let's assume it's called 
mnuCOMPorts.  Create one sublevel menu named mnuCOMPort; give it an index of 
0, and "None" for a caption.

Next, add this code:

Private mlngCountCOMPorts As Long             'CountCOMPorts

Private Sub AddCOMPort(COMPort As String)
    If mlngCountCOMPorts Then
        Load mnuCOMPort(mlngCountCOMPorts)
    End If

    mnuCOMPort(mlngCountCOMPorts).Caption = COMPort
    mnuCOMPort(mlngCountCOMPorts).Visible = True

    CountCOMPorts = mlngCountCOMPorts + 1
End Sub

Public Property Let CountCOMPorts(ByVal NewVal As Long)
    Dim lngPort As Long
    Dim lngMinPort As Long

    If NewVal = 0 Then
        lngMinPort = 1
    Else
        lngMinPort = NewVal
    End If

    For lngPort = (mlngCountCOMPorts - 1) To lngMinPort Step -1
        Unload mnuCOMPort(lngPort)
    Next

    mlngCountCOMPorts = NewVal

    If mlngCountCOMPorts = 0 Then
        mnuCOMPort(0).Caption = "None"
    End If
End Property


Finally, create the menu items with the following code:

    CountCOMPorts = 0

    AddCOMPort "COM1"
    AddCOMPort "COM2"
    AddCOMPort "COM3"

Good luck!

Jay Taplin MCP 


0
Reply jtaplin (13) 1/16/2006 6:11:30 PM


Jay,

Thanks. That works fine. 

Now, I just need to figure out how to sort them before building the menu. 

"Jay Taplin" <jtaplin@integraware.com> wrote:

>Dave -
>
>I only have access to VB6, not VB4, but if memory serves me, I believe the 
>menus could be created dynamically in that version too.  Below is some code 
>you could use to add COM ports dynamically.  I didn't include code to 
>enumerate the COM ports, I just hardcoded the values; I am assuming you have 
>that part of the code already.  If you need help with that, defnitely post 
>back.
>
>First, you need to create a top-level menu.  Let's assume it's called 
>mnuCOMPorts.  Create one sublevel menu named mnuCOMPort; give it an index of 
>0, and "None" for a caption.
>
>Next, add this code:
>
>Private mlngCountCOMPorts As Long             'CountCOMPorts
>
>Private Sub AddCOMPort(COMPort As String)
>    If mlngCountCOMPorts Then
>        Load mnuCOMPort(mlngCountCOMPorts)
>    End If
>
>    mnuCOMPort(mlngCountCOMPorts).Caption = COMPort
>    mnuCOMPort(mlngCountCOMPorts).Visible = True
>
>    CountCOMPorts = mlngCountCOMPorts + 1
>End Sub
>
>Public Property Let CountCOMPorts(ByVal NewVal As Long)
>    Dim lngPort As Long
>    Dim lngMinPort As Long
>
>    If NewVal = 0 Then
>        lngMinPort = 1
>    Else
>        lngMinPort = NewVal
>    End If
>
>    For lngPort = (mlngCountCOMPorts - 1) To lngMinPort Step -1
>        Unload mnuCOMPort(lngPort)
>    Next
>
>    mlngCountCOMPorts = NewVal
>
>    If mlngCountCOMPorts = 0 Then
>        mnuCOMPort(0).Caption = "None"
>    End If
>End Property
>
>
>Finally, create the menu items with the following code:
>
>    CountCOMPorts = 0
>
>    AddCOMPort "COM1"
>    AddCOMPort "COM2"
>    AddCOMPort "COM3"
>
>Good luck!
>
>Jay Taplin MCP 
>

0
Reply nobody9919 (41) 1/16/2006 7:13:49 PM

Attached is a link to a great little QuickSort routine:

http://vbnet.mvps.org/index.html?code/sort/qsoverview.htm

I'd recommend bookmarking his (Randy Birch's) site; he has, in my opinion, 
the best collection of useful tools on the web.

Jay Taplin MCP 


0
Reply jtaplin (13) 1/16/2006 8:51:28 PM

I took the brute force route, inserting them into a string array at the
index equal to Val(Mid(PortName, 4)), then scanning the array to populate
the menu.

  For i = 1 To NumPorts
    If Mid$(strPorts(1), 1, 3) = "COM" Then
      AddCOMPort strPorts(i)
    End If
  Next

"Jay Taplin" <jtaplin@integraware.com> wrote:

>Attached is a link to a great little QuickSort routine:
>
>http://vbnet.mvps.org/index.html?code/sort/qsoverview.htm
>
>I'd recommend bookmarking his (Randy Birch's) site; he has, in my opinion, 
>the best collection of useful tools on the web.
>
>Jay Taplin MCP 
>

0
Reply nobody9919 (41) 1/16/2006 9:28:52 PM

On Mon, 16 Jan 2006 19:13:49 GMT, nobody@whocares.com (Dave Houston) wrote:

>Jay,
>
>Thanks. That works fine. 
>
>Now, I just need to figure out how to sort them before building the menu. 
Dave

Create a hidden list box with sorted = True, then add all of the items to
the list then once they are all added read them all back again.

Dim I As Integer
    SortedList.Clear
    For I = 9 To 0 Step -1
        SortedList.AddItem Format(I)
    Next
    
    For I = 0 To SortedList.ListCount - 1
        Debug.Print SortedList.List(I)
    Next

Richard
See http://www.caravanningnow.co.uk/ for most things to do with
caravanning.
-- 
Military justice is to justice what military music is to music. -
Groucho Marx 1890-1977
0
Reply ispcrco1 (162) 1/17/2006 6:02:46 PM

5 Replies
32 Views

(page loaded in 0.071 seconds)

Similiar Articles:






7/14/2012 2:17:34 AM


Reply: