SendMessage mouse click to treeview node

R

R.A.F.

Hi,

I would like simulate a mouse click on on of my treeview nodes when my
form opens.

for that i was thinking to use the same simple way as under C++ :
SendMessage(TreeView.Nodes[0].Handle, WM_LBUTTONDOWN,0,0);

but it does not work. i do not get any error message and application
runs normally... just my first node is not selected (clicked).

i want to do that because like that, another childform will be selected
based on my treeview node clicked.

thanks a lot,

RAF
 
N

Nicholas Paldino [.NET/C# MVP]

RAF,

SendMessage is not the way to simulate a mouse click. It only sends the
message to the control. A mouse click involves a hardware interrupt,
followed by the OS sending a number of messages to the target window, not
just one button down message.

In order to send a click to the tree view, you should find the position
of the node you want to send the mouse click to, and then call the SendInput
API function through the P/Invoke layer.
 
K

Kerem Gümrükcü

In order to send a click to the tree view, you should find the position of
the node you want to send the mouse click to, and then call the SendInput
API function through the P/Invoke layer.

Yes, i agree,...would be the best solution for this!

Regards

Kerem

--
 
R

R.A.F.

Sorry Nicholas but i did not find anything in VS2008 help about
SendInput API.

By the way, why do you not support the SendMessage method for such topic ?

I finally adapt it from C++ to C# and it works great :)

RAF
 
N

Nicholas Paldino [.NET/C# MVP]

RAF,

Here is a link to the documentation for SendInput:

http://msdn2.microsoft.com/en-us/library/ms646310.aspx

The reason I don't like calling SendMessage in this case is that you are
only sending the left mouse button down message. However, a true mouse
click consists of a click event, as well as a mouse down and a mouse up
event and possibly more. The control is also free to respond to any of
these messages as it sees fit.

It just so happens that sending the message is doing what you want, but
it isn't the same as sending a mouse click.
 
K

KWienhold

Hi,

I would like simulate a mouse click on on of my treeview nodes when my
form opens.

for that i was thinking to use the same simple way as under C++ :
SendMessage(TreeView.Nodes[0].Handle, WM_LBUTTONDOWN,0,0);

but it does not work. i do not get any error message and application
runs normally... just my first node is not selected (clicked).

i want to do that because like that, another childform will be selected
based on my treeview node clicked.

thanks a lot,

RAF

Maybe I'm missing something here, but if you want to make sure that
that a specific node is selected when your form opens, why not simply
set the SelectedNode property of the tree?
Since the most common way to use a tree for navigation is to handle
the AfterSelect event, this should result in the same behavior as
actually clicking on the node.

hth,
Kevin Wienhold
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top