RSS FEED

Simple to-do list

Following the question asked in ScriptSpot Forums :: Working checklist I decided to make the code snippet usable in real life and made a basic to-do list with automatic saving and loading of the tasks (saves a different file for each user), resizable interface and... well, no other distractions at all. As usual, no .mse, just plain text for you to have a look at and reuse:
try destroyDialog listview_rollout catch()
rollout listview_rollout " Task List v0.02" width:190 height:300
(
    ---------------------------------------------------------------------------------
    -- Layout Section
    ---------------------------------------------------------------------------------

    editText etTaskName "" width:75 pos:[6,8]
    button btnAdd "Add" pos:[85,7]
    button btnRemove "Remove" pos:[121,7]
    dotNetControl dncTaskList "System.Windows.Forms.ListView" pos:[3,37] width:184 height:260

    ---------------------------------------------------------------------------------
    -- Private Globals
    ---------------------------------------------------------------------------------

    local dn_listitem = dotNetClass "System.Windows.Forms.ListViewItem"
    local dn_buttons = dotNetClass "System.Windows.Forms.MouseButtons"
    local dn_file = dotNetClass "System.IO.File"
    local dn_streamwriter = dotNetClass "System.IO.StreamWriter"
    local dn_streamreader = dotNetClass "System.IO.StreamReader"
    local dn_garbage = dotNetClass "System.GC"
    local file_name = ((dotNetClass "System.Windows.Forms.Application").LocalUserAppDataPath + "\\to-do.list")
    local file_stream = undefined
    local check_states, sel_nums = #(), sel_nums_count = 0 -- for the listview checkbox workaround all the credits go to Jon Huhn
                                                           -- for more details see forums.cgsociety.org/showthread.php?t=598014
    ---------------------------------------------------------------------------------
    -- Event Handlers
    ---------------------------------------------------------------------------------

    on listview_rollout open do
    (
        dncTaskList.View = (dotNetClass "System.Windows.Forms.View").Details
            dncTaskList.BorderStyle = dncTaskList.BorderStyle.FixedSingle
            dncTaskList.FullRowSelect = true
            dncTaskList.GridLines = true
            dncTaskList.CheckBoxes = true
            colums_arr = #(#(" ", 18), #("Task", 166))
            for i in colums_arr do (dncTaskList.Columns.Add i[1] i[2])

        if doesFileExist file_name do
        (
            local read_line, list_items = #()
            local new_item = dotNetObject dn_listitem ""
                new_item.SubItems.Add ""

            file_stream = dotNetObject dn_streamreader file_name
            while (read_line = file_stream.ReadLine()) != undefined do

            (
                local line_item = filterString read_line "\t"
                    list_item = new_item.Clone()
                    list_item.Checked = line_item[1] as booleanClass    
                    list_item.SubItems.Item[1].Text = line_item[2]

                append list_items list_item
            )

            dncTaskList.Items.AddRange list_items            
            list_items = undefined

            file_stream.Close()
            file_stream.Dispose()
            file_stream = undefined
            dn_garbage.collect()
            gc light:true
        )
    )

    on btnAdd pressed do
    (
        if etTaskName.text != "" do
        (
            local new_item = dotNetObject dn_listitem ""
                new_item.SubItems.Add etTaskName.text
                new_item.Checked = false

            dncTaskList.Items.Add new_item
            new_item = undefined
        )
    )

    on btnRemove pressed do
    (
        selected_count = dncTaskList.SelectedItems.Count - 1
        for index = selected_count to 0 by -1 do
            dncTaskList.Items.Remove dncTaskList.SelectedItems.Item[index]
    )

    on dncTaskList ItemSelectionChanged do
    (
        local selected_count = dncTaskList.selectedIndices.count - 1

        sel_nums = for each = 0 to selected_count collect
            dncTaskList.SelectedIndices.Item[each]

        sel_nums_count = sel_nums.count
        check_states = for each = 1 to sel_nums_count collect
            dncTaskList.Items.Item[sel_nums[each]].Checked    
    )

    on dncTaskList MouseUp ctrl evnt do
    (
        if evnt.Button == dn_buttons.Left do
        (
            for item = 1 to sel_nums.count do
                dncTaskList.Items.Item[sel_nums[item]].Checked = check_states[item]

            check_states = #()
            sel_nums = #()
        )
    )

    on dncTaskList ColumnClick ctrl evnt do
    (
        if evnt.Column == 0 do
        (
            local checked_arr = #()
            local unchecked_arr = #()
            
            for i = 0 to (listview_rollout.dncTaskList.Items.count - 1) do
            (
                case listview_rollout.dncTaskList.Items.Item[i].Checked of
                (
                    true : append checked_arr listview_rollout.dncTaskList.Items.Item[i]
                    false : append unchecked_arr listview_rollout.dncTaskList.Items.Item[i]
                )
            )
            
            dncTaskList.BeginUpdate()
            dncTaskList.Items.Clear()
            dncTaskList.Items.AddRange (join checked_arr unchecked_arr)
            dncTaskList.EndUpdate()
        )
    )

    on listview_rollout resized rollout_size do
    (
        if rollout_size[1] <= 190 do
            listview_rollout.width = 190 --limit dialog resizing

        if rollout_size[2] <= 150 do
            listview_rollout.height = 150

        dncTaskList.Size = dotNetObject "System.Drawing.Size" (listview_rollout.width - 6) (listview_rollout.height - 40)
        dncTaskList.Columns.Item[1].Width = (listview_rollout.width - 24) 
    )

    on listview_rollout close do
    (
        file_stream = dotNetObject dn_streamwriter (dn_file.Create file_name)
        item_count = listview_rollout.dncTaskList.Items.Count - 1

        for index = 0 to item_count do
            file_stream.WriteLine ((local list_item = listview_rollout.dncTaskList.Items.Item[index]).Checked as string + "\t" + list_item.SubItems.Item[1].Text)

        file_stream.Close()
        file_stream.Dispose()
        file_stream = undefined
    )
)
createDialog listview_rollout style:#(#style_titlebar, #style_sysmenu, #style_minimizebox, #style_resizing)
USAGE: Run the script, add items, check/uncheck them, remove them – there's not much else to do with it.

To-do list interface

DISCLAIMER: All scripts and snippets are provided as is under Creative Commons Zero (public domain, no restrictions) license. The author and this blog cannot be held liable for any loss caused as a result of inaccuracy or error within these web pages. Use at your own risk.

This Post needs Your Comment!

Anonymous

Great work man. Thanks for the script.
JokerMartini

patriculus

nice script...

Return to top