| 
			
				|  | Cheat Engine The Official Site of Cheat Engine
 
 
 |  
 
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| Game Hacking Dojo Master Cheater
 
  Reputation: 1 
 Joined: 17 Sep 2023
 Posts: 250
 
 
 | 
			
				|  Posted: Tue Nov 26, 2024 10:02 am    Post subject: Set Combo Box Items |   |  
				| 
 |  
				| How do I set items of a combo box to table items or directly insert an item? 
 #################
 
 Okay, solved thank you
 
 
  	  | Code: |  	  | local combobox = createComboBox() local table = {}
 
 if table ~= {} then
 for i = 1, #table do
 combobox.items.add(table[i])
 end
 end
 | 
 |  |  
		| Back to top |  |  
		|  |  
		| atom0s Moderator
 
  Reputation: 205 
 Joined: 25 Jan 2006
 Posts: 8587
 Location: 127.0.0.1
 
 |  |  
		| Back to top |  |  
		|  |  
		| LeFiXER Grandmaster Cheater Supreme
 
 ![]() Reputation: 20 
 Joined: 02 Sep 2011
 Posts: 1069
 Location: 0x90
 
 | 
			
				|  Posted: Wed Nov 27, 2024 10:19 am    Post subject: Re: Set Combo Box Items |   |  
				| 
 |  
				|  	  | Game Hacking Dojo wrote: |  	  | How do I set items of a combo box to table items or directly insert an item? 
 #################
 
 Okay, solved thank you
 
 
  	  | Code: |  	  | local combobox = createComboBox() local table = {}
 
 if table ~= {} then
 for i = 1, #table do
 combobox.items.add(table[i])
 end
 end
 | 
 | 
 
 In addition to what atom0s has said. I would like to point out that this code is very specific and will cause you to copy/paste for every situation where you want to add items from a table to a combobox. It would be better to do this:
 
  	  | Code: |  	  | -- Cleanup the form if it already exists
 if exampleForm then exampleForm.destroy(); exampleForm = nil end
 
 -- Define items for the combobox
 local items = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' }
 
 -- Create the form
 exampleForm = createForm(getMainForm())
 exampleForm.Name = 'frmMain'
 exampleForm.Caption = 'Combobox Example Form'
 exampleForm.Width = 300
 exampleForm.Height = 100
 exampleForm.Position = 'poDesktopCenter'
 
 -- Create a label for the combobox
 local cbItemsLabel = createLabel(exampleForm)
 cbItemsLabel.Caption = 'Combobox Items:'
 cbItemsLabel.Top = 20
 cbItemsLabel.Left = 10
 
 -- Create the combobox
 local cbItems = createComboBox(exampleForm)
 cbItems.Name = 'myCombo'
 cbItems.Width = exampleForm.Width - 20
 cbItems.Top = 40
 cbItems.Left = 10
 cbItems.Text = ''
 
 -- Function to add items from a table to a combobox
 local function addTableToCombobox(form, combobox, tbl)
 local function getFormComponent(form, componentName)
 if form == nil then error('Must provide a valid form.') end
 for i = 0, form.getComponentCount() - 1 do
 local component = form.getComponent(i)
 if component.Name == componentName then
 return component
 end
 end
 error('Invalid component.')
 end
 
 -- Check if the table is valid
 if tbl == nil or #tbl == 0 then error('Must provide a valid table.') end
 
 -- Get the combobox component
 local comboboxComponent = getFormComponent(form, combobox.Name)
 
 -- Add the items from the table to the combobox
 for i = 1, #tbl do
 comboboxComponent.Items.add(tbl[i])
 end
 end
 
 -- Add the items to the combobox, here I am passing the variable names, rather than the name of the component
 addTableToCombobox(exampleForm, cbItems, items)
 
 | 
 
 This way, you can add table items to any combobox on any form. I would highly recommend modular programming because it breaks down a program into smaller, manageable pieces, each handling a specific task. This makes the code easier to understand, maintain, and debug. It also allows for code reuse, faster development, and better collaboration among team members. By isolating functionality into separate modules, changes in one part of the code are less likely to affect others, improving overall efficiency and scalability. Just something I thought I should point out.
 |  |  
		| Back to top |  |  
		|  |  
		| Game Hacking Dojo Master Cheater
 
  Reputation: 1 
 Joined: 17 Sep 2023
 Posts: 250
 
 
 | 
			
				|  Posted: Wed Nov 27, 2024 1:31 pm    Post subject: |   |  
				| 
 |  
				|  	  | atom0s wrote: |  	  | Just a heads up, this is not a valid means of comparison in Lua: 
  	  | Code: |  	  | if table ~= {} then | 
 
 When you define a table by setting something to just {} (an empty table) this creates a new instance of a unique table object. For example:
 
 
  	  | Code: |  	  | local t1 = {};
 local t2 = {};
 
 | 
 
 This would create two separate instances of a table object into t1 and t2 respectively. However, they are not equal to the same thing, meaning doing:
 
 
 Would always be false.
 
 Based on what you're showing in your main post, it looks more like you would want to check the length of the table instead. Such as:
 
 
  	  | Code: |  	  | if (#t1 > 0) then
 -- add elements of table to the combobox..
 end
 
 | 
 | 
 
 Thank you for the feedback, I really appreciate it. fixed.
 
 
  	  | LeFiXER wrote: |  	  | This way, you can add table items to any combobox on any form. I would highly recommend modular programming because it breaks down a program into smaller, manageable pieces, each handling a specific task. This makes the code easier to understand, maintain, and debug. It also allows for code reuse, faster development, and better collaboration among team members. By isolating functionality into separate modules, changes in one part of the code are less likely to affect others, improving overall efficiency and scalability. Just something I thought I should point out. | 
 
 Thank you for the tip. I try to work by that prioritising efficiency and readability.
 
 This is what I needed this for:
 https://forum.cheatengine.org/viewtopic.php?t=622886
 
 Any feedback or criticism is apperciated.
 |  |  
		| Back to top |  |  
		|  |  
		|  |  
  
	| 
 
 | 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
 
 |  |