Jump to content

How to check which TreeViewItem's are checked?


VioFX
 Share

Recommended Posts

So I've just started tonight with AutoIt, trying to create something simple for myself.

Can't understand how to check if a treeviewitem is checked and if it is checked, add results to my listview in accordance to which treeviewitem's are checked.

Here's what I have right now:

#include <GuiConstantsEx.au3>
#include <AVIConstants.au3>
#include <TreeViewConstants.au3>

; GUI
GuiCreate("Sample", 800, 400)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

GuiCtrlCreateLabel("Keyword:", 5, 100)
GuiCtrlCreateInput("", 53, 97, 130, 20)

$button1 = GuiCtrlCreateButton("Add", 10, 340, 100, 30)
$listView = GuiCtrlCreateListView("State                 |Available|", 505, 117, 270, 240)


; TAB
GuiCtrlCreateTab(205, 97, 290, 150)
GuiCtrlCreateTabItem("State")
$caliTree = GuiCtrlCreateTreeView(213, 125, 150, 100, $TVS_CHECKBOXES)
$california = GuiCtrlCreateTreeViewItem("California", $caliTree)
GuiCtrlCreateTreeViewItem("Florida", $caliTree)
GuiCtrlCreateTreeViewItem("New York", $caliTree)
GuiCtrlCreateTreeViewItem("Washington", $caliTree)
GuiCtrlCreateTabItem("City")
GuiCtrlCreateTabItem("Area Code")
GuiCtrlCreateTabItem("")


GUISetState()
While 1
    $nM = GUIGetMsg()
    Switch $nM
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button1
            If GUICtrlRead($california) = $GUI_CHECKED Then
                GUICtrlCreateListViewItem("California "&"|"&" Yes ", $listView)
            Else
                GUICtrlCreateListViewItem("California "&"|"&" No ", $listView)
            EndIf
    EndSwitch
WEnd


; GUI MESSAGE LOOP
GuiSetState()
While GuiGetMsg() <> $GUI_EVENT_CLOSE
WEnd

Probably really sloppy, but it's my first night anyway.

So any help on this?

It functions the first time I click "Add", but afterwards, if I uncheck the box it still add's the same results it did whether it's checked or unchecked.

Link to comment
Share on other sites

You mean something like this:

#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>

; GUI
GuiCreate("Sample", 800, 400)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

GuiCtrlCreateLabel("Keyword:", 5, 100)
GuiCtrlCreateInput("", 53, 97, 130, 20)

$button1 = GuiCtrlCreateButton("Add", 10, 340, 100, 30)
$listView = GuiCtrlCreateListView("State                 |Available|", 505, 117, 270, 240)


; TAB
GuiCtrlCreateTab(205, 97, 290, 150)
GuiCtrlCreateTabItem("State")
$caliTree = GuiCtrlCreateTreeView(213, 125, 150, 100, $TVS_CHECKBOXES)
$california = GuiCtrlCreateTreeViewItem("California", $caliTree)
GuiCtrlCreateTreeViewItem("Florida", $caliTree)
GuiCtrlCreateTreeViewItem("New York", $caliTree)
GuiCtrlCreateTreeViewItem("Washington", $caliTree)
GuiCtrlCreateTabItem("City")
GuiCtrlCreateTabItem("Area Code")
GuiCtrlCreateTabItem("")


GUISetState()
While 1
    $nM = GUIGetMsg()
    Switch $nM
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button1
            $index = _GUICtrlTreeView_GetSelection($caliTree)
            GUICtrlCreateListViewItem(_GUICtrlTreeView_GetText($caliTree, $index) & "|" & _GUICtrlTreeView_GetChecked ($caliTree, $index),  $listView)
;~             If GUICtrlRead($california) = $GUI_CHECKED Then
;~                 GUICtrlCreateListViewItem("California "&"|"&" Yes ", $listView)
;~             Else
;~                 GUICtrlCreateListViewItem("California "&"|"&" No ", $listView)
;~             EndIf
    EndSwitch
WEnd


; GUI MESSAGE LOOP
GuiSetState()
While GuiGetMsg() <> $GUI_EVENT_CLOSE
WEnd

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Yes thank you, this is definitely on the right track.

But how do I do a check for all boxes checked and post results accordingly. I don't want it to say False or No if the box isn't checked, just want it to say nothing at all.

When no box is checked, it comes up with California False, which if nothing is checked it shouldn't say anything.

EDIT:

Actually just noticed it comes up with (City Name) False, according to the last box that was checked.

Edited by VioFX
Link to comment
Share on other sites

Try this then:

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GuiTreeView.au3>
#include <TreeViewConstants.au3>

; GUI
GuiCreate("Sample", 800, 400)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

GuiCtrlCreateLabel("Keyword:", 5, 100)
$input = GuiCtrlCreateInput("", 53, 97, 130, 20)

$button1 = GuiCtrlCreateButton("Add", 10, 340, 100, 30)
$listView = GuiCtrlCreateListView("State                 |Available|", 505, 117, 270, 240)


