12 Replies - 1406 Views - Last Post: 06 January 2015 - 08:02 PM Rate Topic: -----

#1 ZakaryHansen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 163
  • Joined: 28-February 14

Simplest way to confirm data has been modified?

Posted 03 January 2015 - 01:55 AM

When the user loads a file into my program, my "Dim Modified as Boolean" will be set to False. If the user modifies the loaded data in any of the tools provided, I'd like it to set the Modified to True so I can remind them to save their changes.

The only way I can think to do this is to do something like..
Private Sub SaveModified(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged, NumericUpDown2.ValueChanged, ... NumericUpDown50.ValueChanged, etc.
    Modified = True
    'code to remind user
End Sub


The only issue is that I've got over 300 NumericUpDowns, a few Textboxes, etc. for the user to modify lots of data with. Is there a simple way to flag that the data has been modified without identifying literally every single tool?

Is This A Good Question/Topic? 0
  • +

Replies To: Simplest way to confirm data has been modified?

#2 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1460
  • View blog
  • Posts: 3,703
  • Joined: 01-February 13

Re: Simplest way to confirm data has been modified?

Posted 03 January 2015 - 06:41 AM

Hi,

When you say they modify the loaded data, do you mean they modify some text in a Textbox or RichTextbox some how? If so, then you could just use the TextChanged event of the TextBox or RichTextBox to set the boolean variable to True.

If it is not in a textbox or richtextbox then maybe you can save the file in a class scoped variable and when the user wants to close it then compare the file that has been changed with the saved one to see if they are the same. Show a messagebox to ask if they want to save it first.

There may be a few other ways to do this but, without knowing what the file type is and how it is being modified then it is kind of guessing on our end.

This post has been edited by IronRazer: 03 January 2015 - 06:43 AM

Was This Post Helpful? 0
  • +
  • -

#3 ZakaryHansen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 163
  • Joined: 28-February 14

Re: Simplest way to confirm data has been modified?

Posted 03 January 2015 - 03:48 PM

There's many different tools to modify data in my program.
There's NumericUpDowns, textboxes, comboboxes, checkboxes, etc. I was hoping there would be a simple way to just flag once data has been changed, but if there's not, I can certainly do .ValueChanged, .TextChanged, etc. and flag it that way.
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1460
  • View blog
  • Posts: 3,703
  • Joined: 01-February 13

Re: Simplest way to confirm data has been modified?

Posted 03 January 2015 - 04:15 PM

Hi,

Well, if there is not just one control that you can monitor for changes then you can go the way you where and monitor all the controls for changes or you can check out the CodeProject link below. It is made just for this purpose and they show example code of how to use it in VB and C#. :)

Form Changed Control
Was This Post Helpful? 0
  • +
  • -

#5 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1460
  • View blog
  • Posts: 3,703
  • Joined: 01-February 13

Re: Simplest way to confirm data has been modified?

Posted 03 January 2015 - 05:13 PM

Now that i looked at that control a little more, you might not be able to open the project because of its age, i`m not sure. :/
Was This Post Helpful? 0
  • +
  • -

#6 ZakaryHansen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 163
  • Joined: 28-February 14

Re: Simplest way to confirm data has been modified?

Posted 03 January 2015 - 05:26 PM

I'm able to open it, but I can't seem to implement the control into my existing project, no matter what I do.

The control works fine with the provided source code, but I'm not sure how to put the component into mine.
Was This Post Helpful? 0
  • +
  • -

#7 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5078
  • View blog
  • Posts: 13,707
  • Joined: 18-April 07

Re: Simplest way to confirm data has been modified?

Posted 03 January 2015 - 05:27 PM

Are all of these controls (buttons, numericupdowns, textboxes etc) on the same form or do you open up child forms? If on child forms you can use an ok button or something on the child form to ask "Did the state of one of my controls on this form change? If so, mark the data as modified when I press ok". That would at least group your checks together in one control instead of having a ton of events where the only thing that is in the body of the event is marking the data has been modified.

Another thing you could think about is event listening. Attach a function to listen for an event from some controls and as the control triggers an event, it calls your listeners, the function is executed which marks the data as changed. For this look up the topic "Delegates and Events" or a my blog entry on the idea at...

http://www.coderslex...tes-and-events/

Do keep in mind that one event handler can be called from multiple controls.

Third option might be polling. At some interval have a timer or something check the current state of the data against the state it was in when it was first loaded. If they are different, then mark it as changed.

I would just hate for you to create a bunch of one line event handlers just to mark data as been modified. Which option you go with will depend on your design of course and what makes the most sense. :)

This post has been edited by Martyr2: 03 January 2015 - 05:29 PM

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,904
  • Joined: 12-December 12

Re: Simplest way to confirm data has been modified?

Posted 03 January 2015 - 05:38 PM

How are you associating the file with all of the controls? If you populate a DataTable and bind to it then you can subscribe to the DataTable.RowChanged Event, or RowChanging.

This post has been edited by andrewsw: 03 January 2015 - 06:01 PM

Was This Post Helpful? 0
  • +
  • -

#9 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1460
  • View blog
  • Posts: 3,703
  • Joined: 01-February 13

Re: Simplest way to confirm data has been modified?

Posted 03 January 2015 - 06:51 PM

Hi,

Well, i see you have a few other suggestions that may work out easier for you but, if you want to still try the control from CodeProject then here is how. I would suggest trying it on a new form project first so, create a new form project and then you need to add the two classes i added below to the project.

To add the classes go to the VB menu and click (Project) and then select (Add Class). You can leave them as their default names. Replace any code in them with the code from the 2 files i added below. After adding them, save your project.

Then go to the VB menu and click (Build) and select (Build NameOfYourProject).

Then you can go to the Form [Design] tab and you should see the FormChangedComponent at the top of your Toolbox. Drag just one onto the Form. That will add three properties to all the controls on the Form.

1. ChangeEventToMonitor -> Name of event such as TextChanged or ValueChanged
2. MonitorForChanges -> True to monitor the control.
3. ValueNameToMonitor -> The property name it effects like (Text or Value)

After setting those properties on all the controls you want to monitor you can check the FormChangedComponent1.ControlsThatHaveChanged property to see if any controls where changed.

Attached File  Class1.rtf (21.48K)
Number of downloads: 120
Attached File  Class2.rtf (2.14K)
Number of downloads: 101
Was This Post Helpful? 0
  • +
  • -

#10 ZakaryHansen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 163
  • Joined: 28-February 14

Re: Simplest way to confirm data has been modified?

Posted 04 January 2015 - 09:08 AM

View Postandrewsw, on 03 January 2015 - 05:38 PM, said:

How are you associating the file with all of the controls? If you populate a DataTable and bind to it then you can subscribe to the DataTable.RowChanged Event, or RowChanging.

The data is loaded into variables, like Dim data1 as Integer, etc. From there, I reflect that data in a NumericUpDown, which the user can modify.


And I'll try to implement that now, thanks.
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,904
  • Joined: 12-December 12

Re: Simplest way to confirm data has been modified?

Posted 04 January 2015 - 10:00 AM

Looks like it requires ColumnChanged rather than RowChanged:
Public Class Form1
    Private _dt As DataTable = New DataTable("tblTest")
    Private _changed As Boolean = False

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        _dt.Columns.Add("Name", Type.GetType("System.String"))
        _dt.Columns.Add("Number1", Type.GetType("System.Int32"))
        _dt.Columns.Add("Number2", Type.GetType("System.Int32"))

        Dim row As DataRow = _dt.NewRow()
        row("Name") = "Bob"
        row("Number1") = 5
        row("Number2") = 20
        _dt.Rows.Add(row)
        _dt.AcceptChanges()

        Me.txtName.DataBindings.Add("Text", _dt, "Name", False, DataSourceUpdateMode.onpropertychanged)
        Me.nupFirst.DataBindings.Add("Value", _dt, "Number1", False, DataSourceUpdateMode.onpropertychanged)
        Me.nupSecond.DataBindings.Add("Value", _dt, "Number2", False, DataSourceUpdateMode.onpropertychanged)

        AddHandler _dt.ColumnChanged, New DataColumnChangeEventHandler(AddressOf Column_Changed)
    End Sub

    Private Sub Column_Changed()
        If Not _changed Then
            _changed = True
            MessageBox.Show("Something has changed..")
            'could remove handler..
        End If
    End Sub

End Class

This makes sense, as it is the first change to any column. RowChanged probably requires some movement between rows, but we only want a single row.
Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • say what now
  • member icon

Reputation: 6409
  • View blog
  • Posts: 25,904
  • Joined: 12-December 12

Re: Simplest way to confirm data has been modified?

Posted 04 January 2015 - 10:08 AM

I prefer to remove the handler once it's done its job:
        AddHandler _dt.ColumnChanged, AddressOf Column_Changed
    End Sub

    Private Sub Column_Changed(sender As Object, e As DataColumnChangeEventArgs)
        If Not _changed Then
            _changed = True
            MessageBox.Show("Something has changed..")
            RemoveHandler _dt.ColumnChanged, AddressOf Column_Changed
        End If
    End Sub

Was This Post Helpful? 0
  • +
  • -

#13 ZakaryHansen  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 163
  • Joined: 28-February 14

Re: Simplest way to confirm data has been modified?

Posted 06 January 2015 - 08:02 PM

So, I had implemented the given suggestions. They worked as they're intended to, but I decided to zero in on the problem areas that really needed to be addressed, rather than the entire form, so I flagged a few buttons that caused the most data changes, and added a visual element for the user to see they need to save.

Thanks for the help, though! :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1