Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 01-05-2017, 01:47 PM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 368
Default First Letter Capital

How to get First letter of first word capital in each line of Memo.
Reply With Quote
  #2  
Old 01-05-2017, 09:18 PM
Marsheng Marsheng is offline
Senior Member
 
Join Date: Nov 2008
Posts: 278
Default

Loop through the lines
use Copy to get the 1st letter of each line
Check its ASCII valve and change if necessary.

Cheers Wallace
Reply With Quote
  #3  
Old 01-06-2017, 01:06 AM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 158
Default

The following capitalizes the first character you type in the memo, any character you type after the enter key or after a period/exclamation point or question mark.
Code:
type
  TForm1 = class(TForm)
    Memo1: TMemo;
    procedure Memo1KeyPress(Sender: TObject; var Key: Char);
  private
    function GetCharFromString(I: Integer; const S: String): Char;
    function DoCapitalize(CurrentCaretPos: Integer; const S: String): Boolean;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function TForm1.DoCapitalize(CurrentCaretPos: Integer; const S: String): Boolean;
var
   I: Integer;
   C: Char;
 begin
   Result := False;
   if (CurrentCaretPos = 0) or (trim(S) = '') then
     Result := True
   else
   begin
     for I := CurrentCaretPos downto 1 do
     begin
       C := GetCharFromString(I, S);
       if C in [' ', #9] then
         continue
       else if C in ['.','!','?',#10,#13] then
       begin
         Result := True;
         Break;
       end
       else
         Break;
     end;
   end;
 end;

function TForm1.GetCharFromString(I: Integer; const S: String): Char;
begin
  Result := S[I];
end;

procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
  if DoCapitalize(Memo1.SelStart, Copy(Memo1.Text, 1, Memo1.SelStart)) then
    Key := UpCase(Key);
end;

end.

Last edited by rojam; 01-06-2017 at 03:04 AM.
Reply With Quote
  #4  
Old 01-06-2017, 04:15 AM
robert robert is offline
Senior Member
 
Join Date: Sep 2014
Posts: 368
Default

Thanks a lot but there is no Key event for Memo1. Memo1 is already filled with Text lines and when I copy Memo1 lines to Memo2 then each line word should have first letter Capital in Memo2.
Reply With Quote
  #5  
Old 01-06-2017, 12:17 PM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 158
Default

Quote:
Originally Posted by robert View Post
Thanks a lot but there is no Key event for Memo1. Memo1 is already filled with Text lines and when I copy Memo1 lines to Memo2 then each line word should have first letter Capital in Memo2.
Then try to code what @Marsheng provided. If you get stuck, then ask.
Reply With Quote
  #6  
Old 03-12-2017, 08:07 PM
Zaaka Zaaka is offline
Senior Member
 
Join Date: Aug 2002
Location: Teesside, UK
Posts: 209
Default

Here is a gnarly solution to your question, The Capitalize function is not a stramline as it should be but gives you the just of what you need.

Code:
unit Unit2;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm2 = class(TForm)
    Memo1: TMemo;
    Memo2: TMemo;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

function Capitalize(s: string): string;
begin
  Result := '';
  s := LowerCase(s);
  Result := UpperCase(s[1]);
  Result := Result + trim(Copy(s, 2, 255));
end;

procedure TForm2.Button1Click(Sender: TObject);
begin
  Form2.Memo1.Lines.Add('');

  Form2.Memo1.Lines.Add('Lorem ipsum dolor sit amet, egestas lacus posuere');
  Form2.Memo1.Lines.Add('nascetur eu tellus suspendisse, nulla nullam a id');
  Form2.Memo1.Lines.Add
    ('vestibulum pellentesque rutrum. Pellentesque volutpat');
  Form2.Memo1.Lines.Add('justo varius ac elit. Neque amet magna pede laoreet,');
  Form2.Memo1.Lines.Add
    ('felis a dictum urna donec in. Scelerisque eleifend non');
  Form2.Memo1.Lines.Add
    ('etiam sed et vestibulum, ligula elit, eros id in ut metus porta magnis.');

end;

procedure TForm2.Button2Click(Sender: TObject);
var
  t: Integer;
begin
  for t := 0 to Memo1.Lines.count - 1 do
  begin
    if Memo1.Lines[t] <> '' then
      Memo2.Lines.Add(Capitalize(Memo1.Lines[t]));
  end;
end;

end.
Reply With Quote
  #7  
Old 03-13-2017, 11:07 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,179
Default

Copy is the bad solution, especially when you only copy 255 characters. Then you should copy using the Length(s). But it's easies to just uppercase the first character.
Code:
    s := LowerCase(s);
    s[1] := UpCase(s[1]);
    Result := s;
A string is nothing more then an array of Char, therefor I prefer this construction.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 01:59 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.