Results 1 to 15 of 15

Thread: [2005] MenuItem Checked

  1. #1

    Thread Starter
    Lively Member Christineeve's Avatar
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    78

    [2005] MenuItem Checked

    I have this lovely menubar that works great except for two components that
    need to allow the user to check and uncheck if necessary. If the check
    mark is visible (checked) then it should run a procedure. If the checkmark
    is not visible (or checked) then it should not run the procedure.

    Additionally, the default setting will be checked so both procedures run when
    the application is installed and run. So therefore if the user "unchecks" the
    checkmark an advisory needs to pop up and say, "You've chosen to not
    view...." and else if they turn it back on, "You've chosen to play the ...."

    I only have two procedures where this needs to occur and right now, my
    code cannot seem to figure out what to do with the checkmark. I have it
    checked and it runs the code for the unchecked state.

    Additionally, the application doesn't remember the changed setting the next
    time I run the application. I cannot use a global settings saved method
    because I need to save the state of other controls independantly of this
    control.

    I apologize for posting this when there are numerous posts on this subject.
    Despite reading site after site on this subject, I cannot figure what I've done
    wrong or what I am missing in my code. Any help would be appreciated. I've
    been to all the sites offered on this site and already reviewed MSDN and my
    VB books. I'm at my wit's end.

    Here is my sample code.

    VB Code:
    1. 'The default setting is checked.
    2.  Private Sub MissionBriefingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    3. Handles MissionBriefingToolStripMenuItem.Click
    4.         Dim mymenu As New ToolStripMenuItem
    5.         Try
    6.             If mymenu.CheckState = CheckState.Checked = True Then
    7.                 MsgBox("You have turned ON the Mission Briefing. _
    8.               If you wish to turn it off, _
    9.                remove the check from the 'View Mission Briefing' box.", _
    10.  
    11.                    MsgBoxStyle.Information, "Mission Briefing")
    12.             Else
    13.                 'mymenu.CheckState = CheckState.Checked = false Then
    14.                 MsgBox("You have turned OFF the Mission Briefing. _
    15.                 To turn it back on, place a check mark in the 'View Mission _
    16. Briefing' box.", MsgBoxStyle.Information, "Mission Briefing")
    17.             End If
    18.         Catch ex As Exception
    19.             If Err.Number <> 0 Then
    20.                 errLogger(Err.Number, Err.Description, Err.Source)
    21.             End If
    22.             Exit Sub
    23.         Finally
    24.         End Try
    25.     End Sub
    26.  
    27.  
    28.  Private Sub MissionBriefingToolStripMenuItem_CheckStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
    29.       MissionBriefingToolStripMenuItem.CheckStateChanged
    30.         Dim mymenu As New ToolStripMenuItem
    31.         Try
    32.             If mymenu.CheckState = CheckState.Checked Then
    33.                 MsgBox("You have turned ON the Mission Briefing. _
    34.               If you wish to turn it off, remove the check from the _
    35.            'View Mission Briefing' box.", MsgBoxStyle.Information, "Mission Briefing")
    36.             ElseIf mymenu.CheckState = CheckState.Unchecked Then
    37.                 MsgBox("You have turned OFF the Mission Briefing. _
    38.                 To turn it back on, place a check mark in the 'View Mission _
    39.                  Briefing' box.", MsgBoxStyle.Information, "Mission Briefing")
    40.             End If
    41.         Catch ex As Exception
    42.             If Err.Number <> 0 Then
    43.                 errLogger(Err.Number, Err.Description, Err.Source)
    44.             End If
    45.             Exit Sub
    46.         Finally
    47.         End Try
    48.     End Sub
    49.  
    50. Private Sub PlayMissionBriefing()
    51.         Dim MissionBriefingToolStripMenuItem As New ToolStripMenuItem
    52.  
    53.         Try
    54.             If MissionBriefingToolStripMenuItem.CheckState = CheckState.Checked Then
    55.                 LaunchtheMission("mission_briefing", "mission_briefing.exe", "XXXXXXX")
    56.    End If
    57.  
    58.         Catch ex As Exception
    59.             If Err.Number <> 0 Then
    60.                 errLogger(Err.Number, Err.Description, Err.Source)
    61.             End If
    62.             Exit Sub
    63.         Finally
    64.  
    65.         End Try
    66.     End Sub
    Christine Lee
    VB '05 Newbie
    Anything but ordinary
    ------------------------------
    Newbie: VB '05 Express Videos:
    http://msdn.microsoft.com/vstudio/ex...g/default.aspx
    My Space! My family!
    http://www.myspace.com/christineeve

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

    Re: [2005] MenuItem Checked

    This is a pretty silly line:
    VB Code:
    1. If mymenu.CheckState = CheckState.Checked = True Then
    It will compare CheckState.Checked to True, which will return False, and then compare that to mymenu.CheckState, which will also return False. To do what you intended it would have to be:
    VB Code:
    1. If (mymenu.CheckState = CheckState.Checked) = True Then
    but that's silly too. Change it to:
    VB Code:
    1. If mymenu.Checked Then
    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

  3. #3

    Thread Starter
    Lively Member Christineeve's Avatar
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    78

    Re: [2005] MenuItem Checked

    Quote Originally Posted by jmcilhinney
    This is a pretty silly line:
    VB Code:
    1. If mymenu.CheckState = CheckState.Checked = True Then
    It will compare CheckState.Checked to True, which will return False, and then compare that to mymenu.CheckState, which will also return False. To do what you intended it would have to be:
    VB Code:
    1. If (mymenu.CheckState = CheckState.Checked) = True Then
    but that's silly too. Change it to:
    VB Code:
    1. If mymenu.Checked Then
    Silly!?! I work in the department of redundancy department therefore it makes perfect sense.

    Thanks, I wondered why it appeared to be true and came back false.

    I'll give this baby a whirl. Thank you. Of course you're brilliant!
    Christine Lee
    VB '05 Newbie
    Anything but ordinary
    ------------------------------
    Newbie: VB '05 Express Videos:
    http://msdn.microsoft.com/vstudio/ex...g/default.aspx
    My Space! My family!
    http://www.myspace.com/christineeve

  4. #4

    Thread Starter
    Lively Member Christineeve's Avatar
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    78

    Re: [2005] MenuItem Checked

    How can I get the application to save the settings I made for the menu? It's not saving those settings.

    Christine Lee
    VB '05 Newbie
    Anything but ordinary
    ------------------------------
    Newbie: VB '05 Express Videos:
    http://msdn.microsoft.com/vstudio/ex...g/default.aspx
    My Space! My family!
    http://www.myspace.com/christineeve

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

    Re: [2005] MenuItem Checked

    There's never a need to compare anything to True or False in an If statement because in order to be compared to True or False it must already be a Boolean value, so there's no point comparing one Boolean value to another to get a third Boolean value. If you just pass the original value it will have the same effect. The Checked property of a menu item is a Boolean value, so you can just pass that directly to an If statement. If you were using a ThreeState CheckBox and you really did have to test the CheckState then you'd just compare the CheckState property to a known value. That returns a Boolean so you just use that without comparing to another Boolean literal.

    Now, as to your current issue, what exactly do you mean by that last statement? Are you saying that you've used an application setting to persist the Checked property? If so, have created the setting with User or Application scope? If it's User then the current value should be saved automatically when you shut down. If it's Application then it won't because settings with Application scope are ReadOnly. If you want the value to be persisted but you want one value for all users instead of a different value for each user then you'll have to edit the config file yourself using an XmlDocument object. Before you do that make sure that it is in fact more appropriate to use Application scope than User.
    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

  6. #6

    Thread Starter
    Lively Member Christineeve's Avatar
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    78

    Re: [2005] MenuItem Checked

    Hi,
    Thank you for responding. As soon as I saw what you wrote, I saw what I was doing wrong. It was silly because it was so obvious. I realized that I had to take a break from what I was doing--after I repaired that.

    To explain about the settings not saving: When I remove the checkmark from my menuitem, "play mission" in this case, I'd like to never see the mission again. When I close and repopen my ap, the mission plays and the checkmark is back.

    Additionally, in my UI, I have several buttons that have to be saved individually. So, my application has "Save settings on exit" unchecked. I have a command button for "Save" and "Save All." Depending upon the situation, the user can save the state of one command button (enabled or disabled) or just save them all. I've set this up in code.

    I wondered if I had to add these two menu items to the same code or if there is some other way of doing it. I see you mentioned the config file and the Xmldocument. So, as I send this I'll go to that and see what I can figure out.

    For this application, all settings are user-scope.

    I really appreciate your help. My project on this is close to being done and if it wasn't for your help (it won't let me rate you anymore) and this board, I doubt I'd be this far on it.
    Christine Lee
    VB '05 Newbie
    Anything but ordinary
    ------------------------------
    Newbie: VB '05 Express Videos:
    http://msdn.microsoft.com/vstudio/ex...g/default.aspx
    My Space! My family!
    http://www.myspace.com/christineeve

  7. #7

    Thread Starter
    Lively Member Christineeve's Avatar
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    78

    Re: [2005] MenuItem Checked

    Oh, one more thing, I was thinking of just creating a registry setting. I would set the setting to 1 for play the mission. Then if the user unclicks the check box, the code writes a 0 over the 1. Everytime the ap ran it would read the registry and do the right thing depending if it's a 1 or a 0. I can do this, but if .Net offers a better way such as the config file, I'd rather do that.
    Christine Lee
    VB '05 Newbie
    Anything but ordinary
    ------------------------------
    Newbie: VB '05 Express Videos:
    http://msdn.microsoft.com/vstudio/ex...g/default.aspx
    My Space! My family!
    http://www.myspace.com/christineeve

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

    Re: [2005] MenuItem Checked

    You have to either save all your settings or none. If you have unchecked the autosave box then you need to save the settings yourself. You would normally do that by calling My.Settings.Save.

    Here's what I would recommend for you. You should create application settings with User scope for all the values that you do or might want to persist. All the ones that you definitely want to persist you can bind to control properties. All those that you may or may not want to persist you should not bind. When you load the relevant form you can manually assign the setting value to the control property. When you're done you can assign the value of the control property back to the seeting or not, depending on whether you want to persist it or not. You can then recheck the autosave option because you know that only those settings that you want saved have been updated.
    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

  9. #9

    Thread Starter
    Lively Member Christineeve's Avatar
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    78

    Re: [2005] MenuItem Checked

    Quote Originally Posted by jmcilhinney
    You have to either save all your settings or none. If you have unchecked the autosave box then you need to save the settings yourself. You would normally do that by calling My.Settings.Save.

    Here's what I would recommend for you. You should create application settings with User scope for all the values that you do or might want to persist. All the ones that you definitely want to persist you can bind to control properties. All those that you may or may not want to persist you should not bind. When you load the relevant form you can manually assign the setting value to the control property. When you're done you can assign the value of the control property back to the seeting or not, depending on whether you want to persist it or not. You can then recheck the autosave option because you know that only those settings that you want saved have been updated.
    Hi,
    Yesterday you provided this advice to another VB.Net developer:
    http://www.vbforums.com/showthread.php?t=407066

    This is the problem I'm having also, while I'm using the 2.x, would this work for me?

    I followed your advice and I've still not been able to persist the state of the checkmarks.
    Christine Lee
    VB '05 Newbie
    Anything but ordinary
    ------------------------------
    Newbie: VB '05 Express Videos:
    http://msdn.microsoft.com/vstudio/ex...g/default.aspx
    My Space! My family!
    http://www.myspace.com/christineeve

  10. #10

    Thread Starter
    Lively Member Christineeve's Avatar
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    78

    Re: [2005] MenuItem Checked

    Quote Originally Posted by Christineeve
    Hi,
    Yesterday you provided this advice to another VB.Net developer:
    http://www.vbforums.com/showthread.php?t=407066

    This is the problem I'm having also, while I'm using the 2.x, would this work for me?

    I followed your advice and I've still not been able to persist the state of the checkmarks.
    Nevermind, it doesn't work. Who knew saving a checkmark on a menu had to be so difficult.
    Christine Lee
    VB '05 Newbie
    Anything but ordinary
    ------------------------------
    Newbie: VB '05 Express Videos:
    http://msdn.microsoft.com/vstudio/ex...g/default.aspx
    My Space! My family!
    http://www.myspace.com/christineeve

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

    Re: [2005] MenuItem Checked

    It isn't difficult. You just bind to a setting and it's done. You're making it difficult by the fact that you wnat to persist other values sometimes but not others, but that's not difficult either. I've explained how to do it in post #8.
    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

  12. #12

    Thread Starter
    Lively Member Christineeve's Avatar
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    78

    Re: [2005] MenuItem Checked

    Quote Originally Posted by jmcilhinney
    It isn't difficult. You just bind to a setting and it's done. You're making it difficult by the fact that you wnat to persist other values sometimes but not others, but that's not difficult either. I've explained how to do it in post #8.
    Hi,
    You explained it well. I performed those steps, but it still doesn't work. I am overlooking some detail but I cannot figure out what. I set this up exactly how I set up the settings for the command buttons but it still doesn't properly function.

    Edit: Specifically, I have my.settings.save created but it still doesn't save the setting although the code is created and available for it to be set. Again, just like I set up the command buttons. In this case, the default for the state is true, when I uncheck, save and close, the application reopens with the default set to true.

    I appreciate your willingness to continue to help me when I'm struggling to get this right.
    Christine Lee
    VB '05 Newbie
    Anything but ordinary
    ------------------------------
    Newbie: VB '05 Express Videos:
    http://msdn.microsoft.com/vstudio/ex...g/default.aspx
    My Space! My family!
    http://www.myspace.com/christineeve

  13. #13

    Thread Starter
    Lively Member Christineeve's Avatar
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    78

    Re: [2005] MenuItem Checked

    Hi,
    If you would take one more look, maybe your expert eye can see what I might be overlooking. To me it looks exactly like what I want, but my.settings.save just isn't changing the state from true to false, or indeterminate to false/true.

    Here is the code snippets.

    Thank you for looking at this problem again.

    VB Code:
    1. Private Sub DisplayWarningToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayWarningToolStripMenuItem.Click
    2.         'Sets the status of the Display Warning either on or off.
    3.         Dim mywarning As New ToolStripMenuItem
    4.         'Dim myform As New Form
    5.         'Dim myform As New frmDFDisplay
    6.  
    7.         Try
    8.             'myform.Show()
    9.  
    10.             If mywarning.Checked Then
    11.                 MsgBox("You have turned ON the display advisory. This advisory will appear everytime you play DarkFaction.", MsgBoxStyle.Information, "Mission Briefing")
    12.                 'displaystatus = 1
    13.             Else
    14.                 MsgBox("You have turned OFF the display advisory, it will no longer appear.", MsgBoxStyle.Information, "Mission Briefing")
    15.                 'displaystatus = 0
    16.             End If
    17.             My.Settings.Save()
    18.  
    19.         Catch ex As Exception
    20.             If Err.Number <> 0 Then
    21.                 errLogger(Err.Number, Err.Description, Err.Source)
    22.             End If
    23.             Exit Sub
    24.         Finally
    25.  
    26.         End Try
    27.     End Sub

    Userscopesettings:
    VB Code:
    1. <Global.System.Configuration.UserScopedSettingAttribute(),  _
    2.          Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
    3.          Global.System.Configuration.DefaultSettingValueAttribute("True")>  _
    4.         Public Property DisplayWarningToolStripMenuItem() As Boolean
    5.             Get
    6.                 Return CType(Me("DisplayWarningToolStripMenuItem"),Boolean)
    7.             End Get
    8.             Set
    9.                 Me("DisplayWarningToolStripMenuItem") = value
    10.             End Set
    11.         End Property

    and this for the settings of the project:

    VB Code:
    1. <setting name="DisplayWarningToolStripMenuItem" serializeAs="String">
    2.                 <value>True</value>
    Last edited by Christineeve; May 24th, 2006 at 06:13 AM.
    Christine Lee
    VB '05 Newbie
    Anything but ordinary
    ------------------------------
    Newbie: VB '05 Express Videos:
    http://msdn.microsoft.com/vstudio/ex...g/default.aspx
    My Space! My family!
    http://www.myspace.com/christineeve

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

    Re: [2005] MenuItem Checked

    I really don't understand what you're trying to achieve with that code. You have a Click event handler for a menu item in which you create a completely new menu item and then test its Checked property, and then you save your settings. Have you set the CheckOnClick property of the menu item to True so that it actually changes its Checked property when you click it? Have you bound the Checked property to the application setting? Why are you creating a new menu item and texting its Checked property instead of testing the Checked property of the menu item that weas just clicked? Why do you need to save the settings when it will done automatically when you exit the application?

    Finally, and less importantly, that is not a good name for the setting. It's a Boolean, not a ToolStripMenuItem, so naming it "DisplayWarningToolStripMenuItem" does not describe what it represents. It should be named either "DisplayWarning" or "DisplayWarningToolStripMenuItemChecked" because those are the two things that its value actually represents.
    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

  15. #15

    Thread Starter
    Lively Member Christineeve's Avatar
    Join Date
    Mar 2006
    Location
    Germany
    Posts
    78

    Re: [2005] MenuItem Checked

    Hi,
    Yes I have set up the controls, their states are set to true and they are bound to application settings. The problem is that their state is not automatically saving. That is exactly my question, "Why aren't their states being saved?"

    I do not know why I set the new menu instance. It was something I tried to get this to work.

    I have no clue why something that should automcally be saving won't save.


    It's no problem. I will give up and move on.
    Christine Lee
    VB '05 Newbie
    Anything but ordinary
    ------------------------------
    Newbie: VB '05 Express Videos:
    http://msdn.microsoft.com/vstudio/ex...g/default.aspx
    My Space! My family!
    http://www.myspace.com/christineeve

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