Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.net - Enable Save button upon Object Changes

Posted on 2014-02-03
9
Medium Priority
?
715 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 45

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
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!

 
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 1500 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Suggested Courses

879 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