Problem when creating custom control
Hello everyone. This is my first post here. I just joined today.
I created a custom windows control with a Tree View inside of it. On the load event of the control, I fill it with categories structured as a tree from a MsSql database. I created also a OnClick event for the control, and pass it to the OnClick event of the Tree View. Then I use the control in a web form like a directory structure, so when the user clicks on one of the nodes of the Tree View, it gets data related to the node selected and then it displays the data somewhere else on the form. The problem is that when I use the Click event and check the selected node (TreeView.SelectedNode), it is selected AFTER the click event, so when I first click, SelectedNode is equal to null, when I click again on other node, the selected node property is the node I clicked last time and so on.
How can I check the selected node?
Maybe you must to implement a custom property or public variable (i.e LastSelectedNode). In this property you stored the node index/key that the user clicked, after you assing this value to the Selectednode property. The first time, SelectedNode and LastSelectedNode are equals to null/nothing, you must to assign the first node to both properties.
I think I identified the problem (not the solution, yet). The event that I'm using is the treeView_onClick. This event fires when the following events occur:
- The user clicks on a node
- The user clicks again o that node (not double click)
- After the second click, the onClick event is fired.
I traced the AfterSelect and BeforeSelect and they are fired twice each when the user clicks a node, first with the current node (before and after) and then with the node selected by the user (before and after, also). I'm kinda lost here...
Hai androKLez
This is why Because the order in which events get fired.
First when you click "Click" event gets fired and after that "beforeselect" and after that "afterselect" will get fired.
so the selected node will be assigned only when it is get selected which happens when the event "afterselect" is fired. upto that the previous selected only will be the SelectedNode.
so use afterselect to complete your task.
private
void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
MessageBox.Show(e.Node.Text.ToString()); }