Solved

VB.net - Enable Save button upon Object Changes

Posted on 2014-02-03
9
650 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39832630
Is this when you are adding records or when editing? How is your form bound?
0
 
LVL 28

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

756 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