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 


Get numbers of selected lines in ListBox?

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

Joined: 17 Jan 2018
Posts: 202

PostPosted: Fri Aug 20, 2021 4:30 am    Post subject: Get numbers of selected lines in ListBox? Reply with quote

Example: ListBox with lines: line 1, line 2, line 3, line 4, line 5. MultiSelect = true.
How do I get the numbers of the selected lines in ListBox, if line 2 and line 4 are selected?
How to select line 1, line 3, line 5 in ListBox with code?
Back to top
View user's profile Send private message
Corroder
Grandmaster Cheater Supreme
Reputation: 75

Joined: 10 Apr 2015
Posts: 1667

PostPosted: Fri Aug 20, 2021 6:31 am    Post subject: Re: Get numbers of selected lines in ListBox? Reply with quote

Razi wrote:
Example: ListBox with lines: line 1, line 2, line 3, line 4, line 5. MultiSelect = true.
How do I get the numbers of the selected lines in ListBox, if line 2 and line 4 are selected?
How to select line 1, line 3, line 5 in ListBox with code?


Example q1:

Code:
local f = createForm( true )
local lb = createListBox( f )

-- Add items to listbox..
local s = listbox_getItems( lb );
strings_add( s, 'Entry 1' );
strings_add( s, 'Entry 2' );
strings_add( s, 'Entry 3' );
strings_add( s, 'Entry 4' );

lb.MultiSelect = true
local ttItems = lb.Items.Count

function test()
 local line_index = lb.ItemIndex + 1
 if line_index == nil then return nil end
 print('Line no. '..line_index..' of total '..ttItems..' Items')
end

lb.OnSelectionChange = test


Note 1 : I don't know how to print out multi selected items not in order (random).

And attached file is my old CT file to try CEListbox operation.

Note 2 : Make sure Listbox property for 'Sorted' set to false.


make a form with 2 listboxes and 3 buttons (add all, add selected, reset)


Code:
--- Define variables name for propeties name

source_list = UDF1.CEListBox1
selected_list = UDF1.CEListBox2
button_select_all = UDF1.CEButton1
button_select_specific = UDF1.CEButton2
button_reset = UDF1.CEButton3

source_list.MultiSelect = true
-- Define functions for onClick and onDblClick events
function add_single_item()                   --- function to select one item on listbox1 and add to listbox2
 --source_list.MultiSelect = false             --- make user able to select one item on listbox1
 src_index = source_list.ItemIndex           --- get index number of item on listbox1
 src_item = source_list.items[src_index]     --- get item refer by index number on listbox1
 selected_list.items.Add(src_item)           --- add that item to listbox2
end

function select_all_items()                  --- function to select all items on listbox1 and add to listbox2
 source_list.MultiSelect = true              --- make enable user able to multi select on listbox1
 ttl_items_srclist = source_list.items.Count --- variable for store total items on listbox1
  for i = 0, ttl_items_srclist - 1 do        --- get all items from listbox1 nd add to listbox2
   get_item = source_list.items[i]
   selected_list.items.Add(get_item)
   if i == -1 then                           --- counter to avoid error if index out of bound
    return nil
   end   -- end of if i == -1
  end    -- end of for i = 0, ttl_items_srclist - 1 do
end

function delete_selected_item()                --- function to select one item on listbox2 and delete the item
 --selected_list.MultiSelect = false           --- make user able to select one item on listbox2
 selected_list.Sorted = true                   --- make items on listbox2 sorted by item name
 x = selected_list.itemIndex                   --- get index number of item on listbox2
 selected_list.items.Delete(x)                 --- delete that item from listbox2
end

function reset_selected_item()                 --- function to clear items on listbox2
 selected_list.Clear()                         --- delete all items on listbox2
end

function marker_selected()
 --source_list.MultiSelect = false             --- make user able to select one item on listbox1
 source_list.Sorted = true                     --- make items on listbox1 sorted by item name
 x = source_list.itemIndex
 get_item = source_list.items[x]..'  '..string.rep(string.char(1),2)
 source_list.items[x] = get_item
end


function add_selected()
 count = source_list.Items.Count
 for i = 0,count-1 do
 if source_list.Selected[i] then
  get_item = source_list.items[i]..' '..string.rep(string.char(17),2)
  source_list.items[i] = get_item
  selected_list.items.Add(source_list.items[i])
 else
  print(source_list.Items[i],'not selected')
 end
end
end


-- Define functions for onClick and onDblClick events

