Solved

VB.net - Enable Save button upon Object Changes

Posted on 2014-02-03
9
669 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

617 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