tags:

views:

404

answers:

3

Hello

i have a form, which hosts a tabcontrol. each of these tabs have plenty of controls ranging from textbox, combobox, treecontrol, spin controls.

On top of the form there is a textbox.

if there are any changes made using the controls of the forms say a value is changed in the combobox of tab 1 or a item was deleted from the tree control i need to show a "*" indicating that certain values have been changed.

How can i acheive this in an efficient way? or is handling the resp. controls changed event the only way to know whether an item is changed or not ?

A: 

This depends entirely on the underlying architecture of your software. If it was written in a naive fashion, then yes, some form of brute force is just about the only way you can go (and that, of course, will just make the code that much worse-- this is why good architecture is important).

If, on the other hand, the software has been reasonably well designed, then you'll have objects behind the user interface that track the UI state. These objects may implement something like INotifyPropertyChanged and you can leverage that with a mapping mechanism to update the UI for changed fields.

I suspect you fall in the first situation, though, where any good answers to your problem were eliminated long before what you're trying to do now. I'm inferring this from your statement that the form "hosts a tabcontrol" and that "each of these tabs have[sic] plenty of controls..." This is a UI antipattern that I've seen time and time again from poor designers.

Greg D
Why do you describe the tab control containing a number of other controls as an antipattern? Design-wise, how would you refactor that?
Neil Whitaker
@Neil: UserControl(s). I've seen tons of terrible software that slaps (literally) dozens of controls directly on tabs, basically making `Form1` (it's almost inevitably named `Form1`) a single global container for all state w/o any sense of abstraction. Members in Form1 are used as Global state (freq. even prefixed with `glb`). I can think of one specific piece of software that my company sells for >$10k/license that's abused partial classes to spread Form1 over 11 files with controls like `textBox74`. Yes, there are more than 74 textboxes.
Greg D
The one I worked with was called `frmMain`. They left `Form1` in the project though, and `Form1`'s sole purpose was to display `frmMain` and then close itself.
md5sum
@md5sum: I'm cracking up! One of ours has a `frmMain` too! It popped up a splash screen then slept for 5 secs before getting to its own `Application.Run()`. (It did a lot of other bad stuff before getting to its message pump, too, but that's for another day.)
Greg D
-1 imho a rather 'amazing' non-answer. How is this helpful to the OP ? You jump from the very vague information in the question to a "sermon" on anti-patterns, and reveries about your previous experiences with bad code; then, you take us through how bad your company's code is :) Isn't the point of SO to help people at all levels ?
BillW
@BillW: The question is too underspecified to give a specific answer. Because it's a general question, then, I gave a general answer. The justification for why I reached the conclusion I reached comes from experience on StackOverflow where such inferences are naturally challenged-- explaining the source of my inference avoids more smoke than it causes. (And comments aren't part of an answer, regardless of the situation. :) )
Greg D
@GregD Nonsense, all you did was take a dump on the OP, and imply his situation is hopeless. You completely ignored the specifics of his question.
BillW
I have to agree, this is a non-answer. Q: What's the fastest route to drive from LA to NY A: It's faster to fly than it is to drive, a mistake commonly made by inexperienced travelers.
BarrettJ
in agreement with BillW and BarrettJ
dboarman
+2  A: 

I think this article on Codeproject might help you, it aids in the tracking of changes in the winforms controls.

Hope this helps, Best regards, Tom.

tommieb75
Nice article!!!!!
gbianchi
+1  A: 

You can try something along these lines, you'll need to add a bit for each type of control on your form. On your from's load event do a addUpdateNotification(this);

    public void addUpdateNotification(Control start)
    {
        foreach (Control c in start.Controls)
        {
            if (c is TextBox)
            {
                var text = c as TextBox;
                text.TextChanged += notifyChanged;
            }

            addUpdateNotification(c);
        }
    }

    public void notifyChanged(Object sender, EventArgs args)
    {
        UpdateTextBox.Text = "*";
    }
BarrettJ
@BillW are you sure? It's going through all the children of every control starting with the start control. Infinite recursion would only happen if a child control had one of it's parents as a child and the code in that article looks like it would choke on the same situation (which I'm not even sure if that situation is possible).
BarrettJ
@Tester101 fixed, should have been used on the next line instead of c.TextChanged
BarrettJ
@BarretJ My apologies, your code does not produce recursion : I violated my own policy of not commenting on code I had not personally tested : that will teach me !
BillW