; TAB
GuiCtrlCreateTab(205, 97, 290, 150)
GuiCtrlCreateTabItem("State")
$caliTree = GuiCtrlCreateTreeView(213, 125, 150, 100, $TVS_CHECKBOXES)
$california = GuiCtrlCreateTreeViewItem("California", $caliTree)
GuiCtrlCreateTreeViewItem("Florida", $caliTree)
GuiCtrlCreateTreeViewItem("New York", $caliTree)
GuiCtrlCreateTreeViewItem("Washington", $caliTree)
GuiCtrlCreateTabItem("City")
GuiCtrlCreateTabItem("Area Code")
GuiCtrlCreateTabItem("")


GUISetState()
While 1
    $nM = GUIGetMsg()
    Switch $nM
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button1
            _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($listView))
            $hItem = _GUICtrlTreeView_GetFirstItem($caliTree)
            Do
                $status = _GUICtrlTreeView_GetChecked ($caliTree, $hItem)
                If $status Then
                    $status = "yes"
                    GUICtrlCreateListViewItem(GUICtrlRead($input) & _GUICtrlTreeView_GetText($caliTree, $hItem) & "|" & $status,  $listView)
                EndIf
                $hItem = _GUICtrlTreeView_GetNextSibling($caliTree, $hItem)
            Until Not $hItem
    EndSwitch
WEnd


; GUI MESSAGE LOOP
GuiSetState()
While GuiGetMsg() <> $GUI_EVENT_CLOSE
WEnd

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks so much for the help UEZ, this is definitely in the right direction.

However all boxes whether checked or unchecked are getting added to the treeview, any way to ONLY add a treeviewitem for the boxes that are checked?

Also what's the function that I would use to wipe all treeviewitem's? So I can have a button that clears the tree

Thanks again so much for the help, it's really helping me understand how it all works.

Link to comment
Share on other sites

Okay looks like I figured out the function to clear the listview, I used this:

$clearmenu = GuiCtrlCreateButton("Clear All", 140, 340, 100, 30)

$nMe = GUIGetMsg()
    Switch $nMe
        Case $GUI_EVENT_CLOSE
            Exit
        Case $clearmenu
            _GUICtrlListView_DeleteAllItems($listView)
    EndSwitch

Might not be the most efficient, but it seems to work fine.

Still trying to figure out how to only add the ones checked.

Also having multiple switch's inside my GUISetState() seems to be a bit buggy. When I run the script and press the 2 different buttons, it like lags or something, have to click the buttons a few times for it to react. Maybe there's something messed up in my code not too sure.

Here's what I have:

#include <GUIConstantsEx.au3>
#include <GuiTreeView.au3>
#include <GuiListView.au3>
#include <TreeViewConstants.au3>

; GUI
GuiCreate("Test", 800, 400)
GuiSetIcon(@SystemDir & "\mspaint.exe", 0)

GuiCtrlCreateLabel("Keyword:", 5, 100)
$keywordinput = GuiCtrlCreateInput("", 53, 97, 130, 20)

$button1 = GuiCtrlCreateButton("Add", 10, 340, 100, 30)
$clearmenu = GuiCtrlCreateButton("Clear All", 140, 340, 100, 30)
$listView = GuiCtrlCreateListView("City                 |Available|", 505, 117, 270, 240)





; TAB
GuiCtrlCreateTab(205, 97, 290, 150)
GuiCtrlCreateTabItem("By State")
$caliTree = GuiCtrlCreateTreeView(213, 125, 150, 100, $TVS_CHECKBOXES)
GuiCtrlCreateTreeViewItem("California", $caliTree)
GuiCtrlCreateTreeViewItem("Florida", $caliTree)
GuiCtrlCreateTreeViewItem("New York", $caliTree)
GuiCtrlCreateTreeViewItem("Washington", $caliTree)
GuiCtrlCreateTabItem("By City")
GuiCtrlCreateTabItem("By ????")
GuiCtrlCreateTabItem("")




GUISetState()
While 1
    $nM = GUIGetMsg()
    Switch $nM
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button1
            _GUICtrlListView_DeleteAllItems(GUICtrlGetHandle($listView))
            $hItem = _GUICtrlTreeView_GetFirstItem($caliTree)
            Do
                $status = _GUICtrlTreeView_GetChecked ($caliTree, $hItem)
                If Not $status Then
                    $status  = ""
                Else
                    $status = "yes"
                EndIf
                GUICtrlCreateListViewItem(_GUICtrlTreeView_GetText($caliTree, $hItem) & "|" & $status,  $listView)
                 $hItem = _GUICtrlTreeView_GetNextSibling($caliTree, $hItem)
            Until Not $hItem
        EndSwitch
        $nMe = GUIGetMsg()
    Switch $nMe
        Case $GUI_EVENT_CLOSE
            Exit
        Case $clearmenu
            _GUICtrlListView_DeleteAllItems($listView)
    EndSwitch
WEnd


; GUI MESSAGE LOOP
GuiSetState()
While GuiGetMsg() <> $GUI_EVENT_CLOSE
WEnd

