Switching toolgun mode w/o hiding context menu

I made a toolgun history window that shows up everytime you hold down ‘c’ (OnContextMenuOpen), but everytime i choose another toolgun mode from the history, i have to let go ‘c’ and hold it down again in order to get the new context menu.

Is there any better away aroundd this apart from my “hack”?

[lua]–Redraw context menu (let’s just hope they won’t let go the ‘c’ button this frame).
RunConsoleCommand("-menu_context")
timer.Simple(0.15, RunConsoleCommand, “+menu_context”)[/lua]

This is the whole script:
[lua]if SERVER then return end

require(“glon”)

–these are the only 3 global variables i have, don’t worry
drgnvch_toolhistory = {}
drgnvch_toolhistory_panel = nil
drgnvch_toolhistory_panel_list = nil
–load our last saved session
if file.Exists(“drogenviech/drgnvch_toolhistory.txt”) then
drgnvch_toolhistory = glon.decode(file.Read(“drogenviech/drgnvch_toolhistory.txt”)) or {}
end

local toolhistory_enabled = CreateClientConVar(“toolhistory_enabled”, 1, true, false)
local toolhistory_x = CreateClientConVar(“toolhistory_xpos”, 0, true, false)
local toolhistory_y = CreateClientConVar(“toolhistory_ypos”, 0, true, false)

local function toolhistory_addtool()
local tool = GetConVarString(“gmod_toolmode”)
if table.HasValue(drgnvch_toolhistory, tool) then return end

if table.getn(drgnvch_toolhistory) > 10 then
	for i=11, table.getn(drgnvch_toolhistory) do
		table.remove(drgnvch_toolhistory, i)
	end
end

table.insert(drgnvch_toolhistory, 1, tool)

end

local function toolhistory_KeyPress(ply, key)
if (key == IN_ATTACK or key == IN_ATTACK2 or key == IN_RELOAD) and ValidEntity(LocalPlayer():GetActiveWeapon()) and LocalPlayer():GetActiveWeapon():GetClass() == “gmod_tool” then
toolhistory_addtool()
end
end
hook.Add(“KeyPress”, “toolhistory_KeyPress”, toolhistory_KeyPress)

local function toolhistory_ContextScreenClick(aimvec, mousecode, pressed, ply)
if (mousecode == MOUSE_LEFT or MOUSE_RIGHT) and ValidEntity(LocalPlayer():GetActiveWeapon()) and LocalPlayer():GetActiveWeapon():GetClass() == “gmod_tool” then
toolhistory_addtool()
end
end
hook.Add(“ContextScreenClick”,“toolhistory_ContextScreenClick”, toolhistory_ContextScreenClick)

local function toolhistory_createmenu()
drgnvch_toolhistory_panel = vgui.Create(“DFrame”)
if toolhistory_x:GetInt() ~= 0 or toolhistory_y:GetInt() ~= 0 then
if toolhistory_x:GetInt() >= ScrW()-22 or toolhistory_y:GetInt() >= ScrH()-22 then
drgnvch_toolhistory_panel:SetPos(0, 0)
RunConsoleCommand(“toolhistory_xpos”, 0)
RunConsoleCommand(“toolhistory_ypos”, 0)
else
drgnvch_toolhistory_panel:SetPos(toolhistory_x:GetInt(), toolhistory_y:GetInt())
end
else
drgnvch_toolhistory_panel:SetPos(ScrW() / 2 - 75 / 2, ScrH() - 238)
end
drgnvch_toolhistory_panel:SetSize(150, 218)
drgnvch_toolhistory_panel:SetTitle(“Tool History”)
drgnvch_toolhistory_panel:SetVisible(false)
drgnvch_toolhistory_panel:SetDraggable(true)
drgnvch_toolhistory_panel:ShowCloseButton(false)

drgnvch_toolhistory_panel_list = vgui.Create("DListView", drgnvch_toolhistory_panel)
drgnvch_toolhistory_panel_list:SetPos(2, 22)
drgnvch_toolhistory_panel_list:SetSize(146, 194)
drgnvch_toolhistory_panel_list:AddColumn("Tool")
drgnvch_toolhistory_panel_list:SetSortable(false)
drgnvch_toolhistory_panel_list:SetMultiSelect(false)
drgnvch_toolhistory_panel_list.OnClickLine = function(parent, line, isselected)
	RunConsoleCommand("tool_"..line:GetValue(1))

