Parse ComboBox list

Get help with using AutoHotkey and its commands and hotkeys
sttrebo
Posts: 68
Joined: 27 Jan 2014, 12:31

Parse ComboBox list

07 Jul 2017, 11:27

Hi-
I'm trying to integrate some test equipment and need a bit of guidance on part of the script. The test equipment outputs a .csv file like this:

1a2b3c44 > configuration #1
aa263ee248ce > new configuration #3
-1a3e4d5f2n > delete this configuration
xxxefff > last configuration

For the gui part of this script, I need to parse each line, join the first parts (before the '>') and populate a combobox. I was hoping to have the part after the '>' fill in a text field in the gui. see attached screenshot.
https://www.dropbox.com/s/pk5046l5ezxgd ... x.jpg?dl=0

Here is the rfelevant part of the code:

Code: Select all

; =======================================================================================
; Name ..........: Shortcuts.ahk
; Description ...: Hotkey script to load/save strings
; AHK Version ...: AHK_L 1.1.26.00 (Unicode 64-bit)
; Author ........: Steve Trebotich ([email protected])
; =======================================================================================

; Changelog =============================================================================
; Legend: (+) NEW, (*) CHANGED, (!) FIXED
; ---------------------------------------------------------------------------------------
; YYYY-MM-DD  FirstName LastName  <[email protected]>
; + Change Description
; ---------------------------------------------------------------------------------------
; YYYY-MM-DD  FirstName LastName  <[email protected]>
; + Change Description
; =======================================================================================

;++++++++++++++++
; ++++To Do's++++
; Fix GUI
; Read/Write 2 variables to ini file
; Edit/New entries
; Setup "Today" to insert today's date



; Global ================================================================================
#SingleInstance, Force ; Allow only one running instance of script
#Persistent ; Keep script permanently running until terminated
#NoEnv ; Avoid checking empty variables to see if they are environment variables
#Warn ; Enable warnings to assist with detecting common errors
#NoTrayIcon ; Disable the tray icon of the script
SendMode, Input ; Recommended for new scripts due to its superior speed and reliability
SetWorkingDir, %A_ScriptDir% ; Change the working directory of the script
SetBatchLines, -1 ; Run script at maximum speed

F6::
MouseGetPos, LocX, LocY
newlines =
FileDelete, newlines.txt

Loop, Read, shortcuts.csv
{
	RegExMatch(A_LoopReadLine, "^(.*?)>", lines)
	StringTrimRight, lines, lines, 2
	newlines = %lines%|
	FileAppend, %newlines%, newlines.txt
}

FileRead, combolines, newlines.txt

; Gui, Add, ComboBox, x10 y30 w100 h10 , %combolines%
; Gui, Add, Combobox, gparseit, %combo1%
Gui, Add, ComboBox, , %combolines%
; Gui, Add, ComboBox, x125 y30 w200 h10 , ComboBox
; Gui, Add, Text, x10 y10 w100 h20 +Center, Category
; Gui, Add, Text, x150 y10 w100 h20 +Center, Item(s)
Gui, Add, Text, , %combolines%
Gui, Add, Button, x92 y140 w50 h20 , Go!
Gui, Add, Button, x162 y140 w60 h20 , Cancel

; Gui, Show, X%LocX% Y%LocY% h200 w335, 
Gui, Show

; GuiControl, Choose, %combolines%, 0

; Return

; parseit:
; combo1 := SubStr(combo, combolines, 2)
; msgbox, %combo1%
return
The commented out sections is my current attempt (no luck). any ideas on this?

thanks
BoBo
Posts: 3853
Joined: 13 May 2014, 17:15

Re: Parse ComboBox list

07 Jul 2017, 11:37

So, that's the content of shortcut.csv (without the header)?
deleted the rest of my stupid question :crazy: :oops:
Requested CSV file given below :thumbup:
Last edited by BoBo on 07 Jul 2017, 12:25, edited 1 time in total.
sttrebo
Posts: 68
Joined: 27 Jan 2014, 12:31

Re: Parse ComboBox list

07 Jul 2017, 11:53

here's a sample file:
https://www.dropbox.com/s/gos35w4d7weta ... s.csv?dl=0

no header or anything. just a simple .csv (text) file.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Parse ComboBox list

07 Jul 2017, 17:54

It is not clear for me if the following is what your trying to achieve; I hope this could somehow bring help anyway:

Code: Select all

#NoEnv
#Warn
#SingleInstance force


; 1a2b3c44 > configuration #1
; aa263ee248ce > new configuration #3
; -1a3e4d5f2n > delete this configuration
; xxxefff > last configuration

