Results 1 to 22 of 22

Thread: [RESOLVED] [2005] Store History on MenuStrip

  1. #1

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Resolved [RESOLVED] [2005] Store History on MenuStrip

    Hello all,

    Does anybody know how to store history on the menustrip? I want it stored just like when you use Word or Access.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  2. #2
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Store History on MenuStrip

    Store stings in user settings and then load them into your menu strip.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  3. #3

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Store History on MenuStrip

    I know I will have to store strings but where are the user settings?
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  4. #4
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Store History on MenuStrip

    Right-click on your project and go to the settings tab. You can set their values at runtime by assigning values to My.Settings.Whatever
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  5. #5

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Store History on MenuStrip

    Oh, okay. Thank you! I learned something new today.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  6. #6
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Store History on MenuStrip

    Happy to help. Make sure to mark the thread resolved.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  7. #7

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Store History on MenuStrip

    I'm still not quite sure on how to do this. So far I have just added Project1-Project4 all of them are of type string and their scope is Application.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  8. #8
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Store History on MenuStrip

    Click on the My.Settings link in my signature for more info. Change the scope to User and then add code that is something to the extent of:

    Code:
    My.Settings.History1 = "File1.txt"
    My.Settings.History2 = "File2.txt"
    My.Settings.History3 = "File3.txt"
    My.Settings.History4 = "File4.txt"
    My.Settings.Save()
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Store History on MenuStrip

    Just use a single setting of type StringCollection to store the entire list of items.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Store History on MenuStrip

    Thanks John. I forgot there were collections in My.Settings.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  11. #11

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Store History on MenuStrip

    I've read the article but I still don't get it. I've never used settings before.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  12. #12
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Store History on MenuStrip

    What don't you get about it? When you create a user setting by right-clicking on your project and selecting properties, then a user.config file will be created/used when the app is run. Therefore, you can change the settings that are saved in the user.config simply by using code similar to what I posted earlier.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Store History on MenuStrip

    1. Double-click on the My Project node in the Solution Explorer to open the project properties.
    2. Select the Settings tab.
    3. Create a new setting named "History".
    4. Set its scope to "User" and its type to System.Collections.Specialized.StringCollection.
    5. In your form that you want to display the history do this:
    vb.net Code:
    1. Private Sub LoadHistory()
    2.     'Clear the currently displayed history.
    3.     Me.RecentToolStripMenuItem.DropDownItems.Clear()
    4.  
    5.     'Create the history collection if it doesn't already exist.
    6.     If My.Settings.History Is Nothing Then
    7.         My.Settings.History = New Specialized.StringCollection
    8.     End If
    9.  
    10.     'Add a menu item for each history item.
    11.     For Each item As String In My.Settings.History
    12.         Me.RecentToolStripMenuItem.DropDownItems.Add(item)
    13.     Next item
    14. End Sub
    6. To add a new history item you would do this:
    vb.net Code:
    1. 'The maximum number of history items to display.
    2. Private ReadOnly maxHistoryLength As Integer = 5
    3.  
    4. Private Sub AddHistoryItem(ByVal item As String)
    5.     'If the item is already in the list remove it from its current position.
    6.     If My.Settings.History.Contains(item) Then
    7.         My.Settings.History.Remove(item)
    8.     End If
    9.  
    10.     'Place the item at the top of the list.
    11.     My.Settings.History.Insert(0, item)
    12.  
    13.     'Remove any items beyond the maximum.
    14.     While My.Settings.History.Count > Me.maxHistoryLength
    15.         My.Settings.History.RemoveAt(Me.maxHistoryLength)
    16.     End While
    17.  
    18.     'Display the current history.
    19.     Me.LoadHistory()
    20. End Sub
    That's all you need to do. The string collection will be loaded from the config file into My.Settings automatically and likewise it will be saved automatically when your app shuts down.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Store History on MenuStrip

    Is RecentToolStripMenuItem just an item on the menu strip? And should I add the Me.LoadHistory() on the load of the form?
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Store History on MenuStrip

    Quote Originally Posted by onlyGirl
    Is RecentToolStripMenuItem just an item on the menu strip?
    If you've ever added a menu item to a MenuStrip then hopefully you've seen that it is named "XXXXToolStripMenuItem", where "XXXX" is whatever text you type into the designer to be displayed on the menu. That would suggest that "RecentToolStripMenuItem" refers to a menu item with "Recent" displayed on it, which is common for applications that display a list of recent files.
    Quote Originally Posted by onlyGirl
    And should I add the Me.LoadHistory() on the load of the form?
    That LoadHistory method loads the current history, so you should call it whenever and wherever you want to do that. If you want to load the history in the form's Load event handler then that's where you should call it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Store History on MenuStrip

    I have added the code but it still doesn't store any history. This is the code I have:

    On the form's load, I am loading Me.LoadHistory()
    vb Code:
    1. Private Sub frmHome_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Me.OpenMainDBConn()
    3.         Dim F15 As New frmnewProject
    4.         projExist = 1
    5.         F15.MdiParent = Me
    6.         F15.Show()
    7.         Me.LoadHistory()
    8.     End Sub

    vb Code:
    1. Private Sub LoadHistory()
    2.         Me.ToolStripMenuItem1.DropDownItems.Clear()
    3.         If My.Settings.History Is Nothing Then
    4.             My.Settings.History = New Specialized.StringCollection
    5.         End If
    6.  
    7.         For Each item As String In My.Settings.History
    8.             Me.ToolStripMenuItem1.DropDownItems.Add(item)
    9.         Next item
    10.     End Sub
    11.  
    12.     Private ReadOnly maxHistoryLength As Integer = 5
    13.  
    14.     Private Sub AddHistoryItem(ByVal item As String)
    15.         If My.Settings.History.Contains(item) Then
    16.             My.Settings.History.Remove(item)
    17.         End If
    18.  
    19.         My.Settings.History.Insert(0, item)
    20.  
    21.         While My.Settings.History.Count > Me.maxHistoryLength
    22.             My.Settings.History.RemoveAt(Me.maxHistoryLength)
    23.         End While
    24.  
    25.         'Me.LoadHistory()
    26.     End Sub
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Store History on MenuStrip

    Where are you calling the AddHistoryItem method?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Store History on MenuStrip

    How would I add it to the ToolStripMenuItem1 and so forth without it being dropped down? I know that you will have to do something among the lines of :

    If ToolStripMenuItem1.Text = "" Then
    Me.ToolStripMenuItem1.DropDownItems.Add(item)
    ElseIf



    And I know that it wouldn't be .DropDownItems but how would it overwrite them to any new projects?

    Thank you so much for your help!!!

    Name:  menu.GIF
