Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Communication between TEdit and TListBox

arash82arash82 Posts: 106Member
Hi,

I have a TEdit and a TListBox. The ListBox has a lot of line within it ordered alphabetically. I want that when a user starts to type a letter or two in the Edit, the ListBox should jump to that Letter. If the user types exactly the same shit as in a string in the ListBox, that string should be selected. How should I do?

Regards,
Arash

PS. You can study the same phenomena in a TFontDialog, when you type a Font Name in the Edit there DS.

Comments

  • chlifchlif Posts: 24Member
    Hi,

    I made a little code that should work almost like you wanted to. It checks everytime user type any letter to TEdit if there is a word that begin or is the word user is typing. If so the procedure selects word from TListBox... Sorry, my bad english. I hope you understood something...

    Here's the code. I hope it will help:

    [code]
    procedure TForm1.Edit1Change(Sender: TObject);
    var
    i, c : integer; // integers for loops
    s : string;
    ok : boolean;
    begin
    for i := 0 to ListBox1.Items.Count-1 do
    begin

    // if the word in TListBox is excatly the same as the word user
    // type select it and exit the procedure

    if Edit1.Text = ListBox1.Items[i] then
    begin
    ListBox1.ItemIndex := i;
    Exit;
    end;

    s := ListBox1.Items[i];
    ok := True;

    // If Edit1.Text is not empty then check if there is a word in ListBox1
    // that includes all the letters from Edit1.Text

    if Length(Edit1.Text) > 0 then
    begin
    for c := 0 to Length(Edit1.Text) do
    begin
    // if letters doesn't mach then don't select the word...
    if Edit1.Text[c] <> s[c] then
    begin
    ok := False;
    end;
    end;
    // but if every letter is the same that in the ListBox.Items[i]
    // then select the word and exit from procedure
    if ok then
    begin
    ListBox1.ItemIndex := i;
    Exit;
    end;
    end;
    end;
    end;
    [/code]
  • arash82arash82 Posts: 106Member
    Thanks a lot, but I don't get it work! :(
  • arash82arash82 Posts: 106Member
    Thanks a lot. It's a grate code. The only problem is that it doesn't work if the user types with lower chars and the text is in UPPER CHARS. Any idea on how to fix that and make the search process independent from the char set?
  • zibadianzibadian Posts: 6,349Member
    : Thanks a lot. It's a grate code. The only problem is that it doesn't work if the user types with lower chars and the text is in UPPER CHARS. Any idea on how to fix that and make the search process independent from the char set?
    :
    Use the Uppercase() and Upcase() functions, in the if statements, like this:
    [code]
    // Other Code
    if Uppercase(Edit1.Text) = Uppercase(ListBox1.Items[i]) then
    // Other Code
    if Upcase(Edit1.Text[c]) <> Upcase(s[c]) then
    // Other Code
    [/code]
  • arash82arash82 Posts: 106Member
    Hehe I Figured that out. All you have to do is to make all text in the process LoweCare (or UpperCase if you like that better :) )

    Here is the fixed code. Modyfied text is marked with [italic][b]italic bold[/b][/italic]:

    [code]
    procedure TForm1.Edit1Change(Sender: TObject);
    var
    i, c : integer; // integers for loops
    s : string;
    ok : boolean;
    begin
    for i := 0 to ListBox1.Items.Count-1 do
    begin

    // if the word in TListBox is excatly the same as the word user
    // type select it and exit the procedure

    [italic][b]if LowerCase(Edit1.Text) = LowerCase(ListBox1.Items[i]) then[/b][/italic]
    begin
    ListBox1.ItemIndex := i;
    Exit;
    end;

    s := ListBox1.Items[i];
    ok := True;

    // If Edit1.Text is not empty then check if there is a word in ListBox1
    // that includes all the letters from Edit1.Text

    if Length(Edit1.Text) > 0 then
    begin
    for c := 0 to Length(Edit1.Text) do
    begin
    // if letters doesn't mach then don't select the word...
    [italic][b]if LowerCase(Edit1.Text[c]) <> LowerCase(s[c]) then[/b][/italic]
    begin
    ok := False;
    end;
    end;
    // but if every letter is the same that in the ListBox.Items[i]
    // then select the word and exit from procedure
    if ok then
    begin
    ListBox1.ItemIndex := i;
    Exit;
    end;
    end;
    end;
    end;
    [/code]
Sign In or Register to comment.