–Redraw context menu (let’s just hope they won’t let go the ‘c’ button this frame).
RunConsoleCommand("-menu_context")
timer.Simple(0.15, RunConsoleCommand, “+menu_context”)
end
end
timer.Simple(0, toolhistory_createmenu)

local function toolhistory_OnContextMenuOpen()
if toolhistory_enabled:GetBool() == true then
drgnvch_toolhistory_panel_list:Clear()

	for k,v in pairs(drgnvch_toolhistory) do
		drgnvch_toolhistory_panel_list:AddLine(v)
	end
	
	drgnvch_toolhistory_panel:SetVisible(true)
end

end
hook.Add(“OnContextMenuOpen”, “toolhistory_OnContextMenuOpen”, toolhistory_OnContextMenuOpen)

local function toolhistory_toolhistory_OnContextMenuClose()
if toolhistory_enabled:GetBool() == true then
local x, y = drgnvch_toolhistory_panel:GetPos()
RunConsoleCommand(“toolhistory_xpos”, x)
RunConsoleCommand(“toolhistory_ypos”, y)
drgnvch_toolhistory_panel:SetVisible(false)
end
end
hook.Add(“OnContextMenuClose”, “toolhistory_OnContextMenuClose”, toolhistory_toolhistory_OnContextMenuClose)

local function toolhistory_ShutDown()
if not file.IsDir(“drogenviech”) then
file.CreateDir(“drogenviech”)
end
file.Write(“drogenviech/drgnvch_toolhistory.txt”, glon.encode(drgnvch_toolhistory))
end
hook.Add(“ShutDown”, “toolhistory_ShutDown”, toolhistory_ShutDown)[/lua]

I don’t think you can, you would need to delete the contents of the control panel that the options are on or generate the new panel and then swap it. I made a custom spawn menu for a game i’m working on and I need to grab the Control Panel my self, i’ll post that code hope it helps.
I added some comments explaining some things, about what i’ve found.
[lua]

if LocalPlayer():GetActiveWeapon():IsValid() then
if LocalPlayer():GetActiveWeapon():GetClass() == “gmod_tool” then

	local curMode = LocalPlayer():GetActiveWeapon().current_mode
	if curMode == nil then return end
	spawnmenu.AddContext("Options", "ToolsOptions_" .. curMode, "Tools Options", "Tools Options Menu")  
	cInside = GetControlPanel("ToolsOptions_" .. curMode)
	cInside:ClearControls()
	cInside.VertPos = 5
	
	cInside:SetSize(275, 800)
	cInside:SetPos(ScrW() - 290, 0)

	-- Important, some tools have a BuildCPanel other's load from text in the settings folder
	if !(LocalPlayer():GetActiveWeapon():GetTable()['Tool'][curMode].BuildCPanel == nil) then
		LocalPlayer():GetActiveWeapon():GetTable()['Tool'][curMode].BuildCPanel(cInside)
	else
		toolContextFromFile(curMode, cInside)  -- This is part of panel but i couldn't make it work, but a copied version does, odd.
	end
	cInside:PerformLayout()				
	cInside:SetVisible(true)
	RestoreCursorPosition( )			
	if cInside.once == nil then
		cInside.once = true
		cInside:MakePopup()  -- Only make popup the first time
	end
end

end

– This is part of PANEL, but would not work when I tried it normaly it’s in /gamemodes/sandbox/gamemode/spawnmenu/controlpanel.lua
– It’s called from PANEL:FillViaTable and if that can’t find the buildcpanel function it calls PANEL:LoadControlsFromTextFile
– I tried calling FillViaTable and it didn’t work, also calling LoadControlsFromTextFile didn’t work, so I copied LoadControlsFromTextFile and
– I check for the BuildCPanel function myself.
function toolContextFromFile( strName, cp )

local file = file.Read( "settings/controls/"..strName..".txt", true )
if (!file) then return end
local Tab = KeyValuesToTablePreserveOrder( file )
if (!Tab) then return end

for k, data in pairs( Tab ) do
    if ( type( data.Value ) == "table" ) then
        local kv = table.CollapseKeyValue( data.Value )
        local ctrl = cp:AddControl( data.Key, kv )
        if ( ctrl && kv.description ) then
            ctrl:SetTooltip( kv.description );
        end
    end
    
end

end
[/lua]

You might be able to load two at a time and swap them, maybe.