TEN,
Cita:
Empezado por TEN
...como hago para que un label me ponga la primer letra de cada palabra en mayuscula...
|
Revisa este código:
Código Delphi
[-]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
function CapitalLetterText(Sender: TObject) : String;
procedure Button2Click(Sender: TObject);
private
public
end;
const
MsgUsr : String = 'test de primera letra en mayúsculas en cada palabra de un label, edit o memo';
var
Form1: TForm1;
implementation
{$R *.dfm}
function CapitalLetterString(Value : String) : String;
var
SL : TStringList;
i : Integer;
begin
SL := TStringList.Create;
ExtractStrings([' '],[],PChar(Value),SL);
for i := 0 to SL.Count - 1 do
SL.Strings[i] := Uppercase(Copy(SL.Strings[i],1,1)) + Lowercase(Copy(SL.Strings[i],2,Length(SL.Strings[i])));
SL.Delimiter := ' ';
Result := SL.DelimitedText;
SL.Free;
end;
function TForm1.CapitalLetterText(Sender : TObject) : String;
var
SL1, SL2 : TStringList;
i,j : Integer;
AuxStr : String;
begin
if (Sender is TEdit) then
AuxStr := TEdit(Sender).Text;
if (Sender is TLabel) then
AuxStr := TLabel(Sender).Caption;
if (Sender is TEdit) or (Sender is TLabel) then
begin
SL1 := TStringList.Create;
ExtractStrings([' '],[],PChar(AuxStr),SL1);
for i := 0 to SL1.Count - 1 do
SL1.Strings[i] := Uppercase(Copy(SL1.Strings[i],1,1)) + Lowercase(Copy(SL1.Strings[i],2,Length(SL1.Strings[i])));
SL1.Delimiter := ' ';
Result := SL1.DelimitedText;
SL1.Free;
end;
if (Sender is TMemo) then
begin
SL1 := TStringList.Create;
SL2 := TStringList.Create;
for i := 0 to TMemo(Sender).Lines.Count - 1 do
begin
AuxStr := TMemo(Sender).Lines.Strings[i];
ExtractStrings([' '],[],PChar(AuxStr),SL1);
for j := 0 to SL1.Count - 1 do
SL1.Strings[j] := Uppercase(Copy(SL1.Strings[j],1,1)) + Lowercase(Copy(SL1.Strings[j],2,Length(SL1.Strings[j])));
SL1.Delimiter := ' ';
SL2.Add(SL1.DelimitedText);
SL1.Clear;
end;
Result := SL2.Text;
SL1.Free;
SL2.Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Label1.Caption := MsgUsr;
Edit1.Text := MsgUsr;
Memo1.Lines.Add('l1 ' + MsgUsr);
Memo1.Lines.Add('l2 ' + MsgUsr);
end;
procedure TForm1.FormShow(Sender: TObject);
begin
Button1.SetFocus;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Label1.Caption := CapitalLetterText(Label1);
Edit1.Text := CapitalLetterText(Edit1);
Memo1.Lines.Text := CapitalLetterText(Memo1);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
MessageDlg(CapitalLetterString(MsgUsr),mtInformation,[mbOK],0);
end;
end.
El código anterior en Delphi 7 sobre Windows 7 Professional x32,
Permite poner en mayúscula la primera letra de cada palabra de un Edit, Label, Memo o String según se muestra en la siguiente imagen:
Espero sea útil
Nelson.