button_select_all.onClick = select_all_items     --- add all items firm listbox1 to listbox2
source_list.onDblClick = add_single_item         --- add one selected item form listbox1 to listbox2
selected_list.onDblClick = delete_selected_item  --- delete an selected item from listbox2
button_reset.onClick = reset_selected_item       --- delete all items from listbox2
button_select_specific.onClick = add_selected
--source_list.onClick = marker_selected

UDF1.show()


Note 3 : I can't attach the CT file event the size of file only 5 kb

_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL
Back to top
View user's profile Send private message
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Fri Aug 20, 2021 10:11 am    Post subject: Reply with quote

It's my opinion.
Not all of the selections will be visible, but there will be a function that saves the selection and calls it back.
Otherwise, "CheckListBox" may give more probable results.

Code:
if f then f.Destroy() end
f = createForm( true )
local lb = createListBox( f )
local edt = createMemo( f ) edt.left=lb.width + 10 --edt.visible=false
lb.Height=edt.Height
local btn = createButton( f ) btn.top=lb.Height + 10 btn.caption="Enable"

-- Add items to listbox..
local s = listbox_getItems( lb );
strings_add( s, 'Entry 1' );
strings_add( s, 'Entry 2' );
strings_add( s, 'Entry 3' );
strings_add( s, 'Entry 4' );
strings_add( s, 'Entry 5' );

lb.MultiSelect = true
local ttItems = lb.Items.Count
local line_start=2

function test()
line_index=0
if line_start==2 then
 line_index = lb.ItemIndex
 line_text = lb.Items[line_index]
 edt.lines.add(line_text)
 end
 if line_index == nil then return nil end
if line_start==1 then
for i = 0, edt.Lines.Count - 1 do
line_slctext=edt.Lines[i]
if line_index~="" then  --enable func..
 print('Line no. '..line_slctext..' of total '..ttItems..' Items') --or func()
end
end
line_start=2
end
end

btn.OnClick=function()
line_start=1
test()
end

lb.OnSelectionChange = test
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: Fri Aug 20, 2021 11:39 am    Post subject: Reply with quote

use the Selected[] property

go through all lines and check if Selected[linenr] is true

_________________
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
ByTransient
Expert Cheater
Reputation: 5

Joined: 05 Sep 2020
Posts: 240

PostPosted: Fri Aug 20, 2021 1:57 pm    Post subject: Reply with quote

Dark Byte wrote:
use the Selected[] property

go through all lines and check if Selected[linenr] is true


Thanks..


EDIT:
added: Selected[]
added: ExtendedSelect

Added multi-select without Ctrl.
To clear selections, the list was packed in a function.

Code:
if f then f.Destroy() end
f = createForm( true )
local lb = createListBox( f )
local edt = createMemo( f ) edt.left=lb.width + 10 --edt.visible=false
lb.Height=edt.Height
local btn = createButton( f ) btn.top=lb.Height + 10 btn.caption="Enable"

function AddItemsListbox()
local s = listbox_getItems( lb );
strings_add( s, 'Entry 1' );
strings_add( s, 'Entry 2' );
strings_add( s, 'Entry 3' );
strings_add( s, 'Entry 4' );
strings_add( s, 'Entry 5' );
end
AddItemsListbox()

lb.MultiSelect = true
local ttItems = lb.Items.Count
local line_start=2

function test()
lb.ExtendedSelect=false
line_index=0
if line_start==2 then
 line_index = lb.ItemIndex
 line_text = lb.Items[line_index]
 edt.lines.add(line_text)
 end
 if line_index == nil then return nil end
if line_start==1 then
for i = 0, lb.Items.Count - 1 do
line_text=lb.Items[i]
if lb.Selected[i]==true then  --enable func..
lines=i + 1
 print('Select line '..line_text..' of lines '..lines) --or func()
end
end
line_start=2
edt.lines.Text=""
lb.Clear()
AddItemsListbox()
end
end

btn.OnClick=function()
line_start=1
test()
end

lb.OnSelectionChange = test
Back to top
View user's profile Send private message
Razi
Expert Cheater
Reputation: 1

Joined: 17 Jan 2018
Posts: 202

PostPosted: Sat Aug 21, 2021 4:46 am    Post subject: Reply with quote

Thanks a lot for answers.
To get the numbers of the selected lines in ListBox, if line 2 and line 4 are selected, I use the following code:
Code:
for x = 0, 4 do
if UDF1.CEListBox1.Selected[x] == true then
print(x)
end
end


To select line 1, line 3, line 5 in ListBox:
Code:
UDF1.CEListBox1.clearSelection()
for x = 0, 4, 2 do
UDF1.CEListBox1.Selected[x] = true --false to deselect
end
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