Sign in to follow this  
Followers 0
ssyork

Treeview and selecting data with MSGBOX

2 posts in this topic

Hi all

I am trying to create a tree view from data from SQL, which gets the data from the SQL server in to a tree form and then I am trying to get a message box to appear when selected the object in treeview, this is where I am at the moment , I just can't figure out how to get a msg box up when selecting the item in the treeview - any help. Thank you in advance

$objAAAcountID = "3"
$server = "SQL003"
$db = "AutoAdmin"
$username = "AutoAdmin"
$password = "Password1"

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>
#include <ie.au3>


$Form1 = GUICreate("Form1", 633, 453, 261, 171)
$TreeView1 = GUICtrlCreateTreeView(56, 32, 537, 361)


_usermessage()

GUICtrlSetOnEvent($TreeView1, "_Show")

GUISetState(@SW_SHOW)

#EndRegion ### END Koda GUI section ###

While 1
    
    $nMsg = GUIGetMsg()
    Switch $nMsg
        
        
        Case $GUI_EVENT_CLOSE
            
                                    
    Exit
    EndSwitch
WEnd

Func _usermessage()
;Set Varients
$sqlCon = ObjCreate("ADODB.Connection")
$oMyError = ObjEvent("AutoIt.Error", "ComError")
$oMyError = ""

$sqlCon = ObjCreate("ADODB.Connection")
$sqlCon.Open("DRIVER={SQL Server};SERVER=" & $server & ";DATABASE=" & $db & ";uid=" & $username & ";pwd=" & $password & ";")
$rsAccounts = $sqlCon.Execute("SELECT * FROM table_UserMessages")

With $rsAccounts
    While Not .EOF
        $MessageUserID = .Fields("UserID" ).value
        $MessageStatus = .Fields ("MessageStatus").value
        If $MessageUserID = $objAAAcountID Then 
            if $MessageStatus = "Active" Then
                $x = .Fields ("MessageID").value
                $x = GUICtrlCreateTreeViewItem(.Fields ("MessageHeader").value, $TreeView1)
             
        
            Endif

        Endif
        .MoveNext
    WEnd
EndWith

$sqlCon.Close

EndFunc





Func _Show()
ConsoleWrite ("got you!")
MsgBox(0,"",GUICtrlRead(@GUI_CtrlId,1))
EndFunc

Share this post


Link to post
Share on other sites



To instruct AutoIt to switch to event mode you need to specify that before attaching any event handlers. For this task I don't think you need event mode at all.

#include <GUIConstantsEx.au3>
#include <StructureConstants.au3>
#include <TreeViewConstants.au3>
#include <WindowsConstants.au3>

GUIRegisterMsg($WM_NOTIFY, '_WM_NOTIFY')

Global $hGUI = GUICreate('Test', 400, 400)
Global $TreeView = GUICtrlCreateTreeView(0, 0, 400, 400)
GUICtrlSetBkColor($TreeView, 0x4080DD)
GUICtrlSetColor($TreeView, 0xEEEE90)

For $i = 1 To 10
    Local $ParentItem = GUICtrlCreateTreeViewItem('Parent ' & $i, $TreeView)
    
    For $j = 1 To 20
        GUICtrlCreateTreeViewItem('Child ' & $i, $ParentItem)
    Next
Next

GUISetState()

Do
Until GUIGetMsg() = $GUI_EVENT_CLOSE

GUIDelete()
Exit

Func _WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $tNMHdr   = DllStructCreate($tagNMHDR, $ilParam), $tNM_TREEVIEW
    Local $hWndFrom = DllStructGetData($tNMHdr, 'hWndFrom')
    Local $iIDFrom  = DllStructGetData($tNMHdr, 'IDFrom')
    Local $iCode    = DllStructGetData($tNMHdr, 'Code')

    
    If $iIDFrom = $TreeView Then
        Switch $iCode
            Case $TVN_SELCHANGED, $TVN_SELCHANGEDW
                If GUICtrlRead($TreeView) > 0 Then _
                    MsgBox(0x40, 'Item Clicked', 'ID: ' & GUICtrlRead($TreeView) & @CRLF & 'Text: ' & GUICtrlRead($TreeView, 1))
        EndSwitch
    EndIf
    
    Return $GUI_RUNDEFMSG
EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0