开发者

track changes in winform C#

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 ?


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

Hope this helps.


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.


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 = "*";
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