[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 296
  • Last Modified:

Capitalizing the first letter of each word.

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
d4jaj1
Asked:
d4jaj1
  • 12
  • 7
1 Solution
 
anilmsCommented:
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
 
d4jaj1Author Commented:
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
 
ZifNabCommented:
Look at the already aswered questions! There is somewhere one which solves this one too.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
ZifNabCommented:
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
 
d4jaj1Author Commented:
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
 
ZifNabCommented:
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
 
ZifNabCommented:
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
 
d4jaj1Author Commented:
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
 
d4jaj1Author Commented:
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
 
ZifNabCommented:
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
 
ZifNabCommented:
Sorry, mistake, won't work
0
 
ZifNabCommented:
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
 
ZifNabCommented:
And? Found something? If not, can you send the combobox component to me? Then I can have a look at it.
0
 
ZifNabCommented:
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
 
ZifNabCommented:
Still problems??????? Need some help?
0
 
d4jaj1Author Commented:
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
 
ZifNabCommented:
Can you send the layout of such a problem form? Or just the source-code? Then I can have a look.
0
 
d4jaj1Author Commented:
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
 
ZifNabCommented:
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
 
d4jaj1Author Commented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 12
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now