kservice Posted June 15, 2004 Posted June 15, 2004 Here's what I want to do. :D When I load a forms controls with values (like for an edit) I would like to capture those values and place them in a list(?) somewhere (something like an array?) so that if the user changes a value the changed event on the control would fire and that controls value would be compared to it�s matching item in the list. If it�s changed then I would have that information to be used later. Like if the user tries to exit the form without saving, ect� Ideally this would be a generic function that I could use on any form to detect changes. Thanks I hope this is clear what I want to do. If anyone has any suggestions on how to do this or have a better idea, I'm all ears (well all eyes anyway :rolleyes: ) Appreciate your time. ;) Quote
JABE Posted June 15, 2004 Posted June 15, 2004 Instead of reinventing the wheel, you can just use a datatable to bind your controls to, then just hook up to the datatable events (RowChanged, ColumnChanged, etc.) Quote
*Experts* Nerseus Posted June 15, 2004 *Experts* Posted June 15, 2004 In addition to Jabe's comments, if you bind to a DataSet you can check the DataSet's HasChanges property which will quickly tell you if anything's changed, and what. A DataSet also automatically stores the "original" value alongside the changed value so you can compare them. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
kservice Posted June 15, 2004 Author Posted June 15, 2004 Thanks for the suggestions. I will check it out and see if it works for me. I have just one piece of a large project and we are not binding anything yet. I like the idea though! :p Like I said, I knew what I wanted to accomplish but was unsure about the path I should be taking. You guys helped me along!!! Thanks :D Quote
*Experts* Nerseus Posted June 15, 2004 *Experts* Posted June 15, 2004 If you're not binding, you might still be able to use the DataSet. I imagine you have some code to take the values out of controls and put them into a DataSet or some SQL INSERT/UPDATE/DELETE? If so, maybe you'd be better off moving the data from the controls into the DataSet, which will make the dataset marked as "Inserted", "Updated", etc. Something to keep in mind. I've seen people put the original values in the Tag property of a control. Personally, it feels wrong to overuse the Tag property. But, it might work for you. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
kservice Posted June 16, 2004 Author Posted June 16, 2004 We run everything through classes, through a data access layer to the database. I don't have any control over that. I do have an overload where I can return the data as a dataset which I have done. Now I don't know what to do. :( What I really want is a fast, efficient way to tell if the user has selected a control and changed the value in it. I actually don't care what they have changed it to; I just want to know if it is different from the value that was inserted into it from the fill. And I want to know the second they have done it. That way I can provide them with a visual cue that the record needs saved and if they exit the form or try to go to a different record I can prompt them that they made changes and either save them or ignore them What I am not visualizing is where I bind the dataset to the control and how to detect a change without writing code in every change event; which I don�t want to do. I am assuming that you would bind it when you bring it in, but then what. I don�t mind having one call in every event to a function or something but the goal is to reduce lines of code. So if possible could you please explain a little more of what I should be doing. An example is always great. Thanks for putting up with me. :rolleyes: Quote
*Experts* Nerseus Posted June 16, 2004 *Experts* Posted June 16, 2004 You can always have all of your events (or the common ones at least) fire an event that triggers your flag. For example, if you're in C# and you have two textboxes: textBox1.TextChanged += new System.EventHandler(this.textBox_Changed); textBox2.TextChanged += new System.EventHandler(this.textBox_Changed); // farther down private void textBox_Changed(object sender, System.EventArgs e) { // Form-level variable or property (a property could enable a "Save" button) HasChanges = true; } By "common" I mean you'd need an event for each event type. Above, you could tie any contro's approprate event to textBox_Changed, provided the event was a System.EventHandler type. For something like a combobox with a SelectedIndexChanged event, you'd need a *second* event handler and tie all of your comboboxes to it. If ALL of your textBoxes are "bound", then you could automate the code by looping through all controls on a form and checking their type and wiring up an event automatically. Also, if you stored the original value in something like a Tag property (on all controls), then you could wire up ALL control's Leave event and compare some value of the control to the Tag property. If you do custom binding where you set and read the values of controls manually then you get speed at runtime but you get more complex code (usually harder to maintain as well). As you develop more and more apps, you'll likely develop a "framework" of classes and methods (such as looping through all controls and wiring up TextChanged events) to automate some of your custom binding. In the end, you may end up with code that runs at the same speed as the natively bound controls. Something to keep in mind when weighing the benefits of native binding versus custom "binding". I've tried custom and native and they both have their pros/cons. The new features of .NET make it a LOT easier to do native binding, especially given the power of the Parse/Format events and the features of the DataSet. -Nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
*Experts* jfackler Posted June 16, 2004 *Experts* Posted June 16, 2004 What you are doing is essentially data validation, correct? For all the text boxes on a form you could do this: Dim tbo As TextBox Dim ctrl As Control For Each ctrl In Me.Controls If TypeOf ctrl Is TextBox Then tbo = CType(ctrl, TextBox) AddHandler tbo.Validating, AddressOf tboerrorHandler End If Next Then: Private Sub tboerrorHandler(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Dim tb As TextBox tb = CType(sender, TextBox) If Not tb.Text = "" Then 'compare your text value to the bound dataset value here 'or consider testing for a datatable row change here End If End Sub Look here for hints on tying up row change events. Jon Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.