error same variable for more than one control

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

error same variable for more than one control

Post by smbs » 16 Sep 2021, 11:19

I want to double click a letter in letter gui the clicked letter shall appear in search box and filename box should be filtered accordingly
any help would be great

Code: Select all

;;;adopted from https://www.autohotkey.com/boards/viewtopic.php?t=14205
#NoEnv
#SingleInstance force
File2 := "
(Join`r`n
a
b
c
d
)"
File := "
(Join`r`n
abc
apple
bbc
boat
bowl
cat
cotten
coat
dig
dog
)"
SetBatchLines, -1


Gui, Add, ListView, r20 w120 gMyListView, letters


Loop, Parse, File2, `n, `r
{

	 CurrentRow := LV_Add("", A_LoopField)
}

LV_ModifyCol()  ; Auto-size each column to fit its contents.




 MyListView:
 if (A_GuiEvent = "DoubleClick")
 {
     LV_GetText(RowText, A_EventInfo)  ; Get the text from the row's first field.
       
 }
 
LVArray := {}
Gui, Add, Text, ,Search:
Gui, Add, Edit, w120 vSearchTerm gSearch
Gui, Add, ListView, grid r20 w120 vLV gLVGO +altsubmit, FileName 
Loop, Parse, File, `n, `r
{

      
	  
	   LV_Add("", A_Loopfield)  ;,a_loopfilefullpath)
    LVArray.Push({1:A_LoopField}) ;,2:a_loopfilefullpath})
}



TotalItems := LVArray.Length()
LV_ModifyCol()
Gui, Add, StatusBar, , % "   " . TotalItems . " of " . TotalItems . " Items"
Gui, Show,x1750 y0 , Listview

Return

GuiClose1:
ExitApp
Search:
GuiControlGet, SearchTerm
GuiControl, -Redraw, LV
LV_Delete()
For Each, FileName In LVArray
{
   If (SearchTerm != "")
   {
      If (InStr(FileName.1, SearchTerm) = 1) ; for matching at the start
      ; If InStr(FileName, SearchTerm) ; for overall matching
         LV_Add("", FileName.1, FileName.2)
   }
   Else
      LV_Add("", FileName.1,Filename.2)
}
Items := LV_GetCount()
SB_SetText("   " . Items . " of " . TotalItems . " Items")
GuiControl, +Redraw, LV
Return

LVGo:
Gui,1:default
rcon:=a_guicontrol
Gui,1:ListView,%rcon%
  RN:=LV_GetNext("C")
  RF:=LV_GetNext("F")
  GC:=LV_GetCount()
  if (rn=0)
    return
;-----------------   
if A_GuiEvent = DoubleClick
  {
  LV_GetText(C1,a_eventinfo,1)
msgbox, % c1
}

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: error same variable for more than one control

Post by flyingDman » 16 Sep 2021, 13:10

Make sure you are identifying the listviews (see my "note <<<<" comments). Also, set up your listviews and other controls and end with a return. The all labels should be below that. See if this works for you:

Code: Select all

File =
(Join`r`n
abc
apple
bbc
boat
bowl
cat
cotten
coat
dig
dog
)

File2 =
(Join`r`n
a
b
c
d
)

LVArray := []
Gui, Add, ListView, grid r20 w120 vLV0 gchg, Letters
Loop, Parse, File2, `n, `r
	LV_Add("", A_LoopField)
Gui, Add, Text, ,Search:
Gui, Add, Edit, w120 vSearchTerm gSearch
Gui, Add, ListView, grid r20 w120 vLV, FileName
Loop, Parse, File, `n, `r
	{
	LV_Add("", A_LoopField)
	LVArray.Push(A_LoopField)
	}
TotalItems := LVArray.Length()
LV_ModifyCol()
Gui, Add, StatusBar, , % TotalItems . " Items"
Gui, Show, , ListView
Return

chg:
gui, listview, SysListView321                     ; note <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if (A_GuiEvent = "DoubleClick")
     LV_GetText(RowText, A_EventInfo)	          ; Get the text from the row's first field.
guicontrol, ,SearchTerm, % RowText
return

Search:
GuiControlGet, SearchTerm
gui, listview, SysListView322					  ; note <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
GuiControl, -Redraw, LV
LV_Delete()
For Each, FileName In LVArray
	{
	   If (SearchTerm != "")
	   {
		  If (InStr(FileName, SearchTerm) = 1)    ; for matching at the start
		  ; If InStr(FileName, SearchTerm)        ; for overall matching
			 LV_Add("", FileName)
	   }
	   Else
		  LV_Add("", FileName)
	}
Items := LV_GetCount()
SB_SetText("   " . Items . " of " . TotalItems . " Items")
GuiControl, +Redraw, LV
Return

GuiClose:
ExitApp
14.3 & 1.3.7

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: error same variable for more than one control

Post by smbs » 16 Sep 2021, 13:38

Brilliant!!
Much appreciated
Will try to understand it a bit better later by going thru the docs. At the moment I dont really understand except that you differentiate between guis
by SysListView321/322
Will come back if I have questions
Once again many thanx!

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: error same variable for more than one control

Post by smbs » 17 Sep 2021, 09:17

@FlyingDman
I would like to use enter instead of mouse "doubleclick' I have read and found post viewtopic.php?t=31730
Tried to follow it but have failed.
Would really appreciate your help again--maybe it is possible to have both "enter" or "double click" working in same script!
Thanx again

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: error same variable for more than one control

Post by flyingDman » 17 Sep 2021, 10:19

You can follow the method in the post you referred to, or the method shown in the help file for Listview under Remarks. In both cases, you need to create a hidden "default" button. The former is shorter. And, yes, you can have your cake and eat it too:

Code: Select all

File =
(Join`r`n
abc
apple
bbc
boat
bowl
cat
cotten
coat
dig
dog
)

File2 =
(Join`r`n
a
b
c
d
)

LVArray := []
Gui, Add, ListView, grid r20 w120 vLV0 gchg, Letters
Loop, Parse, File2, `n, `r
	LV_Add("", A_LoopField)
Gui, Add, Text, ,Search:
Gui, Add, Edit, w120 vSearchTerm gSearch
Gui, Add, ListView, grid r20 w120 vLV, FileName
Loop, Parse, File, `n, `r
	{
	LV_Add("", A_LoopField)
	LVArray.Push(A_LoopField)
	}
TotalItems := LVArray.Length()
LV_ModifyCol()
Gui, Add, Button, Hidden Default, OK
Gui, Add, StatusBar, , % TotalItems . " Items"
Gui, Show, , ListView
Return

ButtonOK:
gui, listview, SysListView321                     ; note <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
ControlGet, rw, List, Count Focused, SysListView321, A
LV_GetText(RowText, rw,1)
guicontrol, ,SearchTerm, % RowText
return

chg:
gui, listview, SysListView321                     ; note <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if (A_GuiEvent = "DoubleClick")
     LV_GetText(RowText, A_EventInfo)	          ; Get the text from the row's first field.
guicontrol, ,SearchTerm, % RowText
return

Search:
GuiControlGet, SearchTerm
gui, listview, SysListView322					  ; note <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
GuiControl, -Redraw, LV
LV_Delete()
For Each, FileName In LVArray
	{
	   If (SearchTerm != "")
	   {
		  If (InStr(FileName, SearchTerm) = 1)    ; for matching at the start
		  ; If InStr(FileName, SearchTerm)        ; for overall matching
			 LV_Add("", FileName)
	   }
	   Else
		  LV_Add("", FileName)
	}
Items := LV_GetCount()
SB_SetText("   " . Items . " of " . TotalItems . " Items")
GuiControl, +Redraw, LV
Return
14.3 & 1.3.7

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: error same variable for more than one control

Post by smbs » 17 Sep 2021, 15:11

Thanx very much slowly getting there thanx for your patience!-Pls see code below I need to add file name chosen to clipboard-it is as if I need another label ButtonOK: for SysListView323
How is it done?

Code: Select all

File =
(Join`r`n
abc
apple
bbc
boat
bowl
cat
cotten
coat
dig
dog
)

File2 =
(Join`r`n
a
b
c
d
)

LVArray := []
Gui, Add, ListView, grid r20 w120 vLV0 gchg, Letters
Loop, Parse, File2, `n, `r
	LV_Add("", A_LoopField)
Gui, Add, Text, ,Search:
Gui, Add, Edit, w120 vSearchTerm gSearch
Gui, Add, ListView, grid r20 w120 vLV gchg1, FileName  ; added new gch1 see at bottom
Loop, Parse, File, `n, `r
	{
	LV_Add("", A_LoopField)
	LVArray.Push(A_LoopField)
	}
TotalItems := LVArray.Length()
LV_ModifyCol()
Gui, Add, Button, Hidden Default, OK
Gui, Add, StatusBar, , % TotalItems . " Items"
Gui, Show, , ListView
Return

ButtonOK:
gui, listview, SysListView321                     ; note <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
ControlGet, rw, List, Count Focused, SysListView321, A
LV_GetText(RowText, rw,1)
guicontrol, ,SearchTerm, % RowText
return

chg:
gui, listview, SysListView321                     ; note <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if (A_GuiEvent = "DoubleClick")
     LV_GetText(RowText, A_EventInfo)	          ; Get the text from the row's first field.
guicontrol, ,SearchTerm, % RowText
return

Search:
GuiControlGet, SearchTerm
gui, listview, SysListView322					  ; note <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
GuiControl, -Redraw, LV
LV_Delete()
For Each, FileName In LVArray
	{
	   If (SearchTerm != "")
	   {
		  If (InStr(FileName, SearchTerm) = 1)    ; for matching at the start
		  ; If InStr(FileName, SearchTerm)        ; for overall matching
			 LV_Add("", FileName)
	   }
	   Else
		  LV_Add("", FileName)
	}
Items := LV_GetCount()
SB_SetText("   " . Items . " of " . TotalItems . " Items")
GuiControl, +Redraw, LV
Return
chg1:                                             ; added this but I want to use "enter" not "doubleclick"
gui, listview, SysListView323                     ; 
if (A_GuiEvent = "DoubleClick")
     LV_GetText(RowText, A_EventInfo)	          ; 
	 msgbox, % RowText
	 clipboard:= rowtext

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: error same variable for more than one control

Post by flyingDman » 17 Sep 2021, 15:46

When you press Enter and trigger the ButtonOK label, you need to figure out which of the 2 listviews has the focus, and determine the action based on that. Use ControlGetFocus to do that. Don't introduce SysListView323 because there is no 3rd ListView. Let me know if this works for you:

Code: Select all

File =
(Join`r`n
abc
apple
bbc
boat
bowl
cat
cotton
coat
dig
dog
)

File2 =
(Join`r`n
a
b
c
d
)

LVArray := []
Gui, Add, ListView, grid r20 w120 vLV0 gchg, Letters
Loop, Parse, File2, `n, `r
	LV_Add("", A_LoopField)
Gui, Add, Text, ,Search:
Gui, Add, Edit, w120 vSearchTerm gSearch
Gui, Add, ListView, grid r20 w120 vLV gchg1, FileName  ; added new gch1 see at bottom
Loop, Parse, File, `n, `r
	{
	LV_Add("", A_LoopField)
	LVArray.Push(A_LoopField)
	}
TotalItems := LVArray.Length()
LV_ModifyCol()
Gui, Add, Button, Hidden Default, OK
Gui, Add, StatusBar, , % TotalItems . " Items"
Gui, Show, , ListView
Return

ButtonOK:
ControlGetFocus, ctrlfcs
if (ctrlfcs = "SysListView321")
	{
	gui, listview, SysListView321                 ; note <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	ControlGet, rw, List, Count Focused, SysListView321, A
	LV_GetText(RowText, rw,1)
	guicontrol, ,SearchTerm, % RowText
	}
if (ctrlfcs = "SysListView322")
	{
	gui, listview, SysListView322                 ; note <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
	ControlGet, rw, List, Count Focused, SysListView322, A
	LV_GetText(RowText, rw,1)
	msgbox % clipboard:= RowText 
	}
return

chg:
gui, listview, SysListView321                     ; note <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if (A_GuiEvent = "DoubleClick")
     LV_GetText(RowText, A_EventInfo)	          ; Get the text from the row's first field.
guicontrol, ,SearchTerm, % RowText
return

chg1:                                             ; added this but I want to use "enter" not "doubleclick" (you'll have both!)
;~ gui, listview, SysListView322                  ; uncomment if needed
if (A_GuiEvent = "DoubleClick")
     LV_GetText(RowText, A_EventInfo)	          ; 
msgbox, % clipboard:= RowText 
return 

Search:
GuiControlGet, SearchTerm
gui, listview, SysListView322					  ; note <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
GuiControl, -Redraw, LV
LV_Delete()
For Each, FileName In LVArray
	{
	   If (SearchTerm != "")
	   {
		  If (InStr(FileName, SearchTerm) = 1)    ; for matching at the start
		  ; If InStr(FileName, SearchTerm)        ; for overall matching
			 LV_Add("", FileName)
	   }
	   Else
		  LV_Add("", FileName)
	}
Items := LV_GetCount()
SB_SetText("   " . Items . " of " . TotalItems . " Items")
GuiControl, +Redraw, LV
Return
14.3 & 1.3.7

smbs
Posts: 98
Joined: 27 Feb 2014, 11:07

Re: error same variable for more than one control

Post by smbs » 17 Sep 2021, 16:00

Again many thanx works like a charm--without mouse I use TAB to navigate listviews
Have a great weekend!

Post Reply

Return to “Ask for Help (v1)”