Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I have a form using unbound controls (text boxes and combo boxes) with a "Update" and "Cancel" button. If the user makes any changes on the form and then clicks "Cancel" I want to display a form asking if they want to save the changes. What is the best method to determine if the form is "Dirty"?

 

I am using VB.Net 2003.

  • *Experts*
Posted

Im not really sure If understanding you right so this maybe not what you are looking for :).

You could add a handler to all your textboxes and then another one for comboboxes, handler to event that would indicate that the control has been modified, like TextChanged for TextBox. And have a boolean value which would be set to true if anything is modified. Then you check when the person hits cancel what the value is and do what you have to do when the value equals true or false. And when you save the state just set it back to false.

Posted

What is the best method to determine if the form is "Dirty"?

 

Take a wet paper cloth and rub it over your form.

Gamer extraordinaire. Programmer wannabe.
Posted

you could also check if it has " cert 18 " or " XXX " in the caption though

 

Wouldn't that be a "kinky" form? ;)

Gamer extraordinaire. Programmer wannabe.
Posted

The problem with the text changed event (for text boxes) is that it is raised when the text box is initialized in the form's load event (not changed by the user) and with each keystroke in the textbox (when changed by the user).

 

Due to the comments posted to this thread, I will repost this question at a forum with more experienced and mature programmers.

Posted

Write a class, which encapsulates the logix and use this class to determine whether things have changed for example.

 

class MyBusinessLogic
{
   private string propertyOne;
   private string propertyTwo;

   private mflgDirty;

   public MyBusinessLogic
   {
   }

    public string PropertyOne
   {
        get
        {
              return propertyOne;
        }
        set
        {
              if ( propertyOne <> value )
              {
                   PropertyOne  = value;
                   mflgDirty = true;
              }
        }
    }

    public bool IsDirty()
    {
         return mflgDirty;
    }
}

 

Oh by the way your asking for trouble insulting people on a forum! For more information on the above goto Google and search for Rockford Lhotka and look at his CSLA Architecture, which will give you what you wanted.

 

And one last note the other way mentioned does work, just write the following line in the method.

 

if ( mflgLoading ) exit 

 

Obviously you have set the flag in the first place but you would want us to spoon feed answers to you.

Posted

Settle down m8. They're only having a joke...

 

Why don't you do something like this:

 


   Private bLoading As Boolean
   Private bControlChanged As Boolean

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

       bLoading = True

       '** Do loading stuff here

       bLoading = False

   End Sub

   Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
       If Not bLoading Then bControlChanged = True
   End Sub

   Private Sub Save()

       If bControlChanged Then

           '** Save record here

       End If

   End Sub

 

Thanks dsgreen57, you beat me to it. I thought that a VB answer would be helpful as well though ;-)

Posted

Thanks for the reasonable responses.

 

The text changed event will still raise the event 100 times if the user keys in 100 characters in a text box. I see no good reason to use the text changed event for this purpose. (this would evaluate the if and set the boolean value 100 times. It would work but probably is not the most efficient way, which I already new.) I will probably create a structure in the load event (with each textbox contents)and compare it with the textbox contents in the exit routine.

 

As for insulting people on this forum, they asked for it by making the responses they made. They probably had no idea what "Dirty" meant but feel a need to respond to every post.

  • *Experts*
Posted

If you are databinding, you could use the DataSet's built in HasChanges method.

 

If you're not using the Tag property for anything, you might store the initial values of all the controls in their Tag (at the bottom of Form_Load) and compare things that way. Or you could use a more custom solution such as storing the values in a hashtable or somesuch.

 

I don't think one IF on a bool and setting one bool on every keystroke is that much overhead. Plus, you can reuse the event handler for every control to simplify things. That's the route I'd probably go if I weren't binding. Keep in mind that your way won't allow you to show any kind of indicator to the user that something's changed (such as MS Word's asteric that appears as soon as something changes). This may not be a big deal, especially in a dataentry type of app where you're likely just checking if something's changed on form_closing.

 

-Nerseus

"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
Posted

If the user makes any changes on the form and then clicks "Cancel" I want to display a form asking if they want to save the changes.

 

The text changed event will still raise the event 100 times if the user keys in 100 characters in a text box.

 

Isn't that what you're trying to do, determine when something on the form has changed? If the user types something in a TextBox, wouldn't that mean that the TextBox has changed?

 

It sounds like you don't want to detect changes on a TextBox at all. If that's the case then just make a boolean "hasChanged" and set it to true when an appropriate control (such as your ComboBox) changes. It should be the constructor in which all your controls and default values are initialized, so set "hasChanged" to false at the end of your constructor.

 

I think that's pretty much what mutant said. Is there a reason why this method doesn't work?

 

As for insulting people on this forum, they asked for it by making the responses they made.

 

No one insulted you, and mutant answered your question. There's no harm in adding a little fun and humor.. and you know what they say, to laugh is to live longer. ;)

Gamer extraordinaire. Programmer wannabe.
Posted (edited)

Controls Validate Event?

 

What about using the controls Validate event? If there's a value in the control to start with, you could pass that to a variable and then, in the validate event, check to see if it matches and if not, set the boolean accordingly. This way, it's only looking for changes after the user has left the control. Sorry I don't have any example code but, I think you get the general idea. I'll try and whip something up if need be...

 

Due to the comments posted to this thread, I will repost this question at a forum with more experienced and mature programmers.

I'd almost bet that they're contributors on almost any other forum you find...good luck to ya! And, by the way, these are some of the most experienced developers I've interacted with.

Edited by Mothra
Being smarter than you look is always better than looking smarter than you are.
Posted

Maybe a little something different...

 

Upon closer examination, I have decided that I'm not smart...I think using the controls Leave event is what I was thinking of...

 

A little something like this...

 

First -->

 

Public Class Form1
   Inherits System.Windows.Forms.Form

   'Setup some publuc variables for each control and the "dirty" flag at the form level...
   Dim myTextboxValue As String
   Dim myComboboxValue As String
   Dim isDirty As Boolean

 

Next -->

 

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
       'Put any initial or default values into the controls here...
       
       'Now set the values to the variables for each control...
       myTextbox = TextBox1.Text
       myCombobox = ComboBox1.Text
       If CheckBox1.Checked Then
           myCheck = True
       Else
           myCheck = False
       End If

   End Sub

 

Next -->

 

'Check for changes made to the value of hte controls...
   Private Sub ComboBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.Leave
       If Not ComboBox1.Text = myCombobox Then
           'Form is now "dirty", set the flag
           isDirty = True
       End If
   End Sub

   Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
       If Not TextBox1.Text = myTextbox Then
           'Form is now "dirty", set the flag
           isDirty = True
       End If
   End Sub

 

Next -->

 

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       'Check the flag and give the user options...
       If isDirty Then
           MessageBox.Show("Save Changes?", "Your Message...", MessageBoxButtons.YesNoCancel)
       End If
   End Sub

 

Now, this may not be the most elegant way to do this but, it works, and that's what really matters, right?

Being smarter than you look is always better than looking smarter than you are.
Posted

wyrd,

 

I do want to detect a change in a text box contents, but not for each keystroke in the textbox. As I stated eariler, if the user keys 100 characters in one textbox, the text changed handler is executed 100 times. That may be insignificant overhead, but I prefer to do it another way. The text changed handler will not detect if the contents changed, just the fact that the user made keystrokes in the texbox. The user could key data in the text box and then press the backspace key until the textbox contained the original contents. If the user then clicks the Exit button, there is no need to do the prompt. The same applies to the combo box. The user can select a new item from the drop down list and then change it back to the original selection (in effect, no changes).

 

Before I post a question, I try and do as much research as possible, because I learn more doing the research. I had explored all the events with the textbox and combo box and decided that none suited my purposes (these events detect keystrokes, not content change and keystokes do not necessarily mean content change). All other processes I could find in forums were about data bound controls not unbound controls.

 

It is apparent that I must compare the contents at exit time with the original contents, using either the tag property or a structure data type, to achieve the result I prefer. I do not want to prompt the user if the contents did not change, no matter how many keystrokes the user made.

 

I don't know where you got the idea that I was insulted or felt insulted. When I read the posts above I get the impression that I was the insulter not the insultee.

  • Leaders
Posted

can i ask a question now about this...

Due to the comments posted to this thread, I will repost this question at a forum with more experienced and mature programmers.

sadly i have come to believe there must only be a few people on this planet now who realise that life is to short for being serious 24 hours a day :-\ , if trying to bring a bit of humour along is classed as imature or in-experienced ( which being a father of 5 children and a fairly experienced programmer, i can hopefully say i'm neither of those ), then i now realise how bad this planet is getting :rolleyes:

if you cant take humour or see the fun side of stuff, then sorry for any " insults posted " , but maybe you should look at things a little bit more on the light-hearted side

Posted

We did not give any ideas because we felt that mutant already answered your question in the second post.

 

I don't joke around much but that was a good oportunity! You must admit. You phased the title perfectly!

 

And yes, I knew what you meant by "dirty" as well as everyone else who responded here I'm sure.

C#
Posted

dynamic_sysop,

 

I make my living programming not at comedy. There are forums for comedy, I am sure. When I post a question and receive an e-mail stating that I have a response, it is disappointing to read a post that says wipe your screen with a wet towel. I might have said that in the 3rd grade (if there were computer screens back then). I did a lot of research (and even learned the "Dirty" terminology) before I posted this question. I was well aware of the text changed event (should be named the keystoke made event) and methods using bound controls. That is why I stated that the controls were unbound in my question. Notepad does not use the text changed event (open notepad key in 2 blanks and 2 backspaces, no prompt is issued when exiting) and certainly it is not bound to a data source. I thought there must be some magic method to accomplish the IsDirty state. There is a lot of magic in .Net (or any o-o lang.) when compared to 3rd generation languages, such as inheritance and polymorphism and classes. But after careful consideration, I have coded this question the old fashion way.

 

I have posted several questions on this forum, and for the most part received excellent responses, and this post received some excellent responses, responses that made me look at the problem from different view points. I work out of my home and have no other programmers to bounce around ideas. So this and other forums have been a great help, especially for an old programmer trying to learn a new platform and language.

 

FYI:

I have been programming for 37 years (I was a Vice-President of IT 28 years ago, and didn't like management) and to me it is still just a way of making a living, no romance in the occupation at all.

Albiet, it has been a lucrative occupation and has educated my kids. Currently, the only programming I do is minor support for systems I wrote in the early 80's that are still leased. I didn't write a line of code or for that matter work at any occupation for the years 1998 - 2002 (lease income is sufficient). I have one client (has leased my software for over 20 years) that wanted me to convert those systems to the Windows platform. I started in January of this year. I am about 40% done, and when I work on these systems I am serious (I want to have it done). I am coding this system to work with Oracle 8i (no JOIN keyword among other difficulties), Oracle 9i and MS SQL 2000 at their request. But this project is a minor part of my life. You can be sure that about 70% of my time (awake hours) is goofing off, playing golf, drinking beer with my pals, etc. In fact, my pals and my wife will get a big laugh when they read your comment about my being serious 24 hours a day. They know that is about as far from the truth as you can get, and they would say that it would be very difficult for me to look at things a little bit more on the light-hearted side.

 

I will try to be more understanding in the future.

  • *Experts*
Posted
Humour is a great thing, but it shouldn't direct a thread's focus off course. I think it's best to (in the programming related forums) make sure your post has some technical merit, though, and not simply make joke posts.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...