Views: 218
Size:  20.2 KB
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  19. #19

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Store History on MenuStrip

    I tried this code but it just keeps adding the same project path for each one.
    vb Code:
    1. Private Sub LoadHistory()
    2.         Me.ToolStripMenuItem1.DropDownItems.Clear()
    3.         If My.Settings.History Is Nothing Then
    4.             My.Settings.History = New Specialized.StringCollection
    5.         End If
    6.  
    7.         For Each item As String In My.Settings.History
    8.             'Me.ToolStripMenuItem1.DropDownItems.Add(item)
    9.  
    10.             If ToolStripMenuItem1.Text = "" Then
    11.                 Me.ToolStripMenuItem1.Text = item
    12.             Else
    13.                 Me.ToolStripMenuItem1.Text = item
    14.  
    15.                 If ToolStripMenuItem2.Text = "" Then
    16.                     Me.ToolStripMenuItem2.Text = item
    17.                 Else
    18.                     Me.ToolStripMenuItem2.Text = item
    19.  
    20.                     If ToolStripMenuItem3.Text = "" Then
    21.                         Me.ToolStripMenuItem3.Text = item
    22.                     Else
    23.                         Me.ToolStripMenuItem3.Text = item
    24.  
    25.                         If ToolStripMenuItem4.Text = "" Then
    26.                             Me.ToolStripMenuItem4.Text = item
    27.                         Else
    28.                             Me.ToolStripMenuItem2.Text = item
    29.                         End If
    30.                     End If
    31.                 End If
    32.             End If
    33.         Next item
    34.     End Sub
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  20. #20
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Store History on MenuStrip

    I've already shown you exactly how to do it. The code in post #13 does exactly what you need as it is. You call LoadHistory directly when you start, then each time you want to add a new history item you call AddHistoryItem. The new item is always added at the top of the list and removed from its old position if it's already present. The AddHistoryItem method calls LoadHistory so the menu will be automatically refreshed every time you add a new item in code. All you had to do was add the code I provided and then add calls to the two methods where required. Did you read the comments in the code that explain what it's doing?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  21. #21

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Store History on MenuStrip

    I REALLY do appreciate your help and I have read the comments on your code that you provided me with. Although, your code makes a subset of ToolStrip and adds the history there. I am wanting to list the history not in a subset of a toolstrip but the way the picture above shows it, one right below another one.
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

  22. #22

    Thread Starter
    Fanatic Member onlyGirl's Avatar
    Join Date
    Sep 2006
    Location
    Houston, TX
    Posts
    743

    Re: [2005] Store History on MenuStrip

    I finally figured out how to set up the history with one right below another one instead of a subset. The code is this below:
    vb Code:
    1. Public Sub AddHistoryItem(ByVal item As String)
    2.         If My.Settings.History.Contains(DBLocation) Then
    3.             My.Settings.History.Remove(DBLocation)
    4.         End If
    5.  
    6.         My.Settings.History.Insert(0, DBLocation)
    7.  
    8.         While My.Settings.History.Count > Me.maxHistoryLength
    9.             My.Settings.History.RemoveAt(Me.maxHistoryLength)
    10.         End While
    11.  
    12.         Me.LoadHistory()
    13.         End Sub


    vb Code:
    1. Public Sub LoadHistory()
    2.         Dim count As Integer = 0
    3.  
    4.         If My.Settings.History Is Nothing Then
    5.             My.Settings.History = New Specialized.StringCollection
    6.         End If
    7.  
    8.         For Each item As String In My.Settings.History
    9.  
    10.             count += 1
    11.             If count = 1 Then
    12.                 History1.Text = item
    13.             ElseIf count = 2 Then
    14.                 History2.Text = item
    15.             ElseIf count = 3 Then
    16.                 History3.Text = item
    17.             ElseIf count = 4 Then
    18.                 History4.Text = item
    19.             End If
    20.             History1.Visible = True
    21.             History2.Visible = True
    22.             History3.Visible = True
    23.             History4.Visible = True
    24.         Next item
    25.     End Sub
    Using Visual Studio 2008

    Please mark your thread RESOLVED if you no longer need help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width