943,981 Members | Top Members by Rank

Ad:
You are currently viewing page 1 of this multi-page discussion thread
Nov 18th, 2007
0

A few questions to finish up my project

Expand Post »
All the help I have received in here has been wonderful and I am very glad that there are good people in here to help us newbies. I know I sure need it!

So these last few things would be nice to get some help with and then I will be done with this list modifier I have been working on for too long now. haha

1: Trying to sort a list from Z to A. I have A to Z done. I will post code if requested.

2: Sort a list numerically without it being sorted lexicographically. (thanks for that term Duoas)

3: Capitalize the first letter in a list of names... and/or capitalize all first letters on each word.
~I had some help from Duoas on that but I dont seem to be able to get it right. I cant seem to implement this on a button click. I have been trying all afternoon to do this and other things in this list i am writing up. -- s[ 1 ] := UpCase( s[ 1 ] );

4: Take each line in a loaded list and put it in reverse order. (i.e. 12345 will be 54321)

Thats about it. If this is too much for one post or I am not allowed to do this sort of thing, I will close this thread or a moderator can. I don't want to put silly questions in here at all.

Thanks for any help. BTW, this is all being done in a ListView.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007
Nov 19th, 2007
0

Re: A few questions to finish up my project

Yes , please post the code first.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Nov 19th, 2007
0

Re: A few questions to finish up my project

Code is as follows:

1: This is what I posted for the alphabetical and somenumerical values in the list that is loaded.

delphi Syntax (Toggle Plain Text)
  1. procedure TMainForm.SortListButtonClick(Sender: TObject);
  2. begin
  3. ListView1.SortType := stBoth;
  4. CurrentStatusLabel.caption := 'List Sorted';
  5. end;

I would like to also sort it from z - a... that is the problem atm...

2: Sort a list numerically... tried stdata..

delphi Syntax (Toggle Plain Text)
  1. ListView1.SortType := stData;

this does not truly sort numerically...

3: Capitalize the first letter in a list or all first letters in a list... no idea. Not much luck on reading up on it. Most code I saw was applied to other edit boxes and not tlistview.

4: Reversing the order of a line in listview.. Cant get that to work either and looking it up hasn't afforded much help either. I was given this to start off with.

delphi Syntax (Toggle Plain Text)
  1. s[ 1 ] := UpCase( s[ 1 ] );

so there you have it. im new

Thanks for any help...
Last edited by squidd; Nov 19th, 2007 at 2:39 am.
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007
Nov 19th, 2007
0

Re: A few questions to finish up my project

Click to Expand / Collapse  Quote originally posted by squidd ...
Code is as follows:

1: This is what I posted for the alphabetical and somenumerical values in the list that is loaded.

delphi Syntax (Toggle Plain Text)
  1. procedure TMainForm.SortListButtonClick(Sender: TObject);
  2. begin
  3. ListView1.SortType := stBoth;
  4. CurrentStatusLabel.caption := 'List Sorted';
  5. end;

I would like to also sort it from z - a... that is the problem atm...

2: Sort a list numerically... tried stdata..

delphi Syntax (Toggle Plain Text)
  1. ListView1.SortType := stData;

this does not truly sort numerically...

3: Capitalize the first letter in a list or all first letters in a list... no idea. Not much luck on reading up on it. Most code I saw was applied to other edit boxes and not tlistview.

4: Reversing the order of a line in listview.. Cant get that to work either and looking it up hasn't afforded much help either. I was given this to start off with.

delphi Syntax (Toggle Plain Text)
  1. s[ 1 ] := UpCase( s[ 1 ] );

so there you have it. im new

Thanks for any help...
Squidd, whenever you look up Delphi help - e.g. for SortType - also make sure that you check out the entries under See Also.

In this instance what you need is to checkout the OnCompare. event. You can use this event to alter the way the sort is being done. Here is some, untested, code that should do your reverse sort

Pascal and Delphi Syntax (Toggle Plain Text)
  1. procedure TMaster.WhenLVCompare(Sender:TObject;Item1,Item2:TListItem;Data:Integer;var Compare:Integer);
  2. begin
  3. Compare:=CompareText(Item2.Caption,Item1.Caption);
  4. end;

As to the problem with capitalization - remember that you will need to update the corrected entry in the listview. Thus if you have the code

Pascal and Delphi Syntax (Toggle Plain Text)
  1. procedure TMaster.InCapLV;
  2. var i:Integer;
  3. s:String;
  4. AList:TListItems;
  5. begin
  6. AList:=lvOne.Items;
  7. with AList do
  8. for i:=0 to Count - 1 do
  9. begin
  10. s:=Item[i].Caption;
  11. s[1]:=UpCase(s[1]);
  12. Item[1].Caption:=s;
  13. end;
  14. end;

