Sign in to follow this  
Followers 0
Albatroz

Help with Listview and array

3 posts in this topic

hi,,, im creating a little program to become management easier in mom's office.

I can create listview, fill it but when i choose line and click "carregar" Im getting alwas the last array. And, the first line showed in Listview isnt necessary but i dont know how remove it.

if I test picking selected line with => _ArrayDisplay($codid) ,,,, i get the correct selection (codid).

I need some help plz

thx

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <GUIListViewEx.au3>
#include <Guiedit.au3>
#include <file.au3>
### 

Global $tabelaDB = "clientes.db", $file
Global $scriptbusca = "buscasql.txt"
Global $buscatxt = "busca.txt"

;Checa se existe tabela. Cria se não tiver ainda.
if Not FileExists($TabelaDB) Then
_DoSQL("CREATE TABLE Clientes (CODCli integer primary key, razao varchar(40), fantasia varchar(30), cnpj varchar(20), insce varchar(20), endereco varchar(45), bairro varchar(30), cidade varchar(40), estado varchar(2), complemento varchar(30), telefone1 varchar(14), telefone2 varchar(14), fax varchar(14), ramal varchar(4), celular varchar(14), email1 varchar(40), email2 varchar(40), obs varchar(100));")
Else
EndIf


GuiCadastro()

Func GuiCadastro()
#Region ### START Koda GUI section ###
Global $GUIPedido = GUICreate(" Novo Cadastro", 800, 500, -1, -1) ; 465, 245, 269, 193
GUICtrlCreateLabel("Código Cadastro:", 10, 20)
;~ $showcod = GUICtrlCreateInput($xcodNew, 93, 18, 60, 20)
GUICtrlCreateLabel("Razão Social:", 25, 43)
$razao = GUICtrlCreateInput("", 93, 40, 260, 20)
GUICtrlCreateLabel("Nome Fantasia:", 17, 65)
$fantasia = GUICtrlCreateInput("", 93, 62, 260, 20)
GUICtrlCreateLabel("CNPJ:", 425, 40)
$cnpj = GUICtrlCreateInput("", 456, 40, 140, 20)
GUICtrlCreateLabel("Inscrição Estadual:", 365, 65)
$insce = GUICtrlCreateInput("", 456, 62, 140, 20)
GUICtrlCreateLabel("Endereço:", 42, 88)
$endereco = GUICtrlCreateInput("", 93, 84, 260, 20)
GUICtrlCreateLabel("Bairro:", 425, 88)
$Bairro = GUICtrlCreateInput("", 456, 84, 140, 20)
GUICtrlCreateLabel("Cidade:", 55, 109)
$Cidade = GUICtrlCreateInput("", 93, 106, 140, 20)
GUICtrlCreateLabel("Estado:", 285, 109)
$Estado = GUICtrlCreateInput("", 323, 106, 30, 20)
GUICtrlCreateLabel("Complemento:", 388, 109)
$Complemento = GUICtrlCreateInput("", 456, 106, 140, 20)
GUICtrlCreateLabel("Telefone1:", 40, 131)
$Telefone1 = GUICtrlCreateInput("", 93, 128, 140, 20)
GUICtrlCreateLabel("Telefone2:", 404, 131)
$Telefone2 = GUICtrlCreateInput("", 456, 128, 140, 20)
GUICtrlCreateLabel("Fax:", 71, 153)
$Fax = GUICtrlCreateInput("", 93, 150, 140, 20)
GUICtrlCreateLabel("Ramal:", 288, 153)
$Ramal = GUICtrlCreateInput("", 323, 150, 30, 20)
GUICtrlCreateLabel("Celular:", 420, 153)
$Celular = GUICtrlCreateInput("", 456, 150, 140, 20)
GUICtrlCreateLabel("e-mail1:", 55, 175)
$email1 = GUICtrlCreateInput("", 93, 172, 140, 20)
GUICtrlCreateLabel("e-mail2:", 419, 175)
$email2 = GUICtrlCreateInput("", 456, 172, 140, 20)
GUICtrlCreateLabel("Observações:", 25, 210)
$obs = _GUICtrlEdit_Create ($Guipedido, "", 93, 194, 503, 90)


