Narrowing list content like in MS Outlook Express

I am trying to duplicate some functionality found in MS Outlook Express where a list of Newsgroup is displayed. Then the user can enter a string to display newsgroups that contain that string.

Any idea how this can be implemented ?
moonriseAsked:
Who is Participating?
 
erajojCommented:
By accepting this measly answer with very good marks! :-)

/// John
0
 
viktornetCommented:
Is it incremental searching that you want to do?? Like enter a string in the edit box and if this string is contained in any of the words for example in a listbox, then it gets selected???

Regards,
Viktor Ivanov
0
 
nrobinCommented:
Depending on what your data structures you're using, you could if storing the newsgroups in a database, use the like sql statement.  When the text changes, re-submit the sql and then update the listview.  The speed may be of concern when the size is exceptinally large.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
nrobinCommented:
Depending on what your data structures you're using, you could if storing the newsgroups in a database, use the like sql statement.  When the text changes, re-submit the sql and then update the listview.  The speed may be of concern when the size is exceptinally large.

Regards
Nicholas Robinson
0
 
viktornetCommented:
What you could do is this...Have a listbox that loads its items from a datebase or from a file, and have an edit box that is on the top of the listbox....Sort the items in the listbox, and enter this in the OnChange event of the editbox....

procedure TForm1.Edit1Change(Sender: TObject);
var
  S : Array[0..255] of Char;
begin
  StrPCopy(S, Edit1.Text);
  with ListBox1 do
    ItemIndex := Perform(LB_SELECTSTRING, 0, LongInt(@S));
end;

this would set the index on the string that is contained in any of the fields of the listbox...

For example
______
|am        |
|abd       |
|abcde   |
|db         |
|______|

That's the listbox with the items.....
IF = If you type

IF a then ItemIndex := 0;
IF ab then ItemIndex := 1;
IF db then ItemIndex := 3;
and so on....

Regards,
Viktor Ivanov
0
 
nrobinCommented:
Viktors solution is the most simple and easiest way to do this.  If you simply want to select the item, then sending the LB_SELECTSTRING is a good solution.  However if you want the view to look and act like MS's, then that will not do.  

-N.
0
 
viktornetCommented:
Hello nrobin. How does the Outlook looks anyway? Maybe it's got a nice interface, huh?

Regards,
Viktor Ivanov
0
 
nrobinCommented:
The interface is quite nice.  If you can imagine a form with an edit box at the top to enter the newsgroup name in, then a listview sits underneath with all the newsgroups.  Everytime you press a key in the editbox, the listview contains a shortened view.  So if you type alt., then within a few seconds all the items with the entered text are displayed.  Threads are obviously used to do the updating of the list - a key point.

Regards
Nicholas Robinson.
0
 
viktornetCommented:
Yes you can do this then..

var
    List : TStringList;
begin
    for i := 0 to List.Items.Count - 1 do
    begin
        if Pos(edit.text, List.Items[i]) then
            ListBox.Items.Add(List.Items[i]);
    end;
end;

Hope this helps =)

Regards,
Viktor Ivanov
0
 
viktornetCommented:
Actually this is the right code....

var
         List : TStringList;
     begin
         ListBox.Clear;
         for i := 0 to List.Items.Count - 1 do
         begin
             if Pos(edit.text, List.Items[i]) then
                 ListBox.Items.Add(List.Items[i]);
         end;
//vik
     end;
0
 
erajojCommented:
Hi,
Here's an example of getting the same functionality as MS Outlook Express:

  ...
  private
    m_listGroups       : TStringList;
    m_bCriteriaChanged : Boolean;
    procedure SearchGroups( sSubStr: string );
    { Private declarations }
  end;

  ...
procedure TForm1.FormCreate(Sender: TObject);
begin
  if ( not FileExists( 'c:\newsgroups.dat' ) ) then
  begin
    _NNTP1.Login; // uses Andreas Hörstemeier's TCP/IP...
    _NNTP1.GetAllNewsgroups;
    _NNTP1.Logout;
    _NNTP1.NewsGroups.SaveToFile( 'c:\newsgroups.dat' );
  end;
  m_listGroups := TStringList.Create;
  m_listGroups.LoadFromFile( 'c:\newsgroups.dat' );
end;

procedure TForm1.SearchGroups( sSubStr: string );
var
  iIndex: Integer;
  bEmpty: Boolean;
begin
  Screen.Cursor := crHourGlass;
  m_bCriteriaChanged := False;
  try
    sSubStr := LowerCase( sSubStr );
    ListView2.Items.BeginUpdate;
    ListView2.Items.Clear;
    bEmpty := sSubStr = '';
    for iIndex := 0 to m_listGroups.Count - 1 do begin
      if ( bEmpty ) or ( Pos( sSubStr, LowerCase( m_listGroups[ iIndex ] ) ) > 0 )
      then with ListView2.Items.Add do begin
        Caption := m_listGroups[ iIndex ];
        Data    := m_listGroups.Objects[ iIndex ];
      end;
      Application.ProcessMessages; // allow abortion...
      if ( m_bCriteriaChanged )
      then Break;
    end;
  finally
    ListView2.Items.EndUpdate;
    Screen.Cursor := crDefault;
  end;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  Timer1.Enabled  := False;
  m_bCriteriaChanged := True;
  Timer1.Interval := 500;
  Timer1.Enabled  := True;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if ( Key = #13 )
  then begin
    Key := #0; // remove error beep
    Timer1.Enabled := False;
    SearchGroups( Edit1.Text );
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False;
  SearchGroups( Edit1.Text );
end;

Really easy, but quite sufficient...

/// John
0
 
moonriseAuthor Commented:
Thank you all. erajoj has the perfect answer. It works great. How do I give erajoj the points ?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.