you should get the result you desire.

As I have discussed before this is not really the right way to deliver speed nor to write good code. However, I guess that is the learning process you are currently going through.

p.s. - I guess I should have commented on the OnCompare event and the CompareText method

a. By default some Delphi controls, such as ListView, and controls, such as TStringList, know how to do a sort.
b. If you don't like the default sort order the offer - typically A -> Z - you can alter that by writing the OnCompare event.
c. CompareText is a unit in SysUtils - spend some time looking at the routines available in SysUtils. Could save you a lot of time. Here is what Delphi help says regarding CompareText

CompareText compares S1 and S2 and returns 0 if they are equal. If S1 is greater than S2, CompareText returns an integer greater than 0. If S1 is less than S2, CompareText returns an integer less than 0. CompareText is not case sensitive and is not affected by the current locale.


I think you will be able to figure out the rest.
Last edited by ExplainThat; Nov 19th, 2007 at 3:30 am.
Reputation Points: 32
Solved Threads: 7
Junior Poster in Training
ExplainThat is offline Offline
69 posts
since Sep 2007
Nov 19th, 2007
0

Re: A few questions to finish up my project

Working on it now.. Actually have been working on it for the last 2 hours.

I implemented your sample code for Capitalizing the first letter, but instead of doing all of them in the list, it caps the last line and moves that line up to and writes over the second line in a loaded list.

Looking into that right now. Thanks you for your help. And I am still using ListView1 for the name as that is the only LV i have in this program. I am not disregarding your advice, but the name ListView1 for this program is comfortable for my purposes. Thank you for your help on this.

The other questions you ahev answered will be addressed after I correct this one. One thing at a time.
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007
Nov 19th, 2007
0

Re: A few questions to finish up my project

I found the problem with the capitalization of the first letter.

the line:

Item[1].Caption := s;

should be

Item[i].Caption := s;

now it works perfectly.


Also, I am going to try and capitalize any first letter in my list now. Thanks again for your help... You too Duoas!
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007
Nov 20th, 2007
0

Re: A few questions to finish up my project

I have the list capitalizing as it should now.. the code is probably poor, but it works...

I will now work on reversing the items in my listview.. but that will wait until tomorrow, I have spent a lot of time going back and undoing error correction the didnt work.

Thanks again!
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007
Nov 20th, 2007
0

Re: A few questions to finish up my project

How do I get a function to work when I click a Button on my form?

delphi Syntax (Toggle Plain Text)
  1. function StringReverse(const AString : string): string;

I thought I was to put this up in private declarations. Then add a button to perform a stringreverse procedure.

delphi Syntax (Toggle Plain Text)
  1. procedure TMainForm.ReverseOrderButtonClick(Sender:TObject);
  2. var
  3. s : string;
  4. begin
  5. s := Reversestring(s);
  6. end;

Obviously, this isnt working for me. I dont know how to get a button click to do a function listed elsewhere. AGH!

Thanks for any help.
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007
Nov 20th, 2007
0

Re: A few questions to finish up my project

What you have written looks right. But just to make sure you got there correctly (which makes all the difference):

1. You added a button to your form and named it "ReverseOrderButton".
2. You either a) double-clicked the button on your form, or b) changed the object inspector to list events and double clicked the "OnClick" item
3. You added your code.

Yes, it does something. However, in the example posted you reverse an uninitialized local string. That is, a string that only exists in the ReverseOrderButtonClick function and which you did not s := "hello"; before reversing, and which you have not displayed before the procedure quits and the string disappears. So, while you have reversed the string '', it never gets displayed and it appears that nothing happens.

Try this to see what it does:
Delphi Syntax (Toggle Plain Text)
  1. procedure TMainForm.ReverseOrderButtonClick( Sender: TObject );
  2. begin
  3. with ReverseOrderButton do
  4. Caption := StringReverse( Caption )
  5. end;
The same is true for any Item[ x ].Caption.

Hope this helps.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 20th, 2007
0

Re: A few questions to finish up my project

delphi Syntax (Toggle Plain Text)
  1. private
  2.  
  3. function StringReverse(const AString : string): string;

this wont stop giving me the following error:

E2065 Unsatisfied forward or external declaration: 'TMainForm.StringReverse'

I dont know why its doing that.
Reputation Points: 10
Solved Threads: 2
Junior Poster
squidd is offline Offline
100 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: Shorting code lines within one loop
Next Thread in Pascal and Delphi Forum Timeline: Problem with dbExpress





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC