Breaking my teeth loading from flat file

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kestak
Posts: 40
Joined: 15 Aug 2018, 06:35

Breaking my teeth loading from flat file

17 May 2021, 06:09

Greetings,

First, I am not a programmer. I worked years as a computer engineer and retired. Help will be greatly appreciated and thank you in advance. I have the feeling, I just miss a small part of how to get the array coordinates rights.

I need to do something very simple. It is a two steps process. The first one is reading from a simple flat file. The lines will always be in the format A,B,C:
1234,1234 A Street,CP
5678,5678 B Street,HW
9012,9012 C Street,CP

Then I will build a simple GUI with 1 button per line (caption will be first parameter) and a two text captions on the right side of the button (Address and address type). The GUI can be build while reading.

1 - I can read the line into Word-Array with no issue.
2 - I know it is StrSplit(Word_Array[Line],",") that I need to use to split the string, but I was never able to make it work right.

Code: Select all

Word_Array 	:= []
Line 		        := 1
MaxLineArray 	:= 0
Loop
	{
	FileReadLine, Str1, E:\Input1.txt, %Line%
    	if ErrorLevel = 1
        	Break
	Word_Array[Line] := Str1     ;<----- problem one: StrSplit(Word_Array[Line],",")  here but I do not know how to set it up

	Line++
	}   ;<--- I assumje this end loop will be brought down somewhere to dynamically generate the buttons.

; Problem 2: How do I generate the code between the ;***** to generate the buttons? I do not care if the window is too big. I will give it space to generate 20 buttons that will be the maximum.

	Gui, New
	;Gui, Add, Picture, x1 y1 w300 h420 vMyPicture, %CPath%Writing.jpg

;*****
	Gui, Add, Button, x320 y100 vOpt1, Button1 
	Gui, Add, text,   x380 y100 vSometext1, Text here
	Gui, Add, text,   x400 y100 vSometext2, Text here
;*****

	Gui, font, s14, Arial
	Gui, Add, Button, x590 y550 Default gOK, OK
	Gui, Add, Button, x645 y550 Default gCancel, Cancel
	Gui, Show
	return
	OK:
	Gui, Submit

;My code will be here

	Cancel:
   	Gui, Hide
[Mod edit: [code][/code] tags added.]
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Breaking my teeth loading from flat file

17 May 2021, 06:45

Hi,

Code: Select all

Max_Lines := 20
Line_Index := 0
Line_Array := []
Loop, Read, E:\Input1.txt
	If (A_LoopReadLine <> "")
		Line_Array[++Line_Index] := StrSplit(A_LoopReadLine, ",")
Until (Line_Index = Max_Lines)

Code: Select all

Y := 100
For Index, Words In Line_Array
{
	Gui, Add, Button, x320 y%Y% vOpt%Index% , % Words[1]
	Gui, Add, Text, x380 yp vAddr%Index%,  % Words[2]
	Gui, Add, Text, x400 yp vType%Index%,  % Words[3]
	Y += 30
}
Not tested!
kestak
Posts: 40
Joined: 15 Aug 2018, 06:35

Re: Breaking my teeth loading from flat file

17 May 2021, 07:29

Thank you very much! It works! :bravo: It looks so simple once I see the solution. I was able to adapt what you wrote easily.

Now, I need to be able to do 2 more things. I found out with the other code I did, I just used the name of the button in a IF and associated code to it. Problem here, the name of the button is dynamic. So....

1 - When one button is pressed, how do I know which one is pressed and how can I simply msgbox the name of the button?
2 - When a button is pressed, the color of the button text needs to be changed to red for example.
I have no idea where to put that code that will If "button is pressed" msgbox Words[1] and change its color.

Here is the working code:

Code: Select all

Line_Array := []
Line_Index := 0

Loop, Read,  E:\Input1.txt
	{
	If (A_LoopReadLine <> "")
		Line_Array[++Line_Index] := StrSplit(A_LoopReadLine, ",")
	}

	Gui, New
	Gui, Add, Picture, x1 y1 w300 h420 vMyPicture, E:\Templates\Writing.jpg

	Y := 10
	For Index, Words In Line_Array
	{
	Gui, Add, Button, x320 y%Y% vOpt%Index% , % Words[1]
	Gui, Add, Text,   x370 yp   vAddr%Index%,  % Words[2]
	Gui, Add, Text,   x550 yp   vType%Index%,  % Words[3]
	Y += 32
	}


	Gui, font, s14, Arial
	Gui, Add, Button, x590 y550 Default gOK, OK
	Gui, Add, Button, x645 y550 Default gCancel, Cancel
	Gui, Show
	return

        ; I found out I do not really need a ok for now, but I will leave it here.
	OK:
	Gui, Submit

	Cancel:
   	Gui, Hide
Last edited by gregster on 17 May 2021, 10:06, edited 1 time in total.
Reason: [code] tags added. Please use them yourself, next time. Thank you!
just me
Posts: 9442
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Breaking my teeth loading from flat file

18 May 2021, 06:18

1.

Code: Select all

#NoEnv
Gui, Margin, 100, 20
Gui, Add, Button, w100 gBtnLabel vBtn1, Button 1
Gui, Add, Button, w100 gBtnLabel vBtn2, Button 2
Gui, Show, , Click Test
Return
GuiClose:
ExitApp
BtnLabel:
GuiControlGet, Caption, , %A_GuiCOntrol%
MsgBox, You clicked on button %A_GuiCOntrol% with caption %Caption%
Return
2.
It's not easy to change a button's text color on the fly. You might try to replace the buttons with text controls (with a g-label as shown in the example).

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 116 guests