Dynamically adding controls

  • Thread starter Thread starter David Webb
  • Start date Start date
D

David Webb

Hi,

In VB.NET CF, I have a menu called mnuAction. I read a table in a
database and retrieve values, for which I add a menu sub item. E.g. If
I read 3 items, I add 3 submenus. I loop through the Data Reader as
follows :-


Do While sqlDR.Read
ReDim Preserve mnu(iMnuCount)
mnu(iMnuCount) = New MenuItem
mnu(iMnuCount).Text = cDatabase.RetrieveCurrentStatus(2,
CType(sqlDR(0), Long)).ToUpper
frmJobDetails.mnuAction.MenuItems.Add(mnu(iMnuCount))
iMnuCount += 1
Loop

My problem being, is that I need to perform different functions based
on which menu item the user clicks. If the user clicks menuItem(1), I
need to access a function to display a screen. I never know if there
will be 1 or 10 menus. I tried coding a function in mnuAction_Click
but I don't understand the properties. Can anyone explain how I can
put a function in to at least tell me which menu I am in when I click
it? From there, I will be able to work out which function I need to
use.


I hope the about makes sense - I know what I mean, I'm just having
difficulty putting it into words!

Regards,

David.
 
I hope the about makes sense - I know what I mean, I'm just having
difficulty putting it into words!

haha

Let me take a stab at it. I assume you want to create menu dynamically and
handle menu events. My VB is a bit rusty so pardon some minor mistakes

First of all let's establish a way to match a menuitem to a value (such as
database row ID or something else that would let you uniquely identify the
db item)

Dim map as Hashtable = New Hashtable()

Do While sqlDR.Read
Dim mnu as New MenuItem()
mnu.Text = cDatabase.RetrieveCurrentStatus(2, CType(sqlDR(0),
Long)).ToUpper
frmJobDetails.mnuAction.MenuItems.Add(mnu)
' Add menu click handler
AddHahdler mnu.Click, AddressOf OnMenuClick
' Associate menu item with the DB row
map.Add(mnu, CType(sqlDR(0), Long))
Loop

' Now let's hanlde menu click
Sub OnMenuClick( sender as Object, e as EventArgs )
if map.ContainsKey(sender) then
Dim DBID as Long = CType(map.Item(sender), Long)
' Do something here in response to menu click
end if
End Sub

HTH

Alex
 
Back
Top