|
-
May 22nd, 2006, 05:39 AM
#1
Thread Starter
Lively Member
[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:
'The default setting is checked.
Private Sub MissionBriefingToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MissionBriefingToolStripMenuItem.Click
Dim mymenu As New ToolStripMenuItem
Try
If mymenu.CheckState = CheckState.Checked = True Then
MsgBox("You have turned ON the Mission Briefing. _
If you wish to turn it off, _
remove the check from the 'View Mission Briefing' box.", _
MsgBoxStyle.Information, "Mission Briefing")
Else
'mymenu.CheckState = CheckState.Checked = false Then
MsgBox("You have turned OFF the Mission Briefing. _
To turn it back on, place a check mark in the 'View Mission _
Briefing' box.", MsgBoxStyle.Information, "Mission Briefing")
End If
Catch ex As Exception
If Err.Number <> 0 Then
errLogger(Err.Number, Err.Description, Err.Source)
End If
Exit Sub
Finally
End Try
End Sub
Private Sub MissionBriefingToolStripMenuItem_CheckStateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _
MissionBriefingToolStripMenuItem.CheckStateChanged
Dim mymenu As New ToolStripMenuItem
Try
If mymenu.CheckState = CheckState.Checked Then
MsgBox("You have turned ON the Mission Briefing. _
If you wish to turn it off, remove the check from the _
'View Mission Briefing' box.", MsgBoxStyle.Information, "Mission Briefing")
ElseIf mymenu.CheckState = CheckState.Unchecked Then
MsgBox("You have turned OFF the Mission Briefing. _
To turn it back on, place a check mark in the 'View Mission _
Briefing' box.", MsgBoxStyle.Information, "Mission Briefing")
End If
Catch ex As Exception
If Err.Number <> 0 Then
errLogger(Err.Number, Err.Description, Err.Source)
End If
Exit Sub
Finally
End Try
End Sub
Private Sub PlayMissionBriefing()
Dim MissionBriefingToolStripMenuItem As New ToolStripMenuItem
Try
If MissionBriefingToolStripMenuItem.CheckState = CheckState.Checked Then
LaunchtheMission("mission_briefing", "mission_briefing.exe", "XXXXXXX")
End If
Catch ex As Exception
If Err.Number <> 0 Then
errLogger(Err.Number, Err.Description, Err.Source)
End If
Exit Sub
Finally
End Try
End Sub
-
May 22nd, 2006, 06:06 AM
#2
Re: [2005] MenuItem Checked
This is a pretty silly line:
VB Code:
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:
If (mymenu.CheckState = CheckState.Checked) = True Then
but that's silly too. Change it to:
-
May 22nd, 2006, 06:25 AM
#3
Thread Starter
Lively Member
Re: [2005] MenuItem Checked
 Originally Posted by jmcilhinney
This is a pretty silly line:
VB Code:
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:
If (mymenu.CheckState = CheckState.Checked) = True Then
but that's silly too. Change it to:
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!
-
May 22nd, 2006, 06:52 AM
#4
Thread Starter
Lively Member
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.
-
May 22nd, 2006, 05:43 PM
#5
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.
-
May 23rd, 2006, 12:45 AM
#6
Thread Starter
Lively Member
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.
-
May 23rd, 2006, 12:51 AM
#7
Thread Starter
Lively Member
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.
-
May 23rd, 2006, 01:15 AM
#8
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.
-
May 24th, 2006, 05:24 AM
#9
Thread Starter
Lively Member
Re: [2005] MenuItem Checked
 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.
-
May 24th, 2006, 05:32 AM
#10
Thread Starter
Lively Member
Re: [2005] MenuItem Checked
 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.
-
May 24th, 2006, 05:48 AM
#11
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.
-
May 24th, 2006, 05:57 AM
#12
Thread Starter
Lively Member
Re: [2005] MenuItem Checked
 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.
-
May 24th, 2006, 06:07 AM
#13
Thread Starter
Lively Member
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:
Private Sub DisplayWarningToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayWarningToolStripMenuItem.Click
'Sets the status of the Display Warning either on or off.
Dim mywarning As New ToolStripMenuItem
'Dim myform As New Form
'Dim myform As New frmDFDisplay
Try
'myform.Show()
If mywarning.Checked Then
MsgBox("You have turned ON the display advisory. This advisory will appear everytime you play DarkFaction.", MsgBoxStyle.Information, "Mission Briefing")
'displaystatus = 1
Else
MsgBox("You have turned OFF the display advisory, it will no longer appear.", MsgBoxStyle.Information, "Mission Briefing")
'displaystatus = 0
End If
My.Settings.Save()
Catch ex As Exception
If Err.Number <> 0 Then
errLogger(Err.Number, Err.Description, Err.Source)
End If
Exit Sub
Finally
End Try
End Sub
Userscopesettings:
VB Code:
<Global.System.Configuration.UserScopedSettingAttribute(), _
Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
Global.System.Configuration.DefaultSettingValueAttribute("True")> _
Public Property DisplayWarningToolStripMenuItem() As Boolean
Get
Return CType(Me("DisplayWarningToolStripMenuItem"),Boolean)
End Get
Set
Me("DisplayWarningToolStripMenuItem") = value
End Set
End Property
and this for the settings of the project:
VB Code:
<setting name="DisplayWarningToolStripMenuItem" serializeAs="String">
<value>True</value>
Last edited by Christineeve; May 24th, 2006 at 06:13 AM.
-
May 24th, 2006, 08:22 AM
#14
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.
-
May 24th, 2006, 10:16 AM
#15
Thread Starter
Lively Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|