Dynamically update menustrip and ToolStripMenuItem

K

kyungdongkim

Hi,

I have a dynamically generated MenuStrip following this example:
http://www.codeproject.com/useritems/Dynamic_MenuStrip.asp

Basically the menu strip allows users to save and load reports. The
Load menu is to list all the saved reports. I need to refresh the
Load menu after a new report is saved. Any help is much appreciated!

Code:
'Generates the MenuStrip, called at Form_OnLoad
Public Sub PopulateMenuStrip()
Dim mnMenu As New MenuStrip
Dim tsItem As ToolStripMenuItem
mnMenu.Width = 1095
mnMenu.Height = 24
tsItem = New ToolStripMenuItem("Save", Nothing, New
System.EventHandler(AddressOf MainMenuSave_OnClick), "tsmiSave")
mnMenu.Items.Add(tsItem)
tsItem = New ToolStripMenuItem("Save as Existing", Nothing,
New System.EventHandler(AddressOf MainMenuSaveAs_OnClick),
"tsmiSaveAs")
UpdateSavedReportList(tsItem)
mnMenu.Items.Add(tsItem)
tsItem = New ToolStripMenuItem("Load", Nothing, New
System.EventHandler(AddressOf MainMenuLoad_OnClick), "tsmiLoad")
UpdateSavedReportList(tsItem)
mnMenu.Items.Add(tsItem)
tsItem = New ToolStripMenuItem("Delete", Nothing, New
System.EventHandler(AddressOf MainMenuDelete_OnClick), "tsmiDelete")
UpdateSavedReportList(tsItem)
mnMenu.Items.Add(tsItem)
Me.Controls.Add(mnMenu)
End Sub

'Updates the ToolStripMenuItem based on data
'Called in above Sub (which is called OnLoad) but would also need to
cal it after anytime some1 saves a new report
'it works when the MenuStrip above is created, but how can I update
the MenuStrip above after the form has loaded
Public Sub UpdateSavedReportList(ByRef sender As Object)
Dim cms As New ContextMenuStrip()
Dim sMenu() As String
Dim i As Integer
Dim sMenuRD As SqlClient.SqlDataReader

sMenuRD = Blackport.c_BlackportData.getSavedFlexiReports()
If sMenuRD.HasRows Then
ReDim Preserve sMenu(0)
i = 0
While sMenuRD.Read()
ReDim Preserve sMenu(i)
sMenu(i) = sMenuRD("txtReportName")
i = i + 1
End While
End If
sMenuRD.Close()

For Each sMn As String In sMenu
cms.Items.Add(sMn, Nothing, New
System.EventHandler(AddressOf SelectedChildMenu_OnClick))
Next
Dim tsi As ToolStripMenuItem = CType(sender,
ToolStripMenuItem)
tsi.DropDown = cms
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top