Sign in to follow this  
Followers 0
BlackHoleSun

Selecting an item in a list

2 posts in this topic

I'm updating my ghosting program, and now I am working on a feature that lets you set defaults in the INI file. So if you set a default server and computer model it prompts you to continue and then kicks off ghost. I want to expand on this a little and let people set only a default server or computer model. Right now it's all or nothing. The problem I run into, is the way my code is setup now, is it that it looks to see that a server is selected and mapped and that a computer model is selected. The list for both of those are filled in with an array that's pulled from the INI (example: 1|Server Name|\\server\share). So how can I 1) determine if the server share is in the list of available servers and then select that server in the list after mapping it, 2) determine if the computer is in the list of available computer models, and select it, or add it to the list? The server has a custom option and that can be selected if it's not in the list. The servers should be matched based on the share and the computers are based on the file name (i.e. Model|ABC.gho). Below is my code as it stands now. Thanks for all the help.

;-----------------------------
;   Auto Ghosting Utility
;       Version 1.1.0
;   Developed by Luis Lugo
;-----------------------------

#include <Array.au3>
#include <Constants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

;Device items
Global $deviceGroup,$mapDrvButton, $serverGroup,$mapped
Global $ServerList, $DeviceList
Global $StatusMappedDrive
Global $strServerPath
Global $DriveMapLetter="J:"
Global $progressBar
Global $okbutton
Dim $arrDevice[10],$DeviceListItem[10]
Dim $arrServer[10],$ServerListItem[10]
Dim $arrDefault[3],$DefaultListItem[3]
Dim $arrUser[10], $UserName[10]

_Main()

Func _Main()
    ;Main items
    Local $filemenu, $exititem, $helpmenu, $aboutitem, $cancelbutton
    Local $msg, $file
    local $SelectedListViewDeviceItem, $SelectedListViewServerItem

    GUICreate("Auto Ghosting Utility", 410, 300, (@DesktopWidth - 410) / 2, (@DesktopHeight - 300) / 2, -1, $WS_EX_ACCEPTFILES)

    ;Create the list of servers
    $serverGroup = GUICtrlCreateGroup("Servers", 10, 10, 190, 210)
    $serverList = GUICtrlCreateListView("No|Server|Server UNC Path", 30, 30, 147, 161)

    $mapDrvButton = GUICtrlCreateButton("&Map Drive", 30, 195, 90, 20)
    $StatusMappedDrive = GUICtrlCreateLabel("---", 125, 197, 80, 20)

    ;Create the list of devices
    $deviceGroup = GUICtrlCreateGroup("Devices", 210, 10, 190, 210)
    $deviceList = GUICtrlCreateListView("No|Model|Ghost", 230, 30, 147, 161)
    
    ;Create the menu & menu items
    $filemenu = GUICtrlCreateMenu("&File")
    $exititem = GUICtrlCreateMenuItem("E&xit", $filemenu)
    $helpmenu = GUICtrlCreateMenu("&Help")
    $aboutitem = GUICtrlCreateMenuItem("&About", $helpmenu)
    
    ;Create the progress bar
    $progressBar = GUICtrlCreateProgress(10, 225, 390, 20)
    
    ;Create the OK & Cancel buttons
    $okbutton = GUICtrlCreateButton("&OK", 70, 250, 70, 20)
    $cancelbutton = GUICtrlCreateButton("&Cancel", 270, 250, 70, 20)

    ;INI file items
    GetIni()
    CheckMappedDrive()

    GUISetState()
    
    ;Check for default values
    If $arrDefault[1] <> "" And $arrDefault[2] <> "" Then
        Dim $defaultServer[2], $defaultComputer[2]
        $defaultServer = StringSplit($arrDefault[1], "|")
        $defaultComputer = StringSplit($arrDefault[2], "|")
        If MsgBox(4, "Default", "Do you want to use the image '" & $defaultComputer[1] & "'" & @CRLF & "On the server '" & $defaultServer[1] & "'?") = 6 Then
            MapDriveQuick($defaultServer[2])
            Ghosting($defaultComputer[1],$defaultComputer[2])
        EndIf
    ElseIf $arrDefault[1] <> "" And $arrDefault[2] = "" Then
        Dim $defaultServer[2]
        $defaultServer = StringSplit($arrDefault[1], "|")
;~      MsgBox(0, "Server", "Only the server is set")
        MapDriveQuick($defaultServer[2])
    ElseIf $arrDefault[1] = "" And $arrDefault[2] <> "" Then
        $defaultComputer[2]
        $defaultComputer = StringSplit($arrDefault[2], "|")
        MsgBox(0, "Model", "Only the computer model is set")
    EndIf
  
    ; Create dummy key for accelerator
    Local $hENTER = GUICtrlCreateDummy()
    ; Set accelerator for Enter
    Dim $AccelKeys[1][2]=[["{ENTER}", $hENTER]]
    GUISetAccelerators($AccelKeys)

    while 1
        $msg = GUIGetMsg()
        Select
            Case $msg = $cancelbutton Or $msg = $GUI_EVENT_CLOSE
                ExitLoop

            Case $msg = $exititem
                ExitLoop
                
            Case $msg = $hENTER OR $msg = $okbutton
                If CheckMappedDrive()=1 Then
                    $SelectedListViewServerItem=GUICtrlRead(GUICtrlRead($serverList))
                    If $SelectedListViewServerItem=0 Then
                            MsgBox(0, "Server Selection", "No server selected")
                            GUICtrlSetState($serverList, $GUI_FOCUS)
                        Else
                            MapDrive(GUICtrlRead(GUICtrlRead($serverList)))
                            GUICtrlSetState($deviceList, $GUI_FOCUS)
                    EndIf
                Else
                    $SelectedListViewDeviceItem=GUICtrlRead(GUICtrlRead($deviceList))
                    If $SelectedListViewDeviceItem==0 Then
                        MsgBox(0, "Device Selection", "No device selected")
                        GUICtrlSetState($deviceList, $GUI_FOCUS)
                    Else
                        GUICtrlSetData($progressBar, 100)
                        ConsoleWrite($SelectedListViewDeviceItem & @CRLF)
                        Local $Fields=StringSplit($SelectedListViewDeviceItem,"|")
                        ConsoleWrite($Fields[2] & ", " & $Fields[3] & @CRLF)
                        Ghosting($Fields[2],$Fields[3])
                    EndIf
                EndIf

            Case $msg = $aboutitem
                MsgBox(0, "About", "Version 1.1.0" & @CRLF & "Developed by Luis Lugo")
                
            Case $msg = $mapDrvButton
                $SelectedListViewServerItem=GUICtrlRead(GUICtrlRead($serverList))

                if $SelectedListViewServerItem=0 Then
                        MsgBox(0, "Server Selection", "No server selected")
                    Else
                        MapDrive(GUICtrlRead(GUICtrlRead($serverList)))
                endif
        EndSelect
    WEnd

    GUIDelete()

    Exit
EndFunc   ;==>_Main