;---Botoes Menu Pedido
    $FindName = GUICtrlCreateButton("Localizar Nome", 660, 30, 130)
        GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
    $FindMail = GUICtrlCreateButton("Localizar e-mail", 660, 60, 130)
        GUICtrlSetFont(-1, 4, 800, 0, "MS Sans Serif")
    $Cadgrava = GUICtrlCreateButton("Salvar Cadastro", 660, 90, 130)
        GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
        GUICtrlSetColor(-1, 0x0000FF)
    $CadEdit = GUICtrlCreateButton("Editar Cadastro", 660, 120, 130)
        GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
        GUICtrlSetColor(-1, 0xFF0000)
    $Excluir = GUICtrlCreateButton("Excluir Cliente", 660, 230, 130)
        GUICtrlSetFont(-1, 1, 600, 0, "MS Sans Serif")
        GUICtrlSetColor(-1, 0xFF0000)       
    $Carregar = GUICtrlCreateButton("Carregar Cliente", 660, 260, 130)
        GUICtrlSetFont(-1, 1, 600, 0, "MS Sans Serif")
        GUICtrlSetColor(-1, 0x00AB00)       
#EndRegion ### END Koda GUI section ###


;~ Global $listview = GUICtrlCreateListView("", 10, 290, 780, 200, BitOR($LVS_SHOWSELALWAYS, $LVS_REPORT, $WS_BORDER, $LVS_SINGLESEL))
Global $listview = _GUICtrlListView_Create($guipedido, "Código", 10, 290, 780, 200, BitOR($LVS_SHOWSELALWAYS, $LVS_REPORT, $WS_BORDER, $LVS_SINGLESEL))
_GUICtrlListView_SetExtendedListViewStyle($listview, $LVS_EX_FULLROWSELECT)
;~ _GUICtrlListView_AddColumn($ListView, "Código", 70, 0)
_GUICtrlListView_AddColumn($ListView, "Nome Fantasia", 350, 0)
_GUICtrlListView_AddColumn($ListView, "Telefone", 150, 2)
_GUICtrlListView_AddColumn($ListView, "e-mail", 200, 0)


GUISetState()

$msg = 0
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            GUIDelete()
            ExitLoop
        Case $msg = $findname
            PesquisaCli()
        Case $msg = $findmail
            PesquisaEMail()
        Case $msg = $CadEdit
            CadEdit()
        Case $msg = $Excluir
            Excluir()
        Case $msg = $Carregar
            CadLoad()
        Case $msg = $Cadgrava
;~          Global $vcod = GUICtrlRead($showcod)
            Global $vrazao = GUICtrlRead($razao)
            Global $vfantasia = GUICtrlRead($fantasia)
            Global $vcnpj = GUICtrlRead($cnpj)
            Global $vinsce = GUICtrlRead($insce)
            Global $vendereco = GUICtrlRead($endereco)
            Global $vbairro = GUICtrlRead($bairro)
            Global $vcidade = GUICtrlRead($cidade)
            Global $vestado = GUICtrlRead($estado)
            Global $vcomplemento = GUICtrlRead($complemento)
            Global $vtelefone1 = GUICtrlRead($telefone1)
            Global $vtelefone2 = GUICtrlRead($telefone2)
            Global $vfax = GUICtrlRead($fax)
            Global $vramal = GUICtrlRead($ramal)
            Global $vcelular = GUICtrlRead($celular)
            Global $vemail1 = GUICtrlRead($email1)
            Global $vemail2 = GUICtrlRead($email2)
            Global $vobs = _GUICtrlEdit_GetText($obs)
            GravaCad()
            GUIDelete()
            GuiCadastro()
            ExitLoop
    EndSelect
WEnd
EndFunc

Func GravaCad()
;cadastrar
If $vrazao <> "" And $vcnpj <> "" And $vinsce <> "" And $vendereco <> "" And $vcidade <> "" And $vestado <> "" and $vtelefone1 <> "" Then
_DoSQL("INSERT INTO Clientes (razao, fantasia, cnpj, insce, endereco, bairro, cidade, estado, complemento, telefone1, telefone2, fax, ramal, celular, email1, email2, obs) values ('" & $vrazao & "', '" & $vfantasia & "', '" & $vcnpj & "', '" & $vinsce & "', '" & $vendereco & "', '" & $vbairro & "', '" & $vcidade & "', '" & $vestado & "', '" & $vcomplemento & "', '" & $vtelefone1 & "', '" & $vtelefone2 & "', '" & $vfax & "', '" & $vramal & "', '" & $vcelular & "', '" & $vemail1 & "', '" & $vemail2 & "', '" & $vobs & "');")
Else
MsgBox("16", " Erro durante o Cadastro!", "Faltou o Cadastro de Campos Obrigatórios:" & @LF & @LF & "Razão Social" & @LF & "CNPJ" & @LF & "Inscrição Estadual" & @LF & "Endereço" & @LF & "Cidade" & @LF & "Estado" & @LF & "Telefone 1")
EndIf
;----------
EndFunc


