Solved

Capitalizing the first letter of each word.

Posted on 1997-12-01
20
283 Views
Last Modified: 2010-04-04
I use several Comboboxes and I want to captitalize teh first letter of each word while the user types.  Does anyone have code to handle this?  I'm currently using if Combo1.SelStart = 0 then key := UpperCase(string(key))[1];
0
Comment
Question by:d4jaj1
  • 12
  • 7
20 Comments
 
LVL 1

Expert Comment

by:anilms
ID: 1352594
There are two ways of making this work the way you want. The first one is to derive a new Combo Box and in the Text property's write method, make the first character as an upper case character. In fact, I am supposed to develop such a ComboBox component next week (as a part of my project) and I would be able to give it to you after it is finished.

The second way is to hard code it for every combo box as given below:

procedure TForm1.ComboBox1Change(Sender: TObject);
var tmpstr:string;
begin
     if length(ComboBox1.Text)>1 then
     begin
          tmpstr := ComboBox1.Text;
          tmpstr[1] := upcase(tmpstr[1]);
          ComboBox1.Text := tmpstr;
     end
end;

0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1352595
Your second suggestion didn't work!  It did capitaize teh first letter, but it put the cursor back at position 0 - before the character I already typed in.  Also, the rest of the aren't capitalized. I want each letter captialized as the user types, so if the position is 0 or after a ' ' (space), the letter should be capitalized.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352596
Look at the already aswered questions! There is somewhere one which solves this one too.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352597
Hi d4jaj1,

Sorry, my mistake, gives also only first character, not after space. Here is my code, maybe it can better, but this works. Improvement is up to you!

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
var tmpstr : string;
begin
 tmpStr := Combobox1.text;
 if (Combobox1.SelStart = 0) or
  (tmpStr[ComboBox1.SelStart]=' ') then Key := UPCASE(Key);
end;

Regards,
BanZif;

P.S. Is this still for the same program? Big database?

Have fun!
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1352598
Zif,

I get an error when I try to compile your code.  It says undeclaried Indentifier - Key.  I tried to change 'Key' in the code to tmpstr and got another error - incompatable types - PChar & string.  Does the code work for you?

Yes, it for my big database program, scheduled to be finished COB (Close of Business) tomorrow.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352599
Hi d4jaj1,

Yes the code works for me. I tried it out (on D2).
Undeclaried Identifier Key? Do you use KeyPress event? There is a Key declared in the header. (So it should be there! The only way I can figure out why you get an error like that is that you don't work with KeyPress event or you got another version of Delphi (strange that it's changed) or you got a strange combobox component).

If in the other version this doesn't exist anymore, you should look for an event which is activated when a key is pressed!

c.u.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352600
Don't believe me? If not, here is code :

 Just a form with a combobox, named combobox1. That's all you need.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls;

type
  TForm1 = class(TForm)
    ComboBox1: TComboBox;
    procedure ComboBox1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.ComboBox1KeyPress(Sender: TObject; var Key: Char);
var tmpstr : string;

begin
 tmpStr := Combobox1.text;
 if (Combobox1.SelStart = 0) or
  (tmpStr[ComboBox1.SelStart]=' ') then Key := UPCASE(Key);
end;

end.

0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1352601
Thanks Zif!  I accidentally put the code in teh KeyDown event instead of KeyPress.  It works now.  You said it could be better - but I can't see what's wrong with it?  Does it not work under certain situations?

Anilms,

I'm opeing the question up to Zifnab since he provided me with the additional feedback and the correct answer.  Thanks for your efforts.
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1352602
I'm using a custom Combobox called HistoryCombo, which saves your entries to a text file so they are avilable teh next time you use the drop down box.  For some reason, it won't take the tmpStr := Combobox1.text; section, but it will allow you to upcase based on the position of the cursor.  So selstart = 0 will work.  Is there any (other) way to get the previous position and determine if it is a string - selstart - 1?

0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352603
What about tmpStr := Combobox1.SelText;

Thats an improvement and it should work now. Can you give me a sign very soon? Have to go, but can still wait a few minutes.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 8

Expert Comment

by:ZifNab
ID: 1352604
Sorry, mistake, won't work
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352605
Hi d4jaj1,

Have to go now, sorry, I'll look at it.

You can't use tmpStr := ComboBox1.text, probably because it's internally used? Aren't there other properties where yuo can get the last written text in this component? Like last input word in the history? Does the component work with a stringlist? Then it should be easy to work around the problem, that way.

Regards,
ZifNaf.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352606
And? Found something? If not, can you send the combobox component to me? Then I can have a look at it.
0
 
LVL 8

Accepted Solution

by:
ZifNab earned 50 total points
ID: 1352607
Hi d4jaj1,

10x for the historycombo box! I just tried it out, but it works fine with me! Don't have to change anything to the previous answer I gave you? It doesn't gives me a compiling error or (in other words) tempStr := Combobox1.text works!
--> Maybe in your hurry, you forgot to change the name? (Combobox1 has to be changed to the name of the component, here it's HistoryComboBox1)  I can't think of it that you didn't know this, but stress perhaps ;-)?

procedure TForm1.HistoryComboBox1KeyPress(Sender: TObject; var Key: Char);
var tmpstr : string;
begin
  tmpStr := HistoryCombobox1.text;
  if (HistoryCombobox1.SelStart = 0) or
  (tmpStr[HistoryComboBox1.SelStart]=' ') then Key :=     UPCASE(Key);
end;

Sorry, I'm stuck at this moment. It works fine here, so it should work with you as well. I know this is to late (you're program had to be finished already, I think). Sorry. If it still doesn't work, I still want to help you, but then we have to look for the fault somewhere else.

Have fun,
c.u. ZifNab;
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352608
Still problems??????? Need some help?
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1352609
Yea, it is the strangest thing.  If I create a new project, it works okay.  On my existing project, it works okay - until I hit backspace at position 0.  I then get an access violation.  This is the same form my cursor disappears on. I'm starting to beleiev it is due the PageControl component.  My cursur disappears on all of my projects where this is used.  I really don't know what to do.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352610
Can you send the layout of such a problem form? Or just the source-code? Then I can have a look.
0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1352611
I don't think layout or source is teh problem, it has to be in the app I've written. Corrupted something? I can send it to you - you just tell me which why you prefer. I can email it to you - (about 2.5 meg) or you can download it from my website.  If you choose teh second option, let me know because teh 'bug' version isn't on my site - I'll hav eto put it there in a special directory.  Thanks.
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1352612
Hi d4jaj1,

It 's up to you, what the best way for sending me the file is. Maybe it's difficult by e-mail. 2.5meg (but normally it shouldn't)

0
 
LVL 3

Author Comment

by:d4jaj1
ID: 1352613
Zif,

I FINALLY figured it out (or at least got it to work).  Your code sample worked fine until the user hit the Backspace key in position 0 or 1 - then POW! - the dreaded Access Violation.  I think the problem was in the app trying to assign a value to the string (tmpstr) when there was nothing to assign (cursor at position 0).  

The way I finally got it to work is to not assign a value to  tmpstr unless the position wasn't 0.  This way, I won't try to assign a string that doesn't exist.  I wonder if you would get an access violation if you tried to use teh backspace key with teh old code?  Anyway, thanks for your help!

var
tmpstr: string;
begin
If edit1.SelStart = 0 then
  Key := UPCASE(Key)
else
  begin
   tmpStr := edit1.text;
   if tmpStr[edit1.SelStart]=' ' then
     Key := UPCASE(Key);
  end;
end;

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

15 Experts available now in Live!

Get 1:1 Help Now