Func Ghosting($SelectedDeviceType,$SelectedDevice)
    If DriveMapGet("J:")<>"" Then
        Local $ghosting = RunWait("Ghost32.exe -NTIL -Clone,mode=load,src=J:\" & $SelectedDevice & ".GHO,dst=1 -batch")
        Run("Wpeutil reboot", "x:")
        GUICtrlSetState($okbutton, $GUI_DISABLE)
    Else 
        MsgBox(0, "J drive not mapped", "The ghost image directory hasn't been mapped. Please map it before proceeding")
    EndIf
EndFunc

;~ =============================================================
Func GetIni()
    $DeviceListItem = IniReadSection("x.ini", "Devices")
    $ServerListItem = IniReadSection("x.ini", "Servers")
    $DefaultListItem = IniReadSection("x.ini", "Defaults")
        For $i = 1 To $DeviceListItem[0][0]
            $arrDevice[$i] = GUICtrlCreateListViewItem($i&"|" & $DeviceListItem[$i][1], $deviceList)
        Next
        For $i = 1 To $ServerListItem[0][0]
            $arrServer[$i] = GUICtrlCreateListViewItem($i&"|" & $ServerListItem[$i][0]&"|" & $ServerListItem[$i][1], $serverList);
        Next
        For $i = 1 To $DefaultListItem[0][0]
            $arrDefault[$i] = $DefaultListItem[$i][1]
        Next
EndFunc

;~ =============================================================
Func MapDrive($SelectedListViewServerItem)
    local $err,$errorList,$answer,$setUser
    consolewrite($SelectedListViewServerItem)
    $strServerPath=GetComputerName($SelectedListViewServerItem)
    consolewrite($strServerPath & @CRLF)
        if $strServerPath == "" Then
            $strServerPath = InputBox("Custom Server", "Custom Server Address:")
        EndIf

        local $repeat = 1
        local $success = 0
        While $repeat = 1
            If DriveMapGet($DriveMapLetter) <> $strServerPath Then
                DriveMapDel($DriveMapLetter)
                sleep(250)
                $setUser = GetUser()
                If $setUser = "" Then
                    $mapped = DriveMapAdd($DriveMapLetter, $strServerPath, 8)
                Else
                    $mapped = DriveMapAdd($DriveMapLetter, $strServerPath, 8, $setUser)
                EndIf
                $errorList = " Undefined / Other error|Access to the remote share was denied|The device is already assigned|Invalid device name|Invalid remote share|Invalid password"
                $errorList = StringSplit($errorList, "|");make an array for error code lookup
                If $mapped Then
                    CheckMappedDrive()
                    ;MsgBox(0, "Drive Mapped", "Drive mapped to " & @CRLF & $strServerPath)
                    $repeat = 0
                ElseIf Not $mapped then
                    $success = 0
                    If $err = 6 or $err = 2 Then
                        $answer = MsgBox(6,"Error", "Username or password is invalid... Please try again.")
                        If $answer <> $IDTRYAGAIN Then $repeat = 0
                    Else
                        $answer = MsgBox(4096,"Error", "Error code: " & $errorList[$err])
                        If $answer <> $IDTRYAGAIN Then $repeat = 0
                    EndIf
                EndIf
            Else
                MsgBox(0, "Already Mapped", "The drive is already mapped to the selected server")
                $repeat = 0
            EndIf
        WEnd
EndFunc

;~ =============================================================
Func MapDriveQuick($defaultServer)
    Local $err,$errorList,$answer,$setUser
    Local $repeat = 1, $success = 0
    While $repeat = 1
        If DriveMapGet($DriveMapLetter) <> $defaultServer Then
            DriveMapDel($DriveMapLetter)
            sleep(250)
            $setUser = GetUser()
            If $setUser = "" Then
                $mapped = DriveMapAdd($DriveMapLetter, $defaultServer, 8)
            Else
                $mapped = DriveMapAdd($DriveMapLetter, $defaultServer, 8, $setUser)
            EndIf
            $errorList = "Undefined / Other error|Access to the remote share was denied|The device is already assigned|Invalid device name|Invalid remote share|Invalid password|Other access error"
            $errorList = StringSplit($errorList, "|");make an array for error code lookup
            If $mapped Then
                CheckMappedDrive()
                ;MsgBox(0, "Drive Mapped", "Drive mapped to " & @CRLF & $defaultServer)
                $repeat = 0
            ElseIf Not $mapped then
                $success = 0
                If $err = 6 or $err = 2 Then
                    $answer = MsgBox(6,"Error", "Username or password is invalid... Please try again.")
                    If $answer <> $IDTRYAGAIN Then $repeat = 0
                Else
                    $answer = MsgBox(4096,"Error", "Error code: " & $errorList[$err])
                    If $answer <> $IDTRYAGAIN Then $repeat = 0
                EndIf
            EndIf
        Else
            MsgBox(0, "Already Mapped", "The drive is already mapped to the selected server")
            $repeat = 0
        EndIf
    WEnd
EndFunc

;~ =============================================================
Func GetUser()
    $UserName = IniReadSection("x.ini", "User")
        For $i = 1 To $UserName[0][0]
            $arrUser[$i] = $UserName[$i][1]
        Next
        Return $arrUser[1]
EndFunc

;~ =============================================================
Func GetComputerName($SelectedListViewServerItem)
    Local $Fields=StringSplit($SelectedListViewServerItem,"|")
    Return $Fields[3]
EndFunc

;~ =============================================================
Func CheckMappedDrive()
    local $tempResult,$returnEL

    $tempResult=DriveMapGet($DriveMapLetter)
    $returnEL=@error
    If $tempResult="" Then
        GUICtrlSetData($StatusMappedDrive,$DriveMapLetter & " not mapped")
    Else
        GUICtrlSetData($StatusMappedDrive,$DriveMapLetter & " is mapped")
        GUICtrlSetData($progressBar, 50)
    EndIf
    Return $returnEL
EndFunc

Share this post


Link to post
Share on other sites



For Select an item in a listview, you could do like this :

#include <GuiConstantsEx.au3>
#include <GuiListView.au3>

Global $hListView, $_SelectItemOld

GUICreate("ListView Get Item Selected", 400, 300)
$hListView = GUICtrlCreateListView("", 2, 2, 394, 268)
GUISetState()

_GUICtrlListView_AddColumn($hListView, "Items", 100)
_GUICtrlListView_AddItem($hListView, "Item 1")
_GUICtrlListView_AddItem($hListView, "Item 2")
_GUICtrlListView_AddItem($hListView, "Item 3")

Do
    $_SelectItem = _GUICtrlListView_GetSelectedIndices ( $hListView )
    If $_SelectItem <> $_SelectItemOld Then
        MsgBox(4160, "Information", "Selected Item: " & $_SelectItem+1 )
        $_SelectItemOld = $_SelectItem
    EndIf
Until GUIGetMsg() = $GUI_EVENT_CLOSE

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

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