F6::
combolines = ; empty string
textlines := [] ; empty array
Loop, Read, % A_ScriptDir . "\shortcuts.csv"
{
parts := StrSplit(A_LoopReadLine, A_Space . ">" . A_Space) ; splits A_LoopReadLine with A_Space . ">" . A_Space as delimiter in order to retrieve the parts of the field

combolines .=  parts[1] . "|" ; appends part1 + the combobox default delimiter to combolines
textlines.push(parts[2]) ; appends part2 to the end of textlines array
}
combolines := RTrim(combolines, "|") ; removes the last trailing delimiter from combolines
	
Gui, 1:Add, ComboBox, vComboBoxControl glabel +AltSubmit, % combolines ; altsubmit property causes both Gui Submit and GuiControlGet (as below) command to store the position of the selected item rather than its text

Gui, 1:Add, Text, vtextControl w200
Gui, 1:Add, Button, x92 y140 w50 h20, Go!
Gui, 1:Add, Button, x162 y140 wp hp, Cancel
Gui, 1:Show, AutoSize
return

label:
GuiControlGet, index,, ComboBoxControl 
GuiControl, 1:, textControl, % textlines[index] ; set the text control text to be the one of the textlines array element at index
return
my scripts
sttrebo
Posts: 68
Joined: 27 Jan 2014, 12:31

Re: Parse ComboBox list

07 Jul 2017, 18:15

this looks like a great start, thanks for your help. the 'textlines.push(parts[2]) line is interesting. learned something new. quick one though: the +AltSubmit property ... store the position of the selected item. How do I get the text of the actual item selected?
User avatar
YoucefHam
Posts: 344
Joined: 24 Aug 2015, 12:56
Facebook: https://fb.me/YoucefHamDz
Google: [email protected]
GitHub: YoucefHam
Location: Algeria
Contact:

Re: Parse ComboBox list

07 Jul 2017, 23:12

Try this

Code: Select all

Gui, Font, s8 bold
Gui, Add, Edit, % "x10 y10 w460 Center vTxtVar", Text_Variable_Without_Space
Gui, Add, DropDownList, % "x10 w300 Choose1 gSelectTxt vSelectTxt", % MiniGet()
GuiControl,, TxtVar, % (getControl("SelectTxt") = "" ? "Text_Variable_Without_Space" : getControl("SelectTxt"))
Gui, Add, Button, % "x+5 w90 h40 gSaveTxt", SAVE
Gui, Add, Button, % "x+5 w60 h40 gDelTxt", DELETE
Gui, Font, s10 bold underline
Gui, Add, Text, % "x10 y+-15 cBlue", Text To Save :
Gui, Font, s8 norm
Gui, Add, Checkbox, % "x+20 gWinTop Checked", Window Always On Top
Gui, Font, s10 bold
Gui, Add, Edit, % "x10 y+5 W460 h320 vTxt",
gosub, SelectTxt
Gui, Font, s8
Gui, Add, Button, % "x10 y+10 w460 gCopyTxt", COPY

Gui, show
Gui, +AlwaysOnTop +LastFound -Theme
return

WinTop:
Gui, +LastFound
WinGet, ExStyle, ExStyle
if (ExStyle & 0x8)
	Gui, -AlwaysOnTop
else
	Gui, +AlwaysOnTop
return

SelectTxt:
Gui, +LastFound
getcontrol("SelectTxt")
GuiControl,, TxtVar, % SelectTxt
GuiControl,, Txt, % strReplace(strReplace(MiniR(,SelectTxt),"A_NewLine","`n"),"A_NewTrueLine","A_NewLine")
SoundPlay *64
TxtIn :=
return

DelTxt:
Gui, +LastFound
MiniD(getcontrol("SelectTxt"))
GuiControl,, SelectTxt, |
GuiControl,, SelectTxt, % MiniGet()
GuiControl,Choose, SelectTxt, 1
GuiControl,, TxtVar, % getcontrol("SelectTxt")
GuiControl,, Txt, % strReplace(strReplace(MiniR(,getcontrol("SelectTxt")),"A_NewLine","`n"),"A_NewTrueLine","A_NewLine")
SoundPlay *64
return

CopyTxt:
Gui, +LastFound
getcontrol("SelectTxt")
getcontrol("TxtVar")
getcontrol("Txt")
if (TxtVar = "") or (Txt = "") or (SelectTxt = "")
	ErrorMsg("Blanc Field")
