"Pre edit" treeview

kriswinner

Member
Joined
Apr 23, 2009
Messages
23
Programming Experience
10+
I have an application where I'm allowing a user to edit the text of a treeview.

If the text of node is 'sBK01 (1732DS-IB8 (8 In))'
I'd like the text in the "editable" state to start as 'sBK01'.

I've found a couple of references on how to do this with MsgHook, but I can't seem to find the required .dll to implement it.


Here's one reference to the MsgHook...
Binaryworld - Get full control on the text typed in a TreeView's node ... [ VB -> Controls-TreeView ]

If anyone has a solution (with or without MsgHook), it would be appreciated.

KW
 
I'd like the text in the "editable" state to start as 'sBK01'.
You only want nodes that start with "sBK01" to be editable? For BeforeLabelEdit event handler:
VB.NET:
e.CancelEdit = Not e.Node.Text.StartsWith("sBK01")
 
Ok...I didn't explain myself very well (...and I couldn't find the MsgHook reference that I was looking for before)

JohnH - the code you're proposing conditionally allows the user to edit the node text based on the text's content ("sBK01" in this case). This isn't what I'm after.

If the text is "sBK01 (1732DS-IB8 (8 In))", when the user tries to edit the text, I'd like the text that they are editing to become "sBK01"...and then allow the user to edit it from there.

Before posting, I did try the BeforeLabelEdit method to set the value before the user edits it.

VB.NET:
    Private Sub tree_Main_BeforeLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles tree_Main.BeforeLabelEdit

        e.CancelEdit = True   'Need to cancel the edit to be able to overwrite
        If InStr(e.Node.Text, "(") <> 0 Then e.Node.Text = Trim(Mid(e.Node.Text, 1, InStr(e.Node.Text, "(") - 1))

    End Sub

As you'd expect, if try to edit the node text with this code, it just drops everything after the "(", but it cancels the edit.


If I add an e.CancelEdit=False to the end of the routine....

VB.NET:
    Private Sub tree_Main_BeforeLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles tree_Main.BeforeLabelEdit

        e.CancelEdit = True   'Need to cancel the edit to be able to overwrite
        If InStr(e.Node.Text, "(") <> 0 Then e.Node.Text = Trim(Mid(e.Node.Text, 1, InStr(e.Node.Text, "(") - 1))
        e.CancelEdit = False

    End Sub

...the node text can be edited, but it never changes the text prior to editing.

Is there a way to modify the text but not cancel the edit?


Thanks. - KW
 
For that I think you have to resort to API messages for the edit control, for example:
VB.NET:
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 String) As IntPtr
Const WM_SETTEXT As Int32 = &HC
and in BeforeLabelEdit
VB.NET:
Dim ix = e.Node.Text.IndexOf("("c)
If ix <> -1 Then
    Dim editCtl = SendMessage(CType(sender, TreeView).Handle, TVM_GETEDITCONTROL, Nothing, Nothing)
    If editCtl <> IntPtr.Zero Then
        Dim editText = e.Node.Text.Remove(ix)
        SendMessage(editCtl, WM_SETTEXT, Nothing, editText)
    End If
End If
 
Back
Top