Func PesquisaINI()
Global $file = "pesquisa.txt"
If Not FileExists($file) Then
$newtitle = "echo [0] >" & @ScriptDir & "\pesquisa.txt"
RunWait(@ComSpec & " /C " &$newtitle, "", @SW_HIDE)
EndIf
EndFunc


Func PesquisaCli()
Global $pesquisa = InputBox("Busca de e-mail", "Procurar o e-mail: ")
FileOpen($scriptbusca, 10)
FileWrite($scriptbusca, ".mode list" & @lf)
FileWrite($scriptbusca, ".separator |" & @lf)
FileWrite($scriptbusca, ".output busca.txt" & @lf)
FileWrite($scriptbusca, "select codcli,razao,fantasia,email1 FROM Clientes Where razao like '" & $pesquisa & "%' or fantasia like '" & $pesquisa & "%';" & @lf)
FileWrite($scriptbusca, ".exit" & @lf)
FileClose($scriptbusca)
RunWait(@COMSPEC & " /c SQLite3.exe " & $TabelaDB & "< " & $scriptbusca, @SCRIPTDIR, @SW_HIDE)

Global $maxreg = _FileCountLines($buscatxt)
$file = FileOpen($buscatxt, 0)
Global $array[$maxreg + 1][4] = [[$maxreg]]
For $i = 1 To UBound($array) - 1
Global $line = FileReadLine($file, $i)
Global $codst = StringLeft($line, 1)
$2st = StringMid($line, StringInStr($line, "|", 0, 1), StringInStr($line, "|", 0, 2)-StringInStr($line, "|", 0, 1))
Global $fanst = StringReplace($2st, "|", "")
$3st = StringMid($line, StringInStr($line, "|", 0, 2), StringInStr($line, "|", 0, 3)-StringInStr($line, "|", 0, 2))
Global $telst = StringReplace($3st, "|", "")
$4st = StringMid($line, StringInStr($line, "|", 0, 3), StringInStr($line, "|", 0, 4)-StringInStr($line, "|", 0, 3))
Global $mailst = StringReplace($4st, "|", "")
$array[$i][0] = $codst
$array[$i][1] = $fanst
$array[$i][2] = $telst
$array[$i][3] = $mailst
;~ GUICtrlCreateListViewItem($line, $listview)
;~ Global $CodLista = _GUICtrlListView_AddItem($listview, $array[$i][0])
;~  _GUICtrlListView_AddSubItem($listview, $i -1, $array[$i][1], 1)
;~  _GUICtrlListView_AddSubItem($listview, $i -1, $array[$i][2], 2)
;~  _GUICtrlListView_AddSubItem($listview, $i -1, $array[$i][3], 3)
Next
FileClose($file)
_GUICtrlListView_AddArray($ListView, $array)
Global $LVIndex = _GUIListViewEx_Init($listview, $array, 0, 0, True)
EndFunc



Func CadLoad()
$array = _GUIListViewEx_Return_Array($LVIndex)

$CodId = _GUICtrlListView_GetSelectedIndices($listview, true) ;;;original - single selection
;~      _ArrayDisplay($codid)
MsgBox(0, "test", $codst &@LF& $fanst &@LF& $telst &@LF& $mailst)
_GUICtrlListView_DeleteAllItems($ListView)
EndFunc


Func Excluir()
$CodId = _GUICtrlListView_GetSelectedIndices($listview, true) ;;;original - single selection
_GUIListViewEx_Delete()
EndFunc


Func _DoSQL($psSQL)
;pass the SQL statement to SQLite3.exe
;assumptions:
;------------
;   + SQLite3.exe is placed in the system path (eg C:\WINNT\SYSTEM32)
;   + you have already created a database in the script path called "$TabelaDB"
Local $sX, $sSQLout, $sSQLin

;determine  filenames
    $sSQLin = @SCRIPTDIR & "\_sql4au3.sql"
    $sSQLout = "_sql4au3.out"
    
