Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Remove Controls from Form

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting
View previous topic :: View next topic  
Author Message
Rawing
Cheater
Reputation: 0

Joined: 01 May 2010
Posts: 42
Location: Austria

PostPosted: Tue May 12, 2015 10:41 am    Post subject: Remove Controls from Form Reply with quote

I'm trying to remove Controls (buttons, labels, etc...) from a Form. I wrote the following code:

Code:
function wipe_window()
    --for _, control in pairs(win.Control) do --the Control array is always empty, unexpectedly
    --    control:destroy()
    --end
    while win.ControlCount > 0 do
        local control= win:getControl(0)
        control:destroy()
    end
end


win= createForm()
control_setSize(win, 200, 200)
w= createButton(win)
control_setCaption(w, "wipe")
control_setPosition(w, 5, 5)
w.onClick= wipe_window


It does remove all Controls, but it also produces an "access violation" error or causes Cheat Engine to hang. What am I doing wrong?

_________________
Game over! Die again!
Back to top
View user's profile Send private message MSN Messenger
panraven
Grandmaster Cheater
Reputation: 55

Joined: 01 Oct 2008
Posts: 942

PostPosted: Tue May 12, 2015 11:48 am    Post subject: Reply with quote

It seems this work by using a callLater function:
Code:

local function callLater(d,f,...)
  local t,a,n = createTimer(),{...},select('#',...)
  t.Interval = d
  t.OnTimer = function(sender) sender.Destroy() f(unpack(a,1,n)) end
  t.Enabled = true
  return t
end

function wipe_window()
    for i=0,win.ControlCount-1 do -- can't use WHILE as controlCount is not updated
        local control= win:getControl(i)
        callLater(1,control.destroy)
    end
end

win= createForm()
control_setSize(win, 200, 200)
for i=1,5 do
  local w= createButton(win)
  control_setCaption(w, "wipe "..i)
  control_setPosition(w, 5, 5+i*25)
  w.onClick= wipe_window
end


Also this work without using callLater:
Code:
function wipe_window(sender) -- added the sender/owner parameter
    local next = 0
    while win.ControlCount > 1 do -- one button should left which is the one being clicked,ie. sender
        local control= win:getControl(next)
        if control ~= sender then control.destroy() else next = 1 end
    end
end

win= createForm()
control_setSize(win, 200, 200)
for i=1,5 do
  local w= createButton(win)
  control_setCaption(w, "wipe "..i)
  control_setPosition(w, 5, 5+i*25)
  w.onClick= wipe_window
end

... but this might not what you intended, as one child control is left.
This only to show that, if control.Destroy is calling from OnClick function of the exact control of owner/sender, it may be the cause of access violation.

Finally, still using callLater, but least modifying your code,
Code:
function wipe_window2()
    while win.ControlCount > 0 do
        local control= win:getControl(0)
        control.destroy()
    end
end

function wipe_window() callLater(1,wipe_window2) end
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 458

Joined: 09 May 2003
Posts: 25288
Location: The netherlands

PostPosted: Tue May 12, 2015 2:16 pm    Post subject: Reply with quote

don't destroy the caller
also, instead of destroy, set visible to false

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
Rawing
Cheater
Reputation: 0

Joined: 01 May 2010
Posts: 42
Location: Austria

PostPosted: Tue May 12, 2015 4:59 pm    Post subject: Reply with quote

Thanks for the help guys. I'll have to rewrite some parts of my code.
_________________
Game over! Die again!
Back to top
View user's profile Send private message MSN Messenger
DaSpamer
Grandmaster Cheater Supreme
Reputation: 52

Joined: 13 Sep 2011
Posts: 1578

PostPosted: Tue May 12, 2015 7:36 pm    Post subject: This post has 1 review(s) Reply with quote

Code:
function wipe_window()
    --for _, control in pairs(win.Control) do --the Control array is always empty, unexpectedly
    --    control:destroy()
    --end
    while win.ControlCount > 0 do
        local control= win:getControl(0)
        control:destroy()
    end
end


win= createForm()
control_setSize(win, 200, 200)
w= createButton(win)
control_setCaption(w, "wipe")
control_setPosition(w, 5, 5)
w.onMouseUp= wipe_window -- this..

_________________
HEY Hitler
Do you get lazy when making trainers?
Well no more!
My CETrainer will generate it for you in seconds, so you won't get lazy! Very Happy

http://forum.cheatengine.org/viewtopic.php?t=564919
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> Cheat Engine Lua Scripting All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites