Two GuiContextMenu in one Gui

Get help with using AutoHotkey and its commands and hotkeys
CarPiotr
Posts: 21
Joined: 11 Dec 2019, 08:21

Two GuiContextMenu in one Gui

24 Jul 2020, 08:34

Hi ! I was looking for examples but I couldn't manage. My Gui has two tabs where two different ListView are loaded from files. I added a GuiContextMenu for the first Listview but I also need another GuiContextMenu for the second. Please help

Code: Select all

Gui Add, Tab3, x10 y36 w320 h250 +Buttons , TEST1||TEST2
Gui Tab, 1
Gui, Add, ListView,r9 w300 +Grid vComm1 gComm1, Nb| Command | Parameters        | Description
Menu, ME1, Add, Edit1, ME1
Loop,Read,Command1.txt

{
	StringSplit, var_,A_LoopReadLine,%A_TAB%

	LV_Add("",var_1,var_2,var_3,var_4)
}
Gui Tab, 2
Gui, Add, ListView,r9 w300 +Grid vComm2 gComm2, Nb| Command | Parameters        | Description
Menu, ME2, Add, Edit2, ME2
Loop,Read,Command2.txt

{
	StringSplit, var_,A_LoopReadLine,%A_TAB%

	LV_Add("",var_1,var_2,var_3,var_4)
}



Gui,Show
Return
GuiClose:
    ExitApp

Comm1:
Comm2:
if A_GuiEvent = DoubleClick

    LV_GetText(RowText, A_EventInfo, 1) 
	LV_GetText(RowText2, A_EventInfo, 2) 
	LV_GetText(RowText3, A_EventInfo, 3) 
	LV_GetText(RowText4, A_EventInfo, 4) 
	
MsgBox, % "Replacements made:`n" RowText " == " RowText2 " == " RowText3  " == " RowText4
Return

GuiContextMenu:
if A_GuiControl <> Comm1
 Return
Menu, ME1, Show, %A_GuiX%, %A_GuiY%
Return

;I can't help myself here

if A_GuiControl <> Comm2
    return
Menu, ME2, Show, %A_GuiX%, %A_GuiY%
Return

ME1:
Return

ME2:
Return
Attachments
Command2.txt
(58 Bytes) Downloaded 24 times
Command1.txt
(63 Bytes) Downloaded 23 times
digidings
Posts: 13
Joined: 22 Jan 2018, 17:04

Re: Two GuiContextMenu in one Gui

24 Jul 2020, 08:45

Try this:

Code: Select all

GuiContextMenu:
if (A_GuiControl = "Comm1")
    Menu, ME1, Show, %A_GuiX%, %A_GuiY%
else if (A_GuiControl = "Comm2")
    Menu, ME2, Show, %A_GuiX%, %A_GuiY%
Return
CarPiotr
Posts: 21
Joined: 11 Dec 2019, 08:21

Re: Two GuiContextMenu in one Gui

31 Jul 2020, 15:24

Hi
As usual, I got lost somewhere and I am asking for help
I don't know why double clicking on the first ListView will display rows from the second ListView?

Code: Select all

Gui Add, Tab3, x10 y36 w320 h250  , TEST1||TEST2
Gui Tab, 1
Gui, Add, ListView,r9 w300 +Grid vComm1 gComm1,  Nb| Command | Parameters| Description
Menu, ME1, Add, Edit1, ME1
Loop,Read,Command1.txt

{
StringSplit, var_,A_LoopReadLine,%A_TAB%
LV_Add("",var_1,var_2,var_3,var_4)
}

Gui Tab, 2
Gui, Add, ListView,r9 w300 +Grid vComm2 gComm2,  Nb| Command | Parameters| Description
Menu, ME2, Add, Edit2, ME2
Loop,Read,Command2.txt

{
StringSplit, var_,A_LoopReadLine,%A_TAB%
LV_Add("",var_1,var_2,var_3,var_4)
}

Gui,Show
Return
GuiClose:
ExitApp


Comm1:


if (A_GuiEvent = "DoubleClick")

    LV_GetText(RowText1, A_EventInfo, 1) 
	LV_GetText(RowText2, A_EventInfo, 2) 
	LV_GetText(RowText3, A_EventInfo, 3) 
	LV_GetText(RowText4, A_EventInfo, 4) 
	
MsgBox, %  RowText1 " == " RowText2 " == " RowText3  " == " RowText4
Return

Comm2:

if (A_GuiEvent = "DoubleClick")

    LV_GetText(RowText1, A_EventInfo, 1) 
	LV_GetText(RowText2, A_EventInfo, 2) 
	LV_GetText(RowText3, A_EventInfo, 3) 
	LV_GetText(RowText4, A_EventInfo, 4) 
	
MsgBox, %  RowText1 " == " RowText2 " == " RowText3  " == " RowText4
Return


GuiContextMenu:
if (A_GuiControl = "Comm1")
    Menu, ME1, Show, %A_GuiX%, %A_GuiY%
else if (A_GuiControl = "Comm2")
    Menu, ME2, Show, %A_GuiX%, %A_GuiY%
Return

ME1:
Return

ME2:
Return
digidings
Posts: 13
Joined: 22 Jan 2018, 17:04

Re: Two GuiContextMenu in one Gui

31 Jul 2020, 18:15

select the right Listview like this:

Code: Select all

Comm1:
Gui, ListView, Comm1
if (A_GuiEvent = "DoubleClick")
LV_GetText(RowText1, A_EventInfo, 1) 
; ... 
return

Comm2:
Gui, ListView, Comm2
if (A_GuiEvent = "DoubleClick")
LV_GetText(RowText1, A_EventInfo, 1) 
; ... 
return
or - if same behaviour is an option - you can do it for both:

Code: Select all

Comm1:
Comm2:
Gui, ListView, %A_GuiControl%
if (A_GuiEvent = "DoubleClick") {
	LV_GetText(RowText1, A_EventInfo, 1) 
	LV_GetText(RowText2, A_EventInfo, 2) 
	LV_GetText(RowText3, A_EventInfo, 3) 
	LV_GetText(RowText4, A_EventInfo, 4) 
	
	MsgBox, %  RowText1 " == " RowText2 " == " RowText3  " == " RowText4
}
return
CarPiotr
Posts: 21
Joined: 11 Dec 2019, 08:21

Re: Two GuiContextMenu in one Gui

03 Aug 2020, 16:28

Hello !
I have come to this point and I have to ask for help. I want to edit the rows and enter new values after closing the Edit Gui. I want to save the changes to the files after closing the main Gui. Please help

Code: Select all

Gui Add, Tab3, x10 y36 w320 h250  , TEST1||TEST2
Gui Tab, 1
Gui, Add, ListView,r9 w300 +Grid vComm1 gComm1,  Nb| Command | Parameters| Description
Menu, ME1, Add, Edit1, ME1
Loop,Read,Command1.txt

{
StringSplit, var_,A_LoopReadLine,%A_TAB%
LV_Add("",var_1,var_2,var_3,var_4)
}

Gui Tab, 2
Gui, Add, ListView,r9 w300 +Grid vComm2 gComm2,  Nb| Command | Parameters| Description
Menu, ME2, Add, Edit2, ME2
Loop,Read,Command2.txt

{
StringSplit, var_,A_LoopReadLine,%A_TAB%
LV_Add("",var_1,var_2,var_3,var_4)
}

Gui,Show
Return
GuiClose:
ExitApp


Comm1:

Gui, ListView, Comm1

if (A_GuiEvent = "DoubleClick")

    LV_GetText(RowText1, A_EventInfo, 1) 
	LV_GetText(RowText2, A_EventInfo, 2) 
	LV_GetText(RowText3, A_EventInfo, 3) 
	LV_GetText(RowText4, A_EventInfo, 4) 
	
MsgBox, %  RowText1 " == " RowText2 " == " RowText3  " == " RowText4
Return

Comm2:

Gui, ListView, Comm2

if (A_GuiEvent = "DoubleClick")

    LV_GetText(RowText1, A_EventInfo, 1) 
	LV_GetText(RowText2, A_EventInfo, 2) 
	LV_GetText(RowText3, A_EventInfo, 3) 
	LV_GetText(RowText4, A_EventInfo, 4) 
	
MsgBox, %  RowText1 " == " RowText2 " == " RowText3  " == " RowText4
Return


GuiContextMenu:
if (A_GuiControl = "Comm1")
    Menu, ME1, Show, %A_GuiX%, %A_GuiY%
else if (A_GuiControl = "Comm2")
    Menu, ME2, Show, %A_GuiX%, %A_GuiY%
Return

ME1:

Gui, ListView, Comm1
RowNumber := LV_GetNext(0)
    Loop, 5
    LV_GetText(RowText%A_Index%, RowNumber,A_Index)
    
    GoSub,SMGUI2
    
return

SMGUI2:
Gui,1:+Disabled
     Gui,2:Color, 0x3299CC, 0xADEAEA,
     Gui,2:Add,Text, xm Section w150 , Nb
     Gui,2:Add,Edit, ys-4 w220  vA11, %RowText1%
     Gui,2:Add,Text, xm Section w150, Command
     Gui,2:Add,Edit, ys-4 w220  vA12, %RowText2%
     Gui,2:Add,Text, xm Section w150, Parameters
     Gui,2:Add,Edit, ys-4 w220 vA13, %RowText3%
     Gui,2:Add,Text, xm Section w150, Description
     Gui,2:Add,Edit, ys-4 w220 vA14, %RowText4%
     Gui,2:Add, Button, xm w50 h25 +Default -Theme gOK1, OK
     Gui,2:Show, , Command Editor List 1
return

2GuiEscape:
2GuiClose:
   Gui,2: Destroy
   Gui,1: Show
   Gui,1: -Disabled
   Gui,1:Default
return     

OK1:

   Gui,2: Destroy
   Gui,1: Show
   Gui,1: -Disabled
   Gui,1:Default
   
  ;I need help here 
   
Return



ME2:

Gui, ListView, Comm2

RowNumber := LV_GetNext(0)
    Loop, 5
    LV_GetText(RowText%A_Index%, RowNumber,A_Index)
    
    GoSub,SMGUI3
    
return

SMGUI3:
Gui,1:+Disabled
     Gui,3:Color, 0x3299CC, 0xADEAEA,
     Gui,3:Add,Text, xm Section w150 , Nb
     Gui,3:Add,Edit, ys-4 w220  vA11, %RowText1%
     Gui,3:Add,Text, xm Section w150, Command
     Gui,3:Add,Edit, ys-4 w220  vA12, %RowText2%
     Gui,3:Add,Text, xm Section w150, Parameters
     Gui,3:Add,Edit, ys-4 w220 vA13, %RowText3%
     Gui,3:Add,Text, xm Section w150, Description
     Gui,3:Add,Edit, ys-4 w220 vA14, %RowText4%
     Gui,3:Add, Button, xm w50 h25 +Default -Theme gOK2, OK
     Gui,3:Show, , Command Editor List 2
return

3GuiEscape:
3GuiClose:
   Gui,3: Destroy
   Gui,1: Show
   Gui,1: -Disabled
   Gui,1:Default
return  

OK2:

  Gui,2: Destroy
   Gui,1: Show
   Gui,1: -Disabled
   Gui,1:Default
   
   ;I need help here 

Return
CarPiotr
Posts: 21
Joined: 11 Dec 2019, 08:21

Re: Two GuiContextMenu in one Gui

05 Aug 2020, 15:40

Hello
I know that maybe I exaggerated a bit asking for help, but I'm a beginner, I don't know any programming language and there are also some language problems. I base everything on examples and I don't understand a lot. Therefore, I am asking for help only in modifying the rows in ListView.
CarPiotr
Posts: 21
Joined: 11 Dec 2019, 08:21

Re: Two GuiContextMenu in one Gui

09 Aug 2020, 01:27

Hi
I found a script on the forum that, after a slight modification, I could use for my project. Everything works fine, but I have one visual problem. I don't know where and if it is possible to change the length of the edit fields in the Add and Modify Gui at all. I would like all fields to be longer. Please reply.

Code: Select all

gui, add, listview, w310 vLV1 gLVEvents BackgroundWhite Grid, Id|A|B|C|D

loop, 5
LV_ModifyCol(a_index, 55)	;columns change columns width