;Set preferences and udate $sSQLin
   ;To parse records, just leave the defualt of pipe-delimited fields,
   ;then you can StringSplit each record on "|"..
    $sX = ".header off" & @LF
    $sX = $sX & ".mode column" & @LF
   ;nominate the file to receive any results (the recordset)..
    $sX = $sX & ".output " & $sSQLout & @LF
    $sX = $sX & $psSQL 
    
    FileDelete($sSQLout)
    FileDelete($sSQLin)
    FileWrite($sSQLin, $sX)
    
;Send it all to SQLite3
    RunWait(@COMSPEC & " /c SQLite3.exe " & $TabelaDB & "<" & FileGetShortName($sSQLin), @SCRIPTDIR, @SW_HIDE)
    
;Get the output to send back    
    If FileExists($sSQLout) then
        Return FileRead($sSQLout, FileGetSize($sSQLout))
    Endif
    
EndFunc

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

See what happens if you replaced your CadLoad with

Func CadLoad()
    ;$array = _GUIListViewEx_Return_Array($LVIndex)
    $CodId = _GUICtrlListView_GetSelectedIndices($listview, True) ;;;original - single selection
    ;_ArrayDisplay($codid)
    For $i = 1 To $CodId[0]
        MsgBox(64, "Information", "Item " & $CodId[$i] & ":" & @CRLF & _GUICtrlListView_GetItemTextString($hListView, $CodId[$i]))
    Next
    _GUICtrlListView_DeleteAllItems($listview)
EndFunc   ;==>CadLoad
Also the problem with you not being able to delete the first item of the list view may stem from you not treating the indices as 0 based.

EDIT: Corrected some logical errors

Edited by smartee

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

thx for correction above. It worked like a charm.

But I couldnt fix this problem yet:

if I use this in Func PesquisaCli() it works fine without first line (just total array in Código).

Func PesquisaCli()
Global $pesquisa = InputBox("Busca de e-mail", "Procurar o e-mail: ")
FileOpen($scriptbusca, 10)
FileWrite($scriptbusca, ".mode list" & @lf)
FileWrite($scriptbusca, ".separator |" & @lf)
FileWrite($scriptbusca, ".output busca.txt" & @lf)
FileWrite($scriptbusca, "select codcli,razao,fantasia,email1 FROM Clientes Where razao like '" & $pesquisa & "%' or fantasia like '" & $pesquisa & "%';" & @lf)
FileWrite($scriptbusca, ".exit" & @lf)
FileClose($scriptbusca)
RunWait(@COMSPEC & " /c SQLite3.exe " & $TabelaDB & "< " & $scriptbusca, @SCRIPTDIR, @SW_HIDE)

Global $maxreg = _FileCountLines($buscatxt)
$file = FileOpen($buscatxt, 0)
Global $array[$maxreg + 1][4] = [[$maxreg]]
For $i = 1 To UBound($array) -1
Global $line = FileReadLine($file, $i)
Global $codst = StringLeft($line, 1)
$2st = StringMid($line, StringInStr($line, "|", 0, 1), StringInStr($line, "|", 0, 2)-StringInStr($line, "|", 0, 1))
Global $fanst = StringReplace($2st, "|", "")
$3st = StringMid($line, StringInStr($line, "|", 0, 2), StringInStr($line, "|", 0, 3)-StringInStr($line, "|", 0, 2))
Global $telst = StringReplace($3st, "|", "")
$4st = StringMid($line, StringInStr($line, "|", 0, 3), StringInStr($line, "|", 0, 4)-StringInStr($line, "|", 0, 3))
Global $mailst = StringReplace($4st, "|", "")
$array[$i][0] = $codst
$array[$i][1] = $fanst
$array[$i][2] = $telst
$array[$i][3] = $mailst
Global $CodLista = _GUICtrlListView_AddItem($listview, $array[$i][0]) ;; <<<<< This
    _GUICtrlListView_AddSubItem($listview, $i -1, $array[$i][1], 1) ;; <<<<< This
    _GUICtrlListView_AddSubItem($listview, $i -1, $array[$i][2], 2) ;; <<<<< This
    _GUICtrlListView_AddSubItem($listview, $i -1, $array[$i][3], 3) ;; <<<<< This
Next
FileClose($file)
;~ _GUICtrlListView_AddArray($ListView, $array)
Global $LVIndex = _GUIListViewEx_Init($listview, $array, 0, 0, True)
EndFunc

But, If I use _GUICtrlListView_AddArray($ListView, $array) like post above, I get total arrya in first line. But, _GUICtrlListView_AddArray($ListView, $array) is faster then this _GUICtrlListView_AddItem.

any idea???

thx

Edited by Albatroz

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