View previous topic :: View next topic |
Author |
Message |
Razi Expert Cheater
Reputation: 1
Joined: 17 Jan 2018 Posts: 205
|
Posted: Wed May 10, 2023 4:24 am Post subject: Move combobox items |
|
|
How to move combobox items in the following example?
Combobox with several items (with any text) and button on the form. If we type some text in the combobox and click the button, then 1 item will be added to the combobox, but if the same text is already in the combobox, then this item must be removed. Code: | function UDF1_CEButton1Click(sender)
local cmbx=UDF1.CEComboBox1
for x=0, cmbx.Items.Count-1 do
if cmbx.Items[x] == cmbx.Text then
cmbx.Items.Delete(x)
end
end
if cmbx.Text ~= '' then
cmbx.Items.Insert(0,cmbx.Text)
end
end | When 1 of the combobox items is selected, then need to remove that item and insert it in first place. But, when 1 of the combobox items is selected, it gives error: out of bounds. How to avoid this error?
Last edited by Razi on Wed May 10, 2023 10:57 am; edited 1 time in total |
|
Back to top |
|
 |
AylinCE Grandmaster Cheater Supreme
Reputation: 35
Joined: 16 Feb 2017 Posts: 1478
|
Posted: Wed May 10, 2023 4:56 am Post subject: |
|
|
*It can be added to the list.
*Checks the added item in the list.
*If the button is pressed, the selected item is taken to the top of the list.
Code: | if srchFrm1 then srchFrm1.Destroy() srchFrm1=nil end
DP1=getScreenDPI()/96
srchFrm1=createForm()
srchFrm1.height=70*DP1 srchFrm1.width=340*DP1 srchFrm1.left=283*DP1 srchFrm1.top=142*DP1
srchFrm1.PopupMode=0 srchFrm1.caption="Search Add Tbl"
srchFrm1.Position="poDesktopCenter" srchFrm1.BorderStyle="bsSingle" srchFrm1.ShowInTaskBar="stAlways"
-------------------------
local UDF1 = {}
----------------------- UDF1.CEButton1 -----
UDF1.CEButton1=createButton(srchFrm1)
UDF1.CEButton1.AutoSize=false
UDF1.CEButton1.height=25*DP1 UDF1.CEButton1.width=120*DP1 UDF1.CEButton1.left=210*DP1 UDF1.CEButton1.top=9*DP1
UDF1.CEButton1.caption="Search"
UDF1.CEButton1.Font.Style="fsBold" UDF1.CEButton1.Font.Size=10*DP1
-----------------------
----------------------- UDF1.CEButton2 -----
UDF1.CEButton2=createButton(srchFrm1)
UDF1.CEButton2.AutoSize=false
UDF1.CEButton2.height=25*DP1 UDF1.CEButton2.width=190*DP1 UDF1.CEButton2.left=10*DP1 UDF1.CEButton2.top=40*DP1
UDF1.CEButton2.caption="Clear Search List"
UDF1.CEButton2.Font.Style="fsBold" UDF1.CEButton2.Font.Size=10*DP1
-----------------------
----------------------- UDF1.CEComboBox1 -----
UDF1.CEComboBox1=createComboBox(srchFrm1)
UDF1.CEComboBox1.AutoSize=true
UDF1.CEComboBox1.height=22*DP1 UDF1.CEComboBox1.width=190*DP1 UDF1.CEComboBox1.left=10*DP1 UDF1.CEComboBox1.top=10*DP1
UDF1.CEComboBox1.text=""
UDF1.CEComboBox1.Font.Style="fsBold" UDF1.CEComboBox1.Font.Size=10*DP1
--UDF1.CEComboBox1.ReadOnly=false UDF1.CEComboBox1.Style="csDropDown"
-----------------------
--============================================================================--
--============================================================================--
--============================================================================--
local srcTextTbl={}
local newText=""
function clrTbl(tbl)
for i,k in pairs(tbl) do
tbl[i] = nil
end
end
UDF1.CEButton1.OnClick=function()
local check1 = 0
if UDF1.CEComboBox1.text=="" then
print("Please type a word in the search text box!")
else
newText=UDF1.CEComboBox1.text
for l,k in pairs(srcTextTbl) do
if k==newText then check1=1 end
end
if check1==0 then
srcTextTbl[#srcTextTbl+1] = newText
end
UDF1.CEComboBox1.Clear()
UDF1.CEComboBox1.Items.Add(newText)
for m,n in pairs(srcTextTbl) do
if n~=newText then
UDF1.CEComboBox1.Items.Add(n)
end
end
UDF1.CEComboBox1.ItemIndex=0
end
end
UDF1.CEButton2.OnClick=function()
clrTbl(srcTextTbl)
UDF1.CEComboBox1.clear()
end |
_________________
|
|
Back to top |
|
 |
Razi Expert Cheater
Reputation: 1
Joined: 17 Jan 2018 Posts: 205
|
Posted: Wed May 10, 2023 11:08 am Post subject: |
|
|
AylinCE wrote: | *It can be added to the list.
*Checks the added item in the list.
*If the button is pressed, the selected item is taken to the top of the list.
Code: | local srcTextTbl={} |
|
Can this be done without a table and with as few lines of code as possible?
|
|
Back to top |
|
 |
AylinCE Grandmaster Cheater Supreme
Reputation: 35
Joined: 16 Feb 2017 Posts: 1478
|
Posted: Wed May 10, 2023 12:44 pm Post subject: |
|
|
The code is not long, it's just a code that handles 3 phases with mandatory tests.
If you omit any of the steps, the code will be shortened, but you will get complex usage without some checks.
A table is a control that contains the final list.
Table's controls;
It is testing if the searched word is in the list.
It stores the entire list.
When the combobox list is reset, it provides the new list.
Overriding the table requires saving the list to a different control. This can be a local designation or a hidden Memo.
In short, a check is required that will add the new list (Add("text")) when the Combobox is cleared (Clear()).
Code: | UDF1.CEButton1.OnClick=function()
local check1 = 0
-- Check 1: Block blank text search.
if UDF1.CEComboBox1.text=="" then
print("Please type a word in the search text box!")
else
newText=UDF1.CEComboBox1.text
-- Check 2: Query if the searched word is in the list.
for l,k in pairs(srcTextTbl) do
if k==newText then check1=1 end
end
if check1==0 then
srcTextTbl[#srcTextTbl+1] = newText
end
-- Check 3: Add new list. (And move the searched text up.)
UDF1.CEComboBox1.Clear()
UDF1.CEComboBox1.Items.Add(newText)
for m,n in pairs(srcTextTbl) do
if n~=newText then
UDF1.CEComboBox1.Items.Add(n)
end
end
UDF1.CEComboBox1.ItemIndex=0
end
end |
If you want to do this without the Table, you should have a native list control, a stringList() and another for loop that queries the list.
These create more code space than the table.
_________________
|
|
Back to top |
|
 |
Razi Expert Cheater
Reputation: 1
Joined: 17 Jan 2018 Posts: 205
|
Posted: Thu May 11, 2023 5:41 am Post subject: |
|
|
Solved this error in this way: (but the code requires at least one Item in the combobox)
Code: | function UDF1_CEButton1Click(sender)
local cmbx=UDF1.CEComboBox1
if cmbx.Text ~= '' then
for x=0, cmbx.Items.Count-1 do
if cmbx.Text == cmbx.Items[x] and cmbx.Text ~= cmbx.Items[0] then
local it = cmbx.Items[x]
cmbx.Items.Delete(x)
cmbx.Items.Insert(0,it)
cmbx.ItemIndex = 0
end
end
if cmbx.Text ~= cmbx.Items[0] then
cmbx.Items.Insert(0,cmbx.Text)
end
end
end |
AylinCE wrote: | The code is not long |
But your method is also good and has fewer lines of code:
Code: | function UDF1_CEButton1Click(sender)
local cmbx=UDF1.CEComboBox1
local check = 0
if cmbx.Text ~= "" then
for x=0, cmbx.Items.Count-1 do
if cmbx.Text == cmbx.Items[x] then
check = 1
end
end
if check == 0 then
cmbx.Items.Insert(0,cmbx.Text)
end
end
end |
|
|
Back to top |
|
 |
AylinCE Grandmaster Cheater Supreme
Reputation: 35
Joined: 16 Feb 2017 Posts: 1478
|
Posted: Thu May 11, 2023 10:15 am Post subject: |
|
|
Bravo! It's smart to use the table option "Insert" in the ComboBox! This is good work and reorders the item at "0" without deleting it.
And came up with the idea to assign the ComboBox as a table.
Thanks for your idea.
Code: | UDF1.CEButton1.OnClick=function()
local cmbx=UDF1.CEComboBox1
local check = 0
if cmbx.Text ~= "" then
for i,k in pairs({cmbx.Items}) do -- Table assignment
if k == cmbx.Text then
check = 1
end
end
if check == 0 then
cmbx.Items.Insert(0,cmbx.Text)
end
end
end |
_________________
|
|
Back to top |
|
 |
Corroder Grandmaster Cheater Supreme
Reputation: 75
Joined: 10 Apr 2015 Posts: 1668
|
Posted: Fri May 12, 2023 6:32 pm Post subject: |
|
|
Razi wrote: | Solved this error in this way: (but the code requires at least one Item in the combobox)
Code: | function UDF1_CEButton1Click(sender)
local cmbx=UDF1.CEComboBox1
if cmbx.Text ~= '' then
for x=0, cmbx.Items.Count-1 do
if cmbx.Text == cmbx.Items[x] and cmbx.Text ~= cmbx.Items[0] then
local it = cmbx.Items[x]
cmbx.Items.Delete(x)
cmbx.Items.Insert(0,it)
cmbx.ItemIndex = 0
end
end
if cmbx.Text ~= cmbx.Items[0] then
cmbx.Items.Insert(0,cmbx.Text)
end
end
end |
|
make it more match.
Code: | if upper(cmbx.Text) == upper(cmbx.Items[x]) and cmbx.Text ~= cmbx.Items[0] then
.....
|
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
Back to top |
|
 |
|