Solved

add MRU List o Contextmenustrip in vb.net

Posted on 2010-08-18
10
965 Views
Last Modified: 2013-11-27
Hi Experts,
My Windows desktop application has menu to open database files. I want to display 5 most recently used files as a contextmenustrip items. I have code to add it as a File Menu Item. But i am not using any menus. It is all graphical with big buttons and the button has contextmenustrip attached to it. How do I read these MRU files names and assign them dynamically to contextmenustrip? I am using Visual Studio 2008.
I will appreciat your help.
Thank you
0
Comment
Question by:RekhaShah
  • 6
  • 4
10 Comments
 
LVL 17

Expert Comment

by:nepaluz
ID: 33469127
where are you storing the file paths? In an array, list of string ....?
0
 

Author Comment

by:RekhaShah
ID: 33469284
I am storing my list in a public array. But how do i trigger the event to open the file?
0
 

Author Comment

by:RekhaShah
ID: 33469358
in the form load even, I call LoadMRUList. Then I add those list items to my contextMenustrip

For i = 1 To 5
            If MRUListArray(i) <> "" Then
                Me.MRUContextMenuStrip.Items.Add(MRUListArray(i))
            End If
Next
So i can add 1 file. But next time I open a different file, I need to add this to mru list. If kind of replaces the first one and I always have only one entry.
If you have a sample code, will be great. Thanks

Private Sub LoadMruList()

        Dim file_name As String

        For i As Integer = 1 To m_NumEntries

            ' Get the next file name and title.

            file_name = GetSetting(m_ApplicationName, _

                "MruList", "FileName" & i, "")



            ' See if we got anything.

            If file_name.Length > 0 Then

                ' Save this file name.

               MRUListArray(i) = file_name

            End If

        Next i

    End Sub

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33469397
Click on the context menu and then double click on the menu item, you should get this:

   Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click

    End Sub

and trigger your routine inside here, or you could add a handler everytime you add a menu (ie file path) to the menu

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 33469431
the handler you can make for that islike:

AddHandler ToolStripMenuItem1.Click, AddressOf sButton

and sButton will be just like above

   Private Sub sButton(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:RekhaShah
ID: 33469688
let me get back to you tomorrow on it after trying out your suggestion. Thanks
0
 

Author Comment

by:RekhaShah
ID: 33470002
I have 3 buttons. 1. Open New File:  
2. Open Existing File:I show OpenFileDialog
3. 3rd button would have Recently used database list(as a contextmenustrip). So when I click on this third button, it would display MRU, and upon clicking on one of the files, it would open this existing file.  
it seems preety straightforward, but I can't seem to get it to work the way I want. If I had it as a menu item it would be very simple. i will appreciate if you have sample code(comlete code) to handle the click even on mru button.
0
 
LVL 17

Accepted Solution

by:
nepaluz earned 500 total points
ID: 33470165

Dim MRUListArray As Array = Nothing
'populate your array here then
Dim MRUContextMenuStrip As New ContextMenuStrip
For i = 1 To 5
If MRUListArray(i) <> "" Then
MRUContextMenuStrip.Items.Add(MRUListArray(i))
End If
Next
AddHandler MRUContextMenuStrip.ItemClicked, AddressOf sButton1
 

Private Sub sButton1(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim sItem = DirectCast(sender, ContextMenuStrip)
'now you can get the value of the item clicked by
File.Open(sItem.Text, FileMode.Open)
'or
File.Open(sItem.Name, FileMode.Open)
'depending on where you saved the full path
End Sub
0
 

Author Closing Comment

by:RekhaShah
ID: 33476566
Hi,
I got it to work. Your solution was partially correct, instead of adding handler to button, I was suppose to add handler to ToolStripItem.  Attaching the code for reference. This works. Thak you for guiding to the correct direction.

Here is the correct code that works.
Hi,
I got it to work. Your solution was partially correct, instead of adding handler to button, I was suppose to add handler to ToolStripItem.  Attaching the code for reference. This works. Thak you for guiding to the correct direction.
0
 

Author Comment

by:RekhaShah
ID: 33476580
Here is the correct solution that works.

Private Sub MDIParent1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim tsi As ToolStripItem

        m_MruList = New MruList(APP_NAME)

        For i As Integer = 1 To 5

            If MRUListArray(i) <> "" Then

                tsi = MRUContextMenuStrip.Items.Add(MRUListArray(i))

                tsi.Name = "P" & i.ToString("00000")

                AddHandler tsi.Click, AddressOf tsi_Click

            End If

        Next i

        btnMRU.ContextMenuStrip = MRUContextMenuStrip

    End Sub



    Private Sub tsi_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim tsi As ToolStripItem = CType(sender, ToolStripItem)

        Debug.Print(tsi.Name & ", " & tsi.Text)

        MsgBox(tsi.Text)

    End Sub

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now