loop, 4
LV_Add("", a_index)		;add rows

gui, add, button, xm gAdd, Add
gui, add, button, x+5 gEdit, Edit
gui, add, text, x+5, "Alternative: Double-Click rows to edit them!"

gui, show
return

LVEvents:	;_____________ LVEvents ________________

if (A_GuiEvent = "DoubleClick")
LV_AddEditRow(A_Gui, A_GuiControl, "Modify")

return

Edit:		;____________ Edit _______________

LV_AddEditRow(A_Gui, "LV1", "Modify")

return

Add:		;____________ Add _______________

LV_AddEditRow(A_Gui, "LV1", "Add")

return


guiclose:	;_____________ gui close _______________
exitapp


LV_AddEditRow(GuiWnd, LVCtrl, Status, Optns := "")	;_______________ Add/Edit ListView Controls Rows - v1.0 (Function) ___________________
{

;Local	;uncomment this line if you use AutoHotKey v1.1.27+
	;Force all variables in this function to be "Local" vars (with no exceptions)
	;"Force-local" mode (only supported on AutoHotKey v1.1.27+)
	;this function was tested in AutoHotKey 1.1.23.05

	;Local TempControlId	;this line was disabled because otherwise it would switch the function mode from "assume-Local" to "assume-Global" (makes all variables global by default)

Static ControlHwndId := [], CtrlType := [], CtrlHwnd := []		;declare the variable as an object and remember its values between function calls 

Static GuiWindow, LVControl, FocusedRow, TotalCols, Options, GuiHwnd	;"Static" variables, remember values between function calls 

if (Status = "GetVar")
return, (%LVCtrl%)		;return the values of static variables from this function

if (Status = "GetObject")	;return the values of static Objects from this function
return, %GuiWnd%[LVCtrl]

GuiWindow := GuiWnd
LVControl := LVCtrl

Options := Optns.Clone()
;"Clone()", Returns a shallow (not always a 100% clone???) copy of the object.
;"Clone()" creates a new object from the object referenced by "Optns" variable!
;then, "Options" variable will reference that newly created/cloned object!

Gui, %GuiWindow%:Default	;forces Built-in ListView Functions to operate upon the indicated gui window
Gui, ListView, % LVControl	;forces built-in ListView functions to operate upon the indicated Listview Control (the control must belong to the indicated gui window above)

FocusedRow := LV_GetNext(0, "Focused")
;Search for the focused row ("0", the search starts from row number 1!
;there is never more than one focused row in the entire list, and sometimes there is none at all!

	if (Status = "Modify" and FocusedRow = 0)
	{
	msgbox, 8240, Warning!, No row is selected! Please, select a row to be modified!
	;"8240" sum of 8192 and 48 (8192 for "Task Modal" option \ 48 for Icon Exclamation + sound)
	;"52" sum of 4 and 48 (4 for "yes - no" options \ 48 for Icon Exclamation + sound)

	gui, LVAddEditRow:Destroy

	return
	}

TotalCols := LV_GetCount("Col")		;the function returns the number of columns in the control

gui, LVAddEditRow:Default
gui, destroy
Gui +HwndGuiHwnd

	loop, % TotalCols
	{
	Gui, %GuiWindow%:Default			;forces Built-in ListView Functions to operate upon the indicated gui window
	LV_GetText(TempHeader, 0, a_index)		;"0" is the columns header row / "a_index" is the column number

	if (Status = "Modify")
	LV_GetText(TempText, FocusedRow, a_index)	;"a_index" is the column number

	gui, LVAddEditRow:Default

		TempIndex := a_index
		loop
		{
			if (Options["BeforeCell" TempIndex "Ctrl" a_index "Type"] = "")
			break
			else
			{
			gui, add, % Options["BeforeCell" TempIndex "Ctrl" a_index "Type"], % "+HwndTempControlId " Options["BeforeCell" TempIndex "Ctrl" a_index "Options"], % Options["BeforeCell" TempIndex "Ctrl" a_index "Text"]

			TempKey := Options["BeforeCell" TempIndex "Ctrl" a_index "Hwnd"]
			CtrlHwnd[TempKey] := TempControlId
			}
		}

	gui, add, text, % Options["Cell" a_index "HeaderOptions"], % TempHeader

		;Ternary operator, ( ? = if ) and ( : = else )

	ControlType := ControlOption := ControlText := ""	;blank\empty values

	ControlType := CtrlType[a_index] := Options["Cell" a_index "Type"] != "" ? Options["Cell" a_index "Type"] : "Edit"

		if (ControlType = "Checkbox")
		{
		ControlOption := Status = "Add" ? "" : "Checked" TempText
		}
		else if (ControlType = "DateTime")
		{
		TempText := TempText = "" ? "None" : RegExReplace(TempText, "\D")	;"\D" means any non-digit (remove any non-digit characters)
		ControlOption := Status = "Add" ? "" : "Choose" TempText
		}
		else if (ControlType = "ListBox" or ControlType = "DropDownList" or ControlType = "DDL")
		{
			if (Status = "Modify")
			{
			TempIndex := a_index
			TempList := Options["Cell" TempIndex "Text"], Options["Cell" TempIndex "Text"] := ""

				loop, parse, TempList, |
				{
				if (A_LoopField == "")		;if blank\empty (== is case-sensitive)
				continue			;Skips the rest of the current loop iteration and begins a new one. Valid inside any kind of loop.

				TempString := A_LoopField

					loop, parse, TempText, |
					{
						if ("S" TempString == "S" A_LoopField)		;"==" is case-sensitive, "S" forces "string" comprisons and avoids "number" comparisons!
						{
						TempString .= "|"
						break
						}

					}

				Options["Cell" TempIndex "Text"] .= TempString "|"
				}
			}
		}
		else if (ControlType != "Edit" and ControlType != "ComboBox")
		{
		if (ControlType = "MonthCal")
		TempText := RegExReplace(TempText, "\Q/")	;remove any "/" character

		if (Status = "Modify")
		ControlText := TempText, Options["Cell" a_index "Text"] := ""
		}

	gui, add, % ControlType, % "+HwndTempControlId " Options["Cell" a_index "Options"] " " ControlOption, % Options["Cell" a_index "Text"] ControlText

		if (Status = "Modify")
		{
		if (ControlType = "Edit" or ControlType = "ComboBox")
		ControlSetText, , % TempText, % "ahk_id" TempControlId		;"ControlSetText" prevents "EOL" (End of LIne Translation), no "`n" is translated to "`r`n"
		}

	ControlHwndId[a_index] := TempControlId		;the variable must be declared as an object first ("ControlHwndId := []")
	
	TempKey := Options["Cell" a_index "Hwnd"]
	CtrlHwnd[TempKey] := TempControlId

		TempIndex := a_index
		loop
		{
			if (Options["AfterCell" TempIndex "Ctrl" a_index "Type"] = "")
			break
			else
			{
			if (a_index = "1" and Status = "Modify" and Options["AfterCell" TempIndex "Ctrl" a_index "Type"] = "UpDown")
			Options["AfterCell" TempIndex "Ctrl" a_index "Text"] := RegExReplace(TempText, "\.|,")		;Regex removes thousands separator (. or ,)

			gui, add, % Options["AfterCell" TempIndex "Ctrl" a_index "Type"], % "+HwndTempControlId " Options["AfterCell" TempIndex "Ctrl" a_index "Options"], % Options["AfterCell" TempIndex "Ctrl" a_index "Text"]

			TempKey := Options["AfterCell" TempIndex "Ctrl" a_index "Hwnd"]
			CtrlHwnd[TempKey] := TempControlId
			}
		}

	}

TempText := ""	 ;make variable blank (free memory)

if (Status = "Modify")
GuiCaption := ButtonCaption := "Modify"
else
GuiCaption := ButtonCaption := "Add"

gui, add, button, xm gLVAddEditRowLabel Default, % ButtonCaption

	if (Status = "Modify")
	{
	gui, add, button, x+5 gLVAddEditRowLabel , Add
	gui, add, button, x+5 gLVAddEditRowLabel , Delete
	}

gui, add, button, x+5 gLVAddEditRowGuiClose, Cancel

gui, show, , % GuiCaption

return

LVAddEditRowLabel:	;_________ Function Label __________

Gui, %GuiWindow%:Default	;forces Built-in ListView Functions to operate upon the indicated gui window
Gui, ListView, % LVControl	;forces built-in ListView functions to operate upon the indicated Listview Control (the control must belong to the indicated gui window above)

	if (a_guicontrol = "Delete")
	{
	LV_Delete(FocusedRow)
	FocusedRow := LV_GetNext(0, "Focused")		;Search for the focused row ("0", the search starts from row number 1!
	}

	if (a_guicontrol = "Add" or a_guicontrol = "Modify")
	{
		if (a_guicontrol = "Add")
		{
		FocusedRow := LV_Insert(Options["NewRowIndex"] = "" ? LV_GetCount() + 1 : Options["NewRowIndex"], Options["NewRowOptions"])
		;The function adds a new row to the specified index position (if the ListView control does not have the "Sort" or "SortDesc" style)
		;The function returns the new row number, which is not necessarily the specified index position if the ListView has the "Sort" or "SortDesc" style.
		;Ternary operator, (? = if) and (: = else)
		;if Options["NewRowIndex"] is blank\empty, the new row will be inserted to the end of the listview control, "LV_GetCount() + 1"
		;for some reason, the Options["NewRowOptions"] prevents Autohotkey from crashing if Options["NewRowIndex"] is blank/empty and the ListView control contains "Sort" or "SortDesc" options
		}

		if (a_guicontrol = "Modify")
		{
		LV_Modify(FocusedRow, Options["RowOptions"])
		}

		loop, % TotalCols
		{
		if (CtrlType[a_index] = "Edit" or CtrlType[a_index] = "ComboBox")
		ControlGetText, TempText, , % "ahk_id" ControlHwndId[a_index]		;"ControlGetText" prevents EOL "End of line translation", no "`r`n" is translated to "`n"
		else
		GuiControlGet, TempText, LVAddEditRow:, % ControlHwndId[a_index]

			if (CtrlType[a_index] = "DateTime")
			{
			if (TempText != "")
			FormatTime, TempText, % TempText, yyyy-MM-dd/HH:mm:ss		;"HH" 24-hour format (00 – 23)
			}
			else if (CtrlType[a_index] = "MonthCal")
			{
				if (TempText != "")
				{
					Loop, Parse, TempText, -
					{
					TempText := a_index = 1 ? "" : TempText

					FormatTime, TempTime, % A_LoopField, yyyy/MM/dd		;"MonthCal" does not retrieve  "HH24MISS" time portion , retrieves only "YYYYMMDD" Date

					TempText .= TempTime "-"
					}

				TempText := RegExReplace(TempText, "-$")	;remove the last "-" character at the end of the string
				}
			}

		LV_Modify(FocusedRow, "Col" a_index, TempText)
		}
	}

LV_Modify(0, "-Select")		;Deselect all rows

LV_Modify(FocusedRow, "Focus Select Vis")
;"vis", Ensures that the specified row is completely visible by scrolling the ListView, if necessary.
;"Vis", has an effect only for "LV_Modify()" function! (does not work with "LV_Add()" function!)
;"Focus", Sets keyboard focus to the row / "Select", highlight the row

LVAddEditRowGuiClose:	;_______ LVAddEditRow Gui Close _______
LVAddEditRowGuiEscape:

Gui, %GuiWindow%:Default	;forces Built-in ListView Functions to operate upon the indicated gui window
Gui, ListView, % LVControl	;forces built-in ListView functions to operate upon the indicated Listview Control (the control must belong to the indicated gui window above)

GuiControl, Focus, % A_DefaultListView		;"A_DefaultListView" supported from [v1.1.23+]

if (Options["GoSub"] != "")	;if not blank/empty
GoSub, % Options["GoSub"]	;"GoTo" is not allowed inside functions, only "GoSub" is!

TempText := "", ControlHwndId := [], CtrlType := [], CtrlHwnd := [], Options := []	 ;make variable blank (free memory)

gui, LVAddEditRow:Destroy	;by destroying the gui window, "a_guicontrol" will be made blank\empty!

return
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], mabohango, mikeyww, PlumpSamurai, teadrinker and 60 guests