Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.net - Enable Save button upon Object Changes

Posted on 2014-02-03
9
Medium Priority
?
698 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
Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

 
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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

722 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