mercredi 5 octobre 2016

c# WindowsForms: Treeview set selectednode when retrieving focus

Vote count: 0

I'm having some trouble when dealing with a BeforeSelect event AND a pop-up when no treenode is selected in my treeview.

Basically, I am trying to restrict the use of some treenodes depending on the context. Since treenodes do not have "Enable" flags, I do it with the well-known BeforeSelectEvent:

private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
    if(! isEnabled( e.Node ) ) e.Cancel = true;
}

So far, so good, it works like a charm. But of course, I got complains that "this is not working anymore" and a pop-up would be needed to let the end user know this is a PEBKAC scenario. Easy enough since the application containing my UI has a logging system allowing it.

private void treeView1_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
    if( isEnabled( e.Node ) ) return;
    e.Cancel = true;
    parentApplication.LoggingSystem.ErrorPopup("You are an Idiot");
}

The user is then prompted to hit "OK", which gives the focus away from my UI, then closes the pop-up... and gives back focus on my UI (especially my treeview). Nice enough. And it works fine. Except if the canceled node is the first node the user triggered. In which case, when the TreeView retrieves its focus, it changes the selectedNode from null to TreeView.Nodes[0]. Which in turns triggers a lot of events because of course my Nodes[0] has to do server calls against THE server containing THE big database.

Right now the only solutions I've explored look like ducktaping: - now showing up the popup in this specific case works but this is not a correct behavior - forcing the focus to another element of the UI before showing the popup somehow fails (focus comes back at a point?) - setting a flag before showing up the pop-up so that the "afterselect" can abort somehow fails too (looks like focus is given back when leaving beforeselect() as well ?)

Do you have a clean way to show the popup without triggering the focus() ?

asked 26 secs ago

Let's block ads! (Why?)



c# WindowsForms: Treeview set selectednode when retrieving focus

Aucun commentaire:

Enregistrer un commentaire