I have a strong feeling that I'm going about my GUISetState() functions all wrong, doesn't look right but gets the job done. Perhaps anyone knows how to use multiple GUISetState() so I can have a different function/case inside each? Or just how multiple ones are usually written.

Edited by VioFX
Link to comment
Share on other sites

I update the code from post#4 again.

To clear the treeview use

_GUICtrlTreeView_DeleteAll($caliTree)

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thanks a ton! That did the trick, now just to change it up a bit to fit what I'm trying to do here in the long run.

Also I found out the issue to my problem before, found out all I needed to do was add a new case in my GUISetState()

Another quick question though. Alright so when the box is checked it's putting out the correct results. However for the first column in my ListView when I click add it's putting say "California" in the first column, how would I change it so it say put the contents of the Keyword input box + california so the first column displays "keywordcalifornia", instead of the text of the checkbox.

Link to comment
Share on other sites

Awesome that worked wonderfully, integrated it into my code right now and it's going good. Seems I ran into another problem though, okay so here's what I'm trying to do here, the keyword box I want to be the name of a website. So say I put "Google" in the input box, then click add.

I want it to download index.htm into temp directory to check if the website is online, if it downloads then displays yes, if it's unable to download index.htm then returns no.

So here's what I have:

$tempcheck = GuiCtrlCreateButton("Temp Check", 250, 340, 100, 30)

Case $tempcheck
        If InetGet($input & ".com",@TempDir&'\index.htm') Then
            GUICtrlCreateListViewItem(GUICtrlRead($input) & ".com" & "|" & "Yes",  $listView)
        Else
            GUICtrlCreateListViewItem(GUICtrlRead($input) & ".com" & "|" & "No",  $listView)
        EndIf

But it seems InetGet isn't checking the address correctly, do you know how I would combine the contents of the keyword box with ".com" and it use that address for the URL in InetGet? It's displaying the correct thing (input box + .com) in the listview results, however isn't using that combination as the URL for InetGet.

Edit:

I tried this:

If InetGet(GUICtrlRead($input) & ".com",@TempDir&'\index.htm') Then

Instead, however it's the same result. Trying pretty hard to figure this out on my own, just drawing blanks here.

EDIT #2:

Or if you know of a better way to check if a website is online/offline and return the answer that would be awesome, basically I want to do these checks to see if a domain is available.

EDIT #3:

I may have had a breakthrough with it, went a completely different route and it seems to be working fairly well. I'll post back again if I run into some more issues. Thanks so much for the help.

Edited by VioFX
Link to comment
Share on other sites

Edit: Scratch what my post just said here, I figured out what was wrong however have a new problem here that I've been working on for the past hour.

I'm using the different treeview now and am trying to check which is selected and to act accordingly. So I'm trying to figure out how to act according to which item in the treeview is selected.

Hopefully I can get this one figured out, seems to have me at a standstill at the moment.

So this is what my tab looks like:

GuiCtrlCreateTab(205, 97, 290, 150)
GuiCtrlCreateTabItem("By State")
$mainTree = GuiCtrlCreateTreeView(213, 125, 150, 100)
$treeItem = GuiCtrlCreateTreeViewItem("California", $mainTree)
GuiCtrlCreateTreeViewItem("Florida", $mainTree)
GuiCtrlCreateTreeViewItem("New York", $mainTree)
GuiCtrlCreateTreeViewItem("Washington", $mainTree)
GuiCtrlCreateTreeViewItem("Expand", -1)
GuiCtrlSetState($treeItem, $GUI_EXPAND)
GuiCtrlCreateTabItem("By City")
GuiCtrlCreateTabItem("By ????")
GuiCtrlCreateTabItem("")

If I had a button:

$testTxt = GuiCtrlCreateButton("Test Txt", 370, 340, 100, 30)

That is supposed to open a certain text document, how would I get it to say open california.txt when "California" in the treeview is selected.

Here's what I'm using to open the text document:

Case $testTxt
            
            $file = FileOpen("california.txt", 0)
            If $file = -1 Then
            MsgBox(0, "Error", "Could not open text document.")
            Exit
        EndIf
        
        MsgBox(0, "Test", "Success!")

Problem here is when the button is pressed it will always open california.txt, no matter what is selected.

What would I do here to open florida.txt and act accordingly if "Florida" was selected in the treeview, or open newyork.txt if "New York" was selected in the treeview, so on and so forth.

Edit #1:

So I tested this to see if it would just display the name of the treeviewitem selected:

Case $testSelect
        $tSelect = _GUICtrlTreeView_GetSelection($mainTree)
        MsgBox(0, "Selection: ", $tSelect)

Whenever I click the button it puts up the address like 0x0FF916 instead of the name of the treeviewitem. How would I change this so instead if gets the text of the one selected

Edited by VioFX
Link to comment
Share on other sites

Whenever I click the button it puts up the address like 0x0FF916 instead of the name of the treeviewitem. How would I change this so instead if gets the text of the one selected

Of course. The helpfile clearly states that it returns the handle. Just use _GUICtrlTreeView_GetText() to get it's text! :unsure:
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...