Solved

Getting same word in an edit and a listbox?

Posted on 2004-10-21
175 Views
Last Modified: 2010-04-05
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
Question by:rincewind666
    11 Comments
     
    LVL 12

    Accepted Solution

    by:
    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
     

    Expert Comment

    by:krypto2000
    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
     
    LVL 12

    Expert Comment

    by:esoftbg
    download a super example from:
    page:        http://www.geocities.com/esoftbg/
      link:        Q_21177707.zip
    0
     

    Expert Comment

    by:krypto2000
    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
     
    LVL 12

    Expert Comment

    by:esoftbg
    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
     

    Expert Comment

    by:krypto2000
    okay I would check for corrections... :-)
    0
     

    Expert Comment

    by:krypto2000
    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
     
    LVL 12

    Expert Comment

    by:esoftbg
    You see that sometimes "an old-school-pascal style" can work better than "code, simple to understand, powerfull .... !"
    0
     

    Expert Comment

    by:krypto2000
    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
     
    LVL 12

    Expert Comment

    by:esoftbg
    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
     

    Author Comment

    by:rincewind666
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    In this Experts Exchange video Micro Tutorial, I'm going to show how small business owners who use Google Apps can save money by setting up what is called a catch-all email address in their Gmail accounts. By using the catch-all feature, small busin…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    6 Experts available now in Live!

    Get 1:1 Help Now