Solved

VB.net - Enable Save button upon Object Changes

Posted on 2014-02-03
9
632 Views
Last Modified: 2014-02-10
Hi,

I would like to know the best way to enable a "save" button on a form when any of the objects values on the form have changed.

I know I could use the event textChanged etc but I have many objects of different types (textboxes, combo, checkbox) and coding the changed event for each object will take a long time.

Is there an approach where I can track all object changes?

Thanks, Greg
0
Comment
Question by:spen_lang
  • 2
  • 2
  • 2
  • +3
9 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39829035
I don't know of any easier way.
Note each 'changed' event can be added rapidly (via the context menu options from the form design) without you having to write code and each event can just have one line of your code - eg. calling a sub EnableSaveButton
0
 
LVL 12

Expert Comment

by:Paul_Harris_Fusion
ID: 39829128
Here is some sample code using the AddHandler syntax in the form New.
You could extend this to iterate through the Forms control collection and automatically monitor each TextChanged event if that suited you.

For testing I started with a blank form and dragged on TextBox1, Label1, ComboBox1 and btnSave.

Public Class Form1

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        InitMonitoring(TextBox1)
        InitMonitoring(ComboBox1)

    End Sub

    Sub InitMonitoring(ByVal ctl As Control)
        AddHandler ctl.TextChanged, AddressOf HandleTextChanges
    End Sub

    Private Sub HandleTextChanges(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim ctl As Control = TryCast(sender, Control)
        If ctl Is Nothing Then Return

        Label1.Text = ctl.Name & " text property changed"

        btnSave.enabled = True

    End Sub



End Class

Open in new window

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 39831789
Hi...
There is another way...
I use "Tag" in controls..I set the Tag = "Required" And when all "Required" Controls been filled then the Save Button goes to enabled.Take a look here..
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_28074682.html..

Yiannis
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39832630
Is this when you are adding records or when editing? How is your form bound?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

Expert Comment

by:Ark
ID: 39838372
Smth like this?
    Private controlState As New Dictionary(Of Control, String)
    Private affectedControls As Control() = {CheckBox1, CheckBox2, CheckBox3, TextBox1, TextBox2, TextBox3, ComboBox1}
  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        setState()
    End Sub

    Private Sub setState()
        controlState.Clear()
        For Each c In affectedControls
            Dim s As String = ""
            If TypeOf c Is TextBox Then
                s = c.Text
            ElseIf TypeOf c Is ComboBox Then
                s = CType(c, ComboBox).SelectedIndex.ToString
            ElseIf TypeOf c Is CheckBox Then
                s = CType(c, CheckBox).Checked.ToString
            End If
            controlState.Add(c, s)
        Next
    End Sub

    Private Function IsDirty() As Boolean
        For Each c In affectedControls
            Dim s As String = ""
            If TypeOf c Is TextBox Then
                s = c.Text
            ElseIf TypeOf c Is ComboBox Then
                s = CType(c, ComboBox).SelectedIndex.ToString
            ElseIf TypeOf c Is CheckBox Then
                s = CType(c, CheckBox).Checked.ToString
            End If
            If s <> controlState(c) Then Return True
        Next
        Return False
    End Function

Open in new window

0
 

Author Comment

by:spen_lang
ID: 39838377
This is when I am editing records
0
 

Author Comment

by:spen_lang
ID: 39838379
The form objects are bound to a dataset
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39838419
One option is to use DataTable.GetChanges method. It returns you any rows that have been modified. But this will only work once user has moved out of a textbox (which would commit changes to the dataset).
0
 
LVL 12

Accepted Solution

by:
Paul_Harris_Fusion earned 500 total points
ID: 39838468
DataTable.GetChanges will show you the changed records but you still need something to trigger the call to GetChanges.

So you either need
1. to monitor the controls
     1a with individual event handlers generated by the code editor
     1b a common event handler wired up by a call to AddHandler
2.  to monitor the underlying datatable for changes
 
Option 1 has been covered

Option 2 can also be done with AddHandler but here is a method using the VB generated event handlers:

In your form declare :

private withevents  _table as datable

' wherever you retrieve the data you need to have a line like:
_table = MyDataset.Tables("Customers")   ' modify as appropriate


' Then you can declare an event handler against the table
' You can also monitor RowChanging rather than RowChanged if that suits you better.

Private Sub _dt_RowChanged(ByVal sender As Object, ByVal e As System.Data.DataRowChangeEventArgs) Handles _dt.RowChanged
     ' enable your save button  here

    End Sub
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now