View Full Version : Need some help with a ListBox problem?
mystica
08-02-2009, 05:14 AM
I have a ListBox that's tied in with a Dialog.FileBrowse action. Basically, the Dialog.FileBrowse action just adds strings to the ListBox, each time that a file-selection is made:
eg.
C:\example1.txt
C:\example2.txt
C:\example3.txt
I need to get a value returned, to determine if the same string gets added twice. I can get a value returned okay, if the same string is added twice in a row, or sequentially ...
eg.
C:\example1.txt
C:\example1.txt
but how do I get a value returned if the string not added sequentially?
eg.
C:\example1.txt
C:\example2.txt
C:\example1.txt
In other words, how would I get a value returned on the fact that C:\example1.txt has been added a second time, but a little later in the game? :huh
Centauri Soldier
08-02-2009, 06:07 AM
I would use a for loop to check the contents of the listbox against the potential file. If an item from the listbox is equivalent to the one being selected by the user then do not add it an break out of the loop otherwise add it to the listbox.
mystica
08-02-2009, 07:11 AM
Oh okay ... so like a 'do-while' loop, yeh?
Something like,
a = 1;
while a < 10 do
a = a + 1;
end
Just not sure how to apply it in this instance, though?
reteset
08-02-2009, 08:18 AM
Global Functions:
function IsItemExist(strListBox,strItem)
local bRet = false;
nLBCount = ListBox.GetCount(strListBox);
local nCurrent = -1;
while (nCurrent < nLBCount) do
local nFound = ListBox.FindItem(strListBox, nCurrent , LB_BYTEXT, "*"..strItem.."*");
if (nFound ~= LB_ERROR ) then
bRet = true;
nCurrent = nFound;
else
nCurrent = nCurrent + 1;
end
return bRet,nCurrent;
end
end
AnyButton OnClick :
tblFiles = Dialog.FileBrowse(true, "Locate File", _DesktopFolder, "All Files (*.*)|*.*|", "", "txt", false, true);
if ( tblFiles ~= nil) then
bRet,nCurrent = IsItemExist("ListBox1",tblFiles[1]);
if (bRet) then
Dialog.Message("Notice", "Already Exist..!");
ListBox.SelectItem("ListBox1", nCurrent);
else
ListBox.AddItem("ListBox1", tblFiles[1], "");
end
end
Good Luck :yes
mystica
08-02-2009, 02:46 PM
Far out reteset ... it's gonna take me a while to digest that. Many thanks, I'll investigate it and see if it's what I need.
Having said that, while at work last night, I was comtemplating Centauri's suggestion of a loop ... but couldn't get it to work in my head. Then when I got home, after exhausting loop-ideas, substituted the ListBox with a ComboBox ... and then had a bit of an epiphany, and came up with this:
sValue = Dialog.FileBrowse(true, "Locate File", _DesktopFolder, "All Files (*.*)|*.*|", "", "dat", false, false);
nValue = ComboBox.FindItem("ComboBox1", -1, LB_BYTEXTDATA, sValue[1]);
if (nValue == -1) then
ComboBox.AddItem("ComboBox1", sValue[1], "");
ComboBox.SetText("ComboBox1", sValue[1]);
end
if nValue >0 then
chkList = Dialog.Message("Oops!", "Entry already in list. Do you still wish to add entry?", MB_YESNO, MB_ICONQUESTION, MB_DEFBUTTON1);
if chkList == 6 then
ComboBox.AddItem("ComboBox1", sValue[1], "");
ComboBox.SetText("ComboBox1", sValue[1]);
end
end
Now,it seems to work pretty good. But can I ask you 2 guys, for comments on this so-called ephiphany of mine? I know the code works, but in terms of logicality/practicality, and in terms of sticking to good-sense coding, what are your opinions on my solution? (You 2 guys are lightyears ahead of me in this department, so I'd value your opinions, yeh? Same goes for any others out there who'd like to offer up comments ... they'd be much appreciated)
PS.
I've attached an example, if that helps any?:huh
Powered by vBulletin™ Version 4.0.6 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.