: : : Hi all
: : :
: : : From my testings, "stringlist.Duplicates := dupIgnore/dupError/dupAccept" doesn't appear to help with my problem.
: : :
: : : Quick rundown of application:
: : : Two edit fields (Name/IPAddress), 1 button (Add), 1 listbox
: : : When you press the button, it first adds the edName.Text part to the listbox, and then it adds both edName.Text and edIPAddress.Text to a stringlist as a Name=Value pair, which then gets saved to a file.
: : :
: : : What I want to do is to prevent doubling up of either the Name or the IPAddress. dupIgnore/Error only works if both Name and IPAddress are the same the second time around, due to it looking at the entire string
: : : (Name=IPAddress instead of Name or IPAddress by themselves).\
: : :
: : : Is there an easy way to do this, or do I have to figure out a function for myself?
: : :
: : : Cheers in advance
: : :
: : The only way of doing this is checking if the name or IP already exists before adding the item. The IndexOf() method will come in handy for this.
: :
:
: Can IndexOf() look parts of a string? I've been trying to use it, but it only seems to look at complete strings. This is a problem, since the stringlists get stored looking like:
: AWS-2500=192.168.0.42
: Diesel Tank=192.168.0.21
: Workshop=192.168.0.41
: IndexOf() returns -1 if I look for:
: sl.IndexOf('WorkShop') / sl.IndexOf(edName.Text)
: or:
: sl.IndexOf('192.168.0.41) / sl.IndexOf(edIP.Text)
: but it returns the proper index when it's:
: sl.IndexOf('WorkShop=192.168.0.41') / sl.IndexOf(edName.Text+'='+edIP.Text)
:
: I know I can write a function that delimits the strings by the '=' sign, and then do the checking that way, but I'd like it if I could do the task in only a few of lines of code, if possible.
:
You can use IndexOfName() and IndexOfValue() (check the latter with the helpfiles). Otherwise you need to loop through it and check using the Pos() function:
for i := 0 to sl.Count-1 do
if Pos(Name, sl[i])+Pos(IP, sl[i]) = 0 then
sl.Add(Name+'='+IP);