[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 177
  • Last Modified:

Getting same word in an edit and a listbox?

If the same word appears in both an edit and a listbox, I need the wording in the listbox to be shown in a label.  Note that other words will be listed as well, ie:

Example wording in edit: "Do you want elephant.gif?"

Example wording in listbox:
animal/lion.gif
insect/fly.jpg
animal/elephant gif
animal/dog

In the above example, I want "animal/elephant.gif" as a label.

I am using Delphi 6.  Many thanks for your help.
0
rincewind666
Asked:
rincewind666
  • 5
  • 5
1 Solution
 
esoftbgCommented:
procedure TForm1.Button1Click(Sender: TObject);
var
  I:      Integer;
  J:      Integer;
  SL:     TStringList;
  S:      string;
  T:      string;
begin
  SL := TStringList.Create;
  try
    T := '';
    S := Trim(Edit1.Text);
    if (S<>'') then
      Label1.Caption := '';
    for I := 1 to Length(S) do
    begin
      if (S[I] in [#33..#62, #64..#126]) then
        T := T + S[I]
      else
      begin
        if (T<>'') then
        begin
          SL.Add(T);
          T := '';
        end;
      end;
    end;
    for I := 0 to ListBox1.Items.Count-1 do
    begin
      for J := 0 to SL.Count-1 do
      begin
        if (Pos(SL[J], ListBox1.Items[I]) > 0) then
        begin
          Label1.Caption := ListBox1.Items[I];
          Break;
        end;
      end;
    end;
  finally
    SL.Free;
  end;
end;
0
 
krypto2000Commented:
uses strUtils;

procedure split(const Delimiter: Char; Input: string; const Strings: TStrings);
begin
   Assert(Assigned(Strings));
   Strings.Clear;
   Strings.Delimiter := Delimiter;
   Strings.DelimitedText := Input;
end;

procedure TForm1.Button1Click(Sender: TObject);
var splitWords: TStringList; i,j: integer;
begin
   splitWords := TStringList.Create;
   split(#32,edit1.Text,splitWords);

   for i := 0 to listBox1.Items.Count - 1 do
      for j := 0 to splitWords.Count - 1 do
         if ansiContainsStr(listBox1.Items[i],splitWords[j]) then
            label1.Caption := listBox1.Items[i];
end;

That work ;-)
0
 
esoftbgCommented:
download a super example from:
page:        http://www.geocities.com/esoftbg/
  link:        Q_21177707.zip
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
krypto2000Commented:
okay but this example is to much complicated,
it don't use many powerfull function included in delphi,
it's a old-school-pascal style, for something it's most powerfull to make function like this
but not in this case. I've make some test, the execution time is the same, but I think my solution is better
because it's the half of code, simple to understand, powerfull and working !

thank you
0
 
esoftbgCommented:
Hi krypto2000,

I did improve my example a little bit and then I implement your example into my example to test both ....
I was surprised that yours one does not work at all ????
Please download my example again to see where you have a mistake, so to correct it ....

thank you very much
0
 
krypto2000Commented:
okay I would check for corrections... :-)
0
 
krypto2000Commented:
I've found where is the problem.

That's because the text delimiter is the char #32 (space)
but there is no space between the last word and the ?
if you add a space between, that work !
0
 
esoftbgCommented:
You see that sometimes "an old-school-pascal style" can work better than "code, simple to understand, powerfull .... !"
0
 
krypto2000Commented:
I dont'd hope... because I've see the question "If the same WORD appears in both an edit and a listbox..."
what define a word ? after each space its another word correct ?
But it's also possible to make the same as you with delphi powerfull functions, I'm sure !

Your code is correct, my code too ! Each one its preferences ;-)
0
 
esoftbgCommented:
Your code works with only one delimiter,
my code works with [' ', '*'..'.', ':'..'?']      (count of 11 delimiters)
at a pass and could be easyly changed by programmer to another set ....
0
 
rincewind666Author Commented:
I am giving the 500 points to esoftbg as his code does exactly what I asked for.  However, I made a mistake in my question and need something similar.  My mistake so I am awarding the points and closing this question.

I have opened another question called "Getting same sequence of characters in an edit and a listbox?".  Another 500 points for anyone who can help.  Thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now