Brandon
Brandon

Reputation: 191

How to detect a User action compared to a Simulated action for a checkbox in a treeview?

I have a treeview with a list of items that are expandable. When the user checks the checkbox for a parent node, I simulate checking the children nodes. However, the user can check any node (not just parent nodes). I want to distinguish the user's first click, which triggers the node's checkbox event, from the simulated checking of the children node's checkboxes.

I want to distinguish the User's click so I can save the state of the treeview before it changes.

I thought of using a mousedown event, but this doesn't work since the user might click to expand something and I do not want to save the state of the treeview since nothing changed. One way to make this work would be to calculate the dimensions of each checkbox location and then check the mousedown's click coordinates, but I would like to avoid doing this as it would not be easy since I have nested nodes so the check boxes have a few different columns they could be.

Another way to think of this is I want to turn groups of events into actions. So one action would represent the User's first action, and all the simulated actions that followed until control is returned to the user.

Another thought, maybe I could do something with window focusing or control. For instance if the window is not focused while simulating, I could just save on the focus change if it would occur during a User's action of checking a tree node's checkbox.

Additional info

The simulated checkbox clicks are invoked by code such as:

e.Node.Checked = false;

This triggers the event: private void tree_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)

When a user clicks a node, this AfterCheck event triggers. I want to save the state of the tree here. But inside the AfterCheck event, there are cases that check other nodes causing the AfterCheck event to trigger again n times, but this time the check for the checkbox was simulated.

Systems.Windows.Forms.TreeView is the sender object in all cases.

Upvotes: 1

Views: 481

Answers (2)

Trisped
Trisped

Reputation: 6003

Have you tried the CheckBox.Click event?

This would make it easy to tell that the user was clicking to make the change.

You could also use a flag to indicate that any events raised would be because the method was making the change. Since this is a windows form, the user will not be able to check another box until your process has finished (so there is no raceway condition).

/// <summary>
/// Flag which can be accessed by the checkboxes which indicates if the program is making the change or the user is.
/// </summary>
bool programmaticallySettingChecks { get; set; }

protected void FlagExample()
{
    CheckBox[] chToCheck = new CheckBox[10];        //Collection of checkboxes to set the Checked property on.

    programmaticallySettingChecks = true;

    for (int i = 0; i < chToCheck.Length; i++)
    {
        chToCheck[0].Checked = true;
    }

    programmaticallySettingChecks = false;
}

protected void Check_Clicked(Object sender, EventArgs e)
{
    if (programmaticallySettingChecks)
    {
        //do things which are done if setting programmatically.
    }
    else
    {
        //Do things which are done if user sets.
    }

}

Upvotes: 1

StoriKnow
StoriKnow

Reputation: 5866

Can you provide us with some code that you've tried?

If the user will only click parent nodes and you will only select child nodes, then perhaps you can do a simple check during the event trigger that says "If parent node, then execute, otherwise return"

Upvotes: 0

Related Questions