treeview Keydown, keypress, keyup events

Cecilia

Member
Joined
Aug 14, 2007
Messages
24
Programming Experience
Beginner
MyForm has a treeview control, I want this treeview to be readonly. But if the user click on one of the form's context menu item, I also want to allow user to use their mouse or (shift+arrow key) to select the text in a node, copy them and paste to other document.

The problem is when user click on a node and choose to copy, he could select and copy, but he could also type into the node. Although I do something in AfterLabelEdit event to cancel all the changes that made to this node, it is kind of misleading. I want to prevent all keyboard input except Shift+arrow, which is for selecting. But I simple couldn't find a way to do it.

I tried AddHandler for keydown, keypress, keyup events, but still catch nothing.

Help! And thanks!
 
Wouldn't it be more convenient to just copy all of node text to Clipboard with a context menu option?
If not you can always enable editing and set e.CancelEdit in AfterLabelEdit event handler (perhaps this was what you indicated?). One can select/edit using only mouse also, you know.
 
On second thought, there is an easy way to set the editing control in readonly mode using Win32 messages TVM_GETEDITCONTROL and EM_SETREADONLY like this:
VB.NET:
Expand Collapse Copy
Const TV_FIRST As Int32 = &H1100
Const TVM_GETEDITCONTROL As Int32 = (TV_FIRST + 15)
Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" ( _
        ByVal hwnd As IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
Const EM_SETREADONLY As Int32 = &HCF
Const [TRUE] As Int32 = 1

Private Sub TreeView1_BeforeLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.BeforeLabelEdit
    Dim edt As Integer = SendMessage(Me.TreeView1.Handle, TVM_GETEDITCONTROL, 0, 0)
    If edt <> 0 Then
        SendMessage(New IntPtr(edt), EM_SETREADONLY, [TRUE], 0)
    End If        
End Sub
 
Hi JohnH,

Thanks for the replies. There are people who have difficulties using mouse, so I need to allow selecting text using keyboard.

I will try your second thought. Thanks a lot!

Pin
 
Back
Top