If (SelectTxt = TxtVar) And !(Txt = strReplace(MiniR(,SelectTxt),"A_NewLine","`n"))
{
	Gui, +OwnDialogs
	MsgBox, 4148, Text Don't Match !!!, Text Don't Match Saved Text.`nCopy Anyway.
	IfMsgBox, No
		return
}
Clipboard := Txt
SoundPlay *64
return

SaveTxt:
Gui, +LastFound
getcontrol("SelectTxt")
getcontrol("TxtVar")
getcontrol("Txt")
if (TxtVar = "") or (Txt = "")
	ErrorMsg("Blanc Field")
IfInString, Txt, `n
	Loop, Parse, % Txt, `n
	{
		Line := A_LoopField
		IfInString, Line, A_NewLine
			Line := strReplace(Line,"A_NewLine","A_NewTrueLine")
		TxtOut .= Line "A_NewLine"
	}
If (SelectTxt = TxtVar) And !(Txt = strReplace(MiniR(,SelectTxt),"A_NewLine","`n"))
{
	Gui, +OwnDialogs
	MsgBox, 4148, Text Exist !!!, You want to Overwrite.
	IfMsgBox, No
		return
}
IfInString, TxtOut, A_NewLine
	MiniW(SubStr(TxtOut,1,StrLen(TxtOut)-9),TxtVar)
else
	MiniW(Txt,TxtVar)
TxtOut := ""
GuiControl,, SelectTxt, |
GuiControl,, SelectTxt, % MiniGet()
GuiControl,ChooseString, SelectTxt, % TxtVar
SoundPlay *64
return

GuiClose:
ExitApp
return

ErrorMsg(Msg="Error") {
	SoundPlay *16
	Gui, +OwnDialogs
	MsgBox, 4112, Error, % Msg, 3
	Exit
}
MiniGet() {
	global
	SavedTextList :=
	IfExist, Saved Text.INI
		Loop, Parse, % MiniR("Saved Text"), `n
			Loop, Parse, % A_LoopField, =
				SavedTextList .= (A_Index = 1 ? A_LoopField "|" :)
	return % SavedTextList
}
getControl(var="var",parm="") {
	global
	GuiControlGet, %var%,%parm%,%var%
	var := %var%
	return var
}
MiniW(var,Key) {
	global
	IniWrite, %var%, % "Saved Text.INI", % "TextList", %Key%
	return
}
MiniR(var = "var",Key="") {
	global
	IniRead, var, % "Saved Text.INI", % "TextList", %Key%, % ""
	return %var%
}
MiniD(Key) {
	global
	IniDelete, % "Saved Text.INI", % "TextList", %Key%
	return
}

:wave: There is always more than one way to solve a problem. ;)
sttrebo
Posts: 68
Joined: 27 Jan 2014, 12:31

Re: Parse ComboBox list

07 Jul 2017, 23:45

thanks YoucefHam, but i'm not sure what to do with this example. but there are some interesting things in the example to learn from.
A_AhkUser
Posts: 1147
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: Parse ComboBox list

08 Jul 2017, 15:47

sttrebo wrote:the +AltSubmit property ... store the position of the selected item. How do I get the text of the actual item selected?

Once the AltSubmit option is set to the combobox you still can retrieve the text (choice) using the following template:

ControlGet, text, Choice,,, AHKIDOfTheControl

Code: Select all

#NoEnv
#Warn
#SingleInstance force


; 1a2b3c44 > configuration #1
; aa263ee248ce > new configuration #3
; -1a3e4d5f2n > delete this configuration
; xxxefff > last configuration

F6::
combolines = ; empty string
textlines := [] ; empty array
Loop, Read, % A_ScriptDir . "\shortcuts.csv"
{
parts := StrSplit(A_LoopReadLine, A_Space . ">" . A_Space) ; splits A_LoopReadLine with A_Space . ">" . A_Space as delimiter in order to retrieve the parts of the field

combolines .=  parts[1] . "|" ; appends part1 + the combobox default delimiter to combolines
textlines.push(parts[2]) ; appends part2 to the end of textlines array
}
combolines := RTrim(combolines, "|") ; removes the last trailing delimiter from combolines
	
Gui, 1:Add, ComboBox, vComboBoxControl glabel hwndcomboBoxID +AltSubmit, % combolines ; hwndcomboBoxID stores theID of the control in ComboboxID

Gui, 1:Add, Text, vtextControl w200
Gui, 1:Add, Button, x92 y140 w50 h20, Go!
Gui, 1:Add, Button, x162 y140 wp hp, Cancel
Gui, 1:Show, AutoSize
return

label:
GuiControlGet, index,, ComboBoxControl 
GuiControl, 1:, textControl, % textlines[index] ; set the text control text to be the one of the textlines array element at index
ControlGet, text, Choice,,, % "ahk_id " . comboBoxID
TrayTip,, % text
return
Btw, concerning textlines.push(parts[2]) you can find in the documentation a list of all the methods supported by objects (creating using [], {}, Object(), Array()).


Cheers.
my scripts
sttrebo
Posts: 68
Joined: 27 Jan 2014, 12:31

Re: Parse ComboBox list

08 Jul 2017, 16:31

excellent, thanks. been looking at the textlines.push(parts[2]) stuff this morning. interesting options here... the array stuff is my next learning focus. thanks again for all your help. what i have at this point works perfectly.

Return to “Ask For Help”

Who is online

Users browsing this forum: BeSu, Chunjee, DaveT1, Google [Bot], mikeyww, pizzapizze, sachin24 and 50 guests