[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ListBox problem

Posted on 1999-01-03
4
Medium Priority
?
1,574 Views
Last Modified: 2012-06-27
Hi...  
  I'm using Delphi 3 and I have an app that uses a TListBox with the sorted property set to true. I have it coded so that I can double-click on any item in the ListBox and I can edit it using a simple inputbox. That works ok, problem is, after I edit an item and change it to something else, the list does not automatically re-sort itself. The new edited item stays just where it was before even though it is now out of alphabetical order! Can you help?
  In a related question concerning TListBoxes...can they be set so that no duplicate items can be entered? Or can this only be done with a TStringList?
 
Thanks
  Shawn Halfpenny
  drumme59@sprint.ca
0
Comment
Question by:aztec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
slautin earned 200 total points
ID: 1354333
for resorted use:

var i : integer;
    s : string;
begin
    s := ListBox1.Items[ListBox1.ItemIndex];
    ListBox1.Sorted:=False;
    ListBox1.Sorted:=True;
    for i := 1 to ListBox1.ItemIndex do
       if ListBox1.Items[i] := s then
           break;
    ListBox1.ItemIndex := i;

For no duplicate:

var i : integer;
    can_add : boolean;
begin
    can_add := true;
    for i := 1 to ListBox1.ItemIndex do
       begin
       if ListBox1.Items[i] := newval then
           begin
           can_add := false;
           break;
           end;
       end;
    if can_add then
       ListBox1.Items.Add[newval];

......

0
 
LVL 10

Expert Comment

by:viktornet
ID: 1354334
Here is the simplest way,,,

ListBox.Items[ListBox.ItemIndex] := 'Whatever';
ListBox.Sorted := False;
ListBox.Sorted := True;

Now everything would be sorted....

If oyu dont want to have duplicates in your listbox as slautin prposed, you can do one of two ways....

the first is this,,,,

if ListBox.Items.IndexOf('Whatever') < 0 then begin
  ListBox.Items[ListBox.ItemIndex] := 'Whatever';
  ListBox.Sorted := False;
  ListBox.Sorted := True;
end;

This was the first way w/o having duaplicates.... The second way is to do this...

var
  List : TStringList;
begin
  List := TStringList.Create;
  List.Assign(ListBox.Items);
  List.Duplicates := dupIgnore;
  List.Sort := True;
  ListBox.Items.Assign(List);
  List.Free;
end;

This would remove the duplicates and will sort the items and put them back in the listbox...

Hope this helps...

-Viktor
--Ivanov
0
 

Author Comment

by:aztec
ID: 1354335
Great Thanks! And thanks to Viktor for his comments.

Shawn
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1354336
No problem... If you are still having problems let me know...
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

656 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