Autor Beitrag
JayK
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1013



BeitragVerfasst: Fr 20.01.06 22:56 
Hallo,

wie kriege ich es hin, dass ein Button seine Größe immer der Länge seiner Caption anpasst (mitwächst), wenn sich diese ändert? Ich möchte keine neue Komponente erstellen, sondern nur wissen, wie ich zur Laufzeit dafür sorgen kann, dass das Teil die Größe nach dem Platzbedarf des Textes ändert.

Schonmal Danke im Voraus
JayK
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Fr 20.01.06 23:00 
Nur manuell, d.h nach Ändern von Caption Breite berechnen und setzen. Da ein Button keine eigenschaft autosize besitzt und es kein Event für die Änderung von caption gibt, kann das ein Button nicht selber.

_________________
Markus Kinzler.
JayK Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1013



BeitragVerfasst: Fr 20.01.06 23:38 
Ja das ist mir schon klar. Mir ging es vorrangig darum, wie ich denn die Breite nun berechnen kann. Ich hab da keine Idee :nixweiss:
mkinzler
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 4106
Erhaltene Danke: 13


Delphi 2010 Pro; Delphi.Prism 2011 pro
BeitragVerfasst: Fr 20.01.06 23:45 
user profile iconJayK hat folgendes geschrieben:
Ja das ist mir schon klar. Mir ging es vorrangig darum, wie ich denn die Breite nun berechnen kann. Ich hab da keine Idee :nixweiss:

Irgendwie mit (Button.Font.Size + <Abstand zwischen Zeichen>)*Length(Button.Caption) + 2*<Abstand Text zu Rand>

_________________
Markus Kinzler.
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Fr 20.01.06 23:47 
voherigen beitrag editiert.. bzw komplett ersetzt!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
procedure TForm1.Edit1Change(Sender: TObject);
begin
button1.Caption := edit1.Text;
 button1.Width := length(button1.Caption) * 10;
end;


allerdings ist das ergebnis wieder exponential...


Zuletzt bearbeitet von Marc. am Sa 21.01.06 00:19, insgesamt 1-mal bearbeitet
Lannes
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2352
Erhaltene Danke: 4

Win XP, 95, 3.11, IE6
D3 Prof, D4 Standard, D2005 PE, TurboDelphi, Lazarus, D2010
BeitragVerfasst: Sa 21.01.06 00:16 
Hallo,

die Prozedur setzt die Button-Größe in Abhängigkeit zur Stringlänge und dem Font des Buttons:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
procedure SetButtonWidthHeight(aButton: TButton;S: String);
var B : TBitmap;
begin
  B := TBitmap.Create;
  B.Canvas.Font := aButton.Font;
  aButton.Width := B.Canvas.TextWidth(S)+14;
  aButton.Height := B.Canvas.TextHeight(S)+14;
  aButton.Caption := S;
  B.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  SetButtonWidthHeight(Button1,'Ein Schalter');
end;

_________________
MfG Lannes
(Nichts ist nicht Nichts) and ('' <> nil ) and (Pointer('') = nil ) and (@('') <> nil )
JayK Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1013



BeitragVerfasst: Sa 21.01.06 16:07 
Dankesehr, das klappt wunderbar :-D