msgbox prompt to save only if new or changed info
I want to make it so that the message box will only pop up if the info has been changed and not already saved or is new info to be saved. This code makes the msgbox pop up every time "exit" is selected.
Private Sub exitbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exitbutton.Click
Select Case MsgBox("Do you want to save your information before exiting?", MsgBoxStyle.YesNoCancel)
Case MsgBoxResult.Yes
My.Settings.Save()
MsgBox("Your information is saving")
Me.Close()
Case MsgBoxResult.No
MsgBox("Goodbye")
Me.Close()
Case MsgBoxResult.Cancel
MsgBox("You decided to cancel")
End Select
End Sub
[725 byte] By [
Bra49er] at [2008-1-8]
As you appear to saving your info to My.Settings you could use the My.Settings.SettingChanging event to set a flag which you then check in your exit button click event.
Declare the flag
Private InfoIsDirty As Boolean Put something like this in your form load
AddHandler My.Settings.SettingChanging, AddressOf MakeDirty
Create the sub MakeDirty to handle the event
Private Sub MakeDirty(ByVal sender As Object, ByVal e As System.Configuration.SettingChangingEventArgs) InfoIsDirty =
True End Sub and add the check to your exit button
If InfoIsDirty Then ' your save code goes here
End If If you save the settings somewhere else other than the exit button click event then just set InfoIsDirty = False whenever you save the settings.
you need to have a value to tell you if there are changes or not
use the cell changed event
Public
Class Form1 Dim needtosave As Boolean
Private Sub Table1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Table1BindingNavigatorSaveItem.Click Me.Validate() Me.Table1BindingSource.EndEdit() Me.Table1TableAdapter.Update(Me.Database1DataSet.Table1) needtosave = False 'put this at the end of your update code to reset the value
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Table1TableAdapter.Fill(Me.Database1DataSet.Table1) needtosave =
False 'you need this after the fill method because during the fill it will fire the cell changed event several times End Sub
Private Sub Table1DataGridView_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Table1DataGridView.CellValueChanged needtosave = true 'this will tell it that you have made changes
End Sub now in your exit button click event use the needtosave value to determing if you need the msgbox to show up
if needtosave = true then
-- msgbox code here --
else
me.close()
end if
you can also use this concept in any other event that you use to edit data such as the textbox validated event, etc...
js06 wrote: |
| you need to have a value to tell you if there are changes or not use the cell changed event Public Class Form1 Dim needtosave As Boolean Private Sub Table1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Table1BindingNavigatorSaveItem.Click Me.Validate() Me.Table1BindingSource.EndEdit() Me.Table1TableAdapter.Update(Me.Database1DataSet.Table1) needtosave = False 'put this at the end of your update code to reset the value End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Table1TableAdapter.Fill(Me.Database1DataSet.Table1) needtosave = False 'you need this after the fill method because during the fill it will fire the cell changed event several times End Sub Private Sub Table1DataGridView_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles Table1DataGridView.CellValueChanged needtosave = true 'this will tell it that you have made changes End Sub now in your exit button click event use the needtosave value to determing if you need the msgbox to show up if needtosave = true then -- msgbox code here -- else me.close() end if you can also use this concept in any other event that you use to edit data such as the textbox validated event, etc... | |
what is the "update code"?
Sorry about that, the update code is already there above that line
i should have worded that differently
the code is all there you just need to place the needtosave code in your subs
when you are done, your code will look pretty much like mine except have all your names
as long as you place the needtosave code in the correct locations it will give you what you want
if you have trouble then post your code and i can add it in for you
i think you can get this one though
I am not using a database or anything like that, just saving the text in the textboxes using my.settings.
Public Class IncomeForm
Private Sub IncomeForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = My.Settings.textboxprimaryincome
TextBox2.Text = My.Settings.textboxprimarytotal
TextBox3.Text = My.Settings.textboxsecondaryincome
TextBox4.Text = My.Settings.textboxsecondarytotal
TextBox5.Text = My.Settings.textboxadditionalincome
TextBox6.Text = My.Settings.textboxadditionaltotal
DateTimePicker1.Text = My.Settings.primarydatetimepicker
DateTimePicker2.Text = My.Settings.secondarydatetimepicker
DateTimePicker3.Text = My.Settings.additionaldatetimepicker
ComboBox1.Text = My.Settings.Primarycombobox
ComboBox2.Text = My.Settings.secondarycombobox
ComboBox3.Text = My.Settings.additionalcombobox
End Sub
Private Sub button_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_save.Click
My.Settings.textboxprimaryincome = Me.TextBox1.Text
My.Settings.textboxprimarytotal = Me.TextBox2.Text
My.Settings.textboxsecondaryincome = Me.TextBox3.Text
My.Settings.textboxsecondarytotal = Me.TextBox4.Text
My.Settings.textboxadditionalincome = Me.TextBox5.Text
My.Settings.textboxadditionaltotal = Me.TextBox6.Text
My.Settings.primarydatetimepicker = Me.DateTimePicker1.Text
My.Settings.secondarydatetimepicker = Me.DateTimePicker2.Text
My.Settings.additionaldatetimepicker = Me.DateTimePicker3.Text
My.Settings.Primarycombobox = Me.ComboBox1.Text
My.Settings.secondarycombobox = Me.ComboBox2.Text
My.Settings.additionalcombobox = Me.ComboBox3.Text
My.Settings.Save()
MsgBox("Your information is saving")
End Sub
Private Sub Button_Exit_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Exit.Click
'only want it to ask this if data has been changed and not saved
Select Case MsgBox("Do you want to save your information before closing?", MsgBoxStyle.YesNoCancel)
Case MsgBoxResult.Yes
My.Settings.textboxprimaryincome = Me.TextBox1.Text
My.Settings.textboxprimarytotal = Me.TextBox2.Text
My.Settings.textboxsecondaryincome = Me.TextBox3.Text
My.Settings.textboxsecondarytotal = Me.TextBox4.Text
My.Settings.textboxadditionalincome = Me.TextBox5.Text
My.Settings.textboxadditionaltotal = Me.TextBox6.Text
My.Settings.primarydatetimepicker = Me.DateTimePicker1.Text
My.Settings.secondarydatetimepicker = Me.DateTimePicker2.Text
My.Settings.additionaldatetimepicker = Me.DateTimePicker3.Text
My.Settings.Primarycombobox = Me.ComboBox1.Text
My.Settings.secondarycombobox = Me.ComboBox2.Text
My.Settings.additionalcombobox = Me.ComboBox3.Text
My.Settings.Save()
MsgBox("Your information is saving")
Me.Close()
Case MsgBoxResult.No
MsgBox("Goodbye")
Me.Close()
Case MsgBoxResult.Cancel
MsgBox("You decided to cancel")
End Select
End Sub
End Class
Am I missing something here?
The question asked about showing the message box only when there was new information to be saved. The post indicated that the saving was done with My.Settings.Save()
Databases, BindingNavigators, DataGridViews were not mentioned. Do you have to be clairvoyant to answer questions in this forum now?
<Edit>
Post above beat me to it.
Please see my response above
<\Edit>
Looking at my alerts it appears that three posts have been removed from this thread.
Now you have posted the extra code I suggest you update the setting whenever one of the values you want to save changes.
For instance for textboxes you could do it like this:
Private Sub WriteTextBoxSetting(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
Dim T As TextBox = CType(sender, TextBox) My.Settings.Item(T.Name) = T.Text End Sub
Updating the setting in this way does not save it until you execute My.Settings.Save
Similar methods could be used for datetimepickers and comboboxes, although I hope you are only wanting to save the selected item for the combobox and not the whole list as the code you have posted will not do the latter.
Dave299 wrote: |
| As you appear to saving your info to My.Settings you could use the My.Settings.SettingChanging event to set a flag which you then check in your exit button click event. Declare the flag Private InfoIsDirty As Boolean Put something like this in your form load AddHandler My.Settings.SettingChanging, AddressOf MakeDirty Create the sub MakeDirty to handle the event Private Sub MakeDirty(ByVal sender As Object, ByVal e As System.Configuration.SettingChangingEventArgs) InfoIsDirty = True End Sub and add the check to your exit button If InfoIsDirty Then ' your save code goes here End If If you save the settings somewhere else other than the exit button click event then just set InfoIsDirty = False whenever you save the settings. | |
when i put this code in, the msgbox no longer pops up whether i put new info in or change info to ask if i want to save it.
did i put something in the wrong place by chance?
Public Class IncomeForm
Private InfoIsDirty As Boolean
Private Sub IncomeForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TextBox1.DataBindings.Add("text", My.Settings, "Textsaved")
'this is to make everything save without the save button
TextBox1.Text = My.Settings.textboxprimaryincome
TextBox2.Text = My.Settings.textboxprimarytotal
TextBox3.Text = My.Settings.textboxsecondaryincome
TextBox4.Text = My.Settings.textboxsecondarytotal
TextBox5.Text = My.Settings.textboxadditionalincome
TextBox6.Text = My.Settings.textboxadditionaltotal
DateTimePicker1.Text = My.Settings.primarydatetimepicker
DateTimePicker2.Text = My.Settings.secondarydatetimepicker
DateTimePicker3.Text = My.Settings.additionaldatetimepicker
ComboBox1.Text = My.Settings.Primarycombobox
ComboBox2.Text = My.Settings.secondarycombobox
ComboBox3.Text = My.Settings.additionalcombobox
AddHandler My.Settings.SettingChanging, AddressOf MakeDirty
End Sub
Private Sub MakeDirty(ByVal sender As Object, ByVal e As System.Configuration.SettingChangingEventArgs)
InfoIsDirty = True
End Sub
Private Sub button_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_save.Click
My.Settings.textboxprimaryincome = Me.TextBox1.Text
My.Settings.textboxprimarytotal = Me.TextBox2.Text
My.Settings.textboxsecondaryincome = Me.TextBox3.Text
My.Settings.textboxsecondarytotal = Me.TextBox4.Text
My.Settings.textboxadditionalincome = Me.TextBox5.Text
My.Settings.textboxadditionaltotal = Me.TextBox6.Text
My.Settings.primarydatetimepicker = Me.DateTimePicker1.Text
My.Settings.secondarydatetimepicker = Me.DateTimePicker2.Text
My.Settings.additionaldatetimepicker = Me.DateTimePicker3.Text
My.Settings.Primarycombobox = Me.ComboBox1.Text
My.Settings.secondarycombobox = Me.ComboBox2.Text
My.Settings.additionalcombobox = Me.ComboBox3.Text
My.Settings.Save()
MsgBox("Your information is saving")
End Sub
Private Sub Button_Exit_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Exit.Click
If InfoIsDirty Then
Select Case MsgBox("Do you want to save your information before closing?", MsgBoxStyle.YesNoCancel)
Case MsgBoxResult.Yes
My.Settings.textboxprimaryincome = Me.TextBox1.Text
My.Settings.textboxprimarytotal = Me.TextBox2.Text
My.Settings.textboxsecondaryincome = Me.TextBox3.Text
My.Settings.textboxsecondarytotal = Me.TextBox4.Text
My.Settings.textboxadditionalincome = Me.TextBox5.Text
My.Settings.textboxadditionaltotal = Me.TextBox6.Text
My.Settings.primarydatetimepicker = Me.DateTimePicker1.Text
My.Settings.secondarydatetimepicker = Me.DateTimePicker2.Text
My.Settings.additionaldatetimepicker = Me.DateTimePicker3.Text
My.Settings.Primarycombobox = Me.ComboBox1.Text
My.Settings.secondarycombobox = Me.ComboBox2.Text
My.Settings.additionalcombobox = Me.ComboBox3.Text
My.Settings.Save()
MsgBox("Your information is saving")
Me.Close()
Case MsgBoxResult.No
MsgBox("Goodbye")
Me.Close()
Case MsgBoxResult.Cancel
MsgBox("You decided to cancel")
End Select
End If
End Sub
End Class
Relax Dave, I rushed into the question and made a mistake.
Next time, maybe just say - "js06 he's not a database. Did you not catch that?"
that would be more tactfull.
Bra49er forget my other posts, for some reason i had it in my head that you were using a database
Probably because i have been using a database project today.
Jeff
At the time of writing I was unaware of the three posts that have been removed - I still don't know what they contained as the e-mail alerts only give the first few lines.
It appeared to me that a question had been asked - I had responded with what I believe is an answer and you had responded, apparently to a different question and the OP was discussing your response and ignoring mine. I just wanted to know what the ..... was going on, which you have now clarified.
Bra49er
My first response is a satisfactory solution to the question you first posed. However now you have changed the question by adding the additional code showing where you are actually assigning the settings it is clear that you need to make further changes which are detailed in my other post above.
hey guys I am sorry for all the confusion that went on here. i deleted the posts because i rushed into freaking out about the code without first trying it. I was asking questions about jeff's code first because AT FIRST it confused me less, if that makes sence, and i deleted them before anyone saw them because it occured to me that he thought i was using a database. you both responded to my first question first only and then i left one of my questions to jeff about his code up there because by then he had already reponded to it as i was about to delete it to avoid any confusion, but that obviously didnt work out for me

I wasn't ignoring anyone, i just am very new to this. but thank you both so very much for your help, i really really appreciate it.
Dave299 wrote: |
| Looking at my alerts it appears that three posts have been removed from this thread. Now you have posted the extra code I suggest you update the setting whenever one of the values you want to save changes. For instance for textboxes you could do it like this: Private Sub WriteTextBoxSetting(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged Dim T As TextBox = CType(sender, TextBox) My.Settings.Item(T.Name) = T.Text End Sub Updating the setting in this way does not save it until you execute My.Settings.Save Similar methods could be used for datetimepickers and comboboxes, although I hope you are only wanting to save the selected item for the combobox and not the whole list as the code you have posted will not do the latter. | |
am i supposed to substitute the "t.name" for a setting name? I put this code in and still nothing is happening, the msgbox does not pop up when info is changed or new.
Let's clarify the terminology first. Writing the settings assigns a property of one of the controls to the appropriate setting.
So My.Settings.Textbox1 = TextBox1.Text sets the My.Settings Property Textbox1 to the value contained in Textbox1.Text. But it does not save it (i.e. write it to the settings file) - that only gets done when you call My.Settings.Save (or when you quit if you have SaveSettings on exit checked.
Having written the settings if any of the property values have been changed then the My.Settings.SettingChanging event will be raised. This event can be used to set a flag (InfoIsDirty) to indicate that the info has changed.
The way that you have written your code means that the settings are not being written until after you click Yes on the message box. At that stage the settings haven't been written so they can't have changed and so the flag will not have been set. If you want to continue to do it this way then you need to move the lines that write the settings to the top of the sub so that when you get to "If InfoIsDirty" they will have been written.
Although writing the settings in the way you have done is ok you have probably written exactly the same code in several places, wherever you wish to save the settings. One way round that is to be put the saving code into a subroutine and call that whenever you want to write the settings.
What I was suggesting above is a more general method which causes the settings to be written whenever the value to which they refer changes. You therefore have no need to specifically write them as it will happen every time the info changes. So when you want to save them you just call My.Settings.Save - you don't have to write them all again.
Have a look at this example which uses three textboxes and may make it clearer
Public
Class Form1
Dim WithEvents Textbox1 As New TextBox Dim WithEvents Textbox2 As New TextBox Dim WithEvents Textbox3 As New TextBox Dim WithEvents Button1 As New Button Dim InfoIsDirty As Boolean Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Textbox1.SetBounds(20, 20, 100, 25)
Textbox1.Name =
"Textbox1" Textbox1.Text =
My.Settings.Textbox1 AddHandler Textbox1.TextChanged, AddressOf SaveToSetting Textbox2.SetBounds(20, 70, 100, 25)
Textbox2.Name =
"Textbox2" Textbox2.Text =
My.Settings.Textbox2 AddHandler Textbox2.TextChanged, AddressOf SaveToSetting Textbox3.SetBounds(20, 120, 100, 25)
Textbox3.Name =
"Textbox3" Textbox3.Text =
My.Settings.Textbox3 AddHandler Textbox3.TextChanged, AddressOf SaveToSetting Button1.SetBounds(150, 20, 100, 50)
Button1.Text =
"Do I need to save settings" Controls.Add(Textbox1)
Controls.Add(Textbox2)
Controls.Add(Textbox3)
Controls.Add(Button1)
AddHandler My.Settings.SettingChanging, AddressOf MakeInfoDirty End Sub Private Sub SaveToSetting(ByVal sender As Object, ByVal e As EventArgs)
Dim T As TextBox = CType(sender, TextBox) My.Settings.Item(T.Name) = T.Text End Sub Private Sub MakeInfoDirty(ByVal sender As Object, ByVal e As System.Configuration.SettingChangingEventArgs) InfoIsDirty =
True End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If InfoIsDirty Then MsgBox(
"Yes") Else MsgBox(
"No") End If End Sub End
Class