Sign in Join
Wiki copytemporary

TEdit: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: TEdit

OldNewLine
1+A **TEdit** ![](/wiki-images/d/db/Tedit.png) is a control with a single line of editable text. It is available from the [[Standard tab]] of the [[Component Palette]].
2+
3+## Usage
4+
5+You can add a TEdit text input field to your [[TForm|form]] by clicking on it in the Standard tab of the component palette and place it on your form with one click. You can now edit this single-line text field at run time.
6+
7+If you want to use this text elsewhere, you can use it like every other [[String]].
8+
9+For example, if you have a TEdit *Edit1* on your form, you can use `myString := Edit1.Text;`. To change the shown text in *Edit1*, you can use `Edit1.Text := myString;`.
10+
11+If you want to display another text instead of the default text (e.g. *Edit1*) in a TEdit *Edit1* at the start of your application, you can proceed as follows:
12+
13+- Select the TEdit on your form with one click.
14+- Go to the properties tab in the Object Inspector.
15+- Select the property **Text** and change it in the neighbouring input field.
16+- In the same way, you can select the property **Name** and give the TEdit a better name.
17+
18+### Password input
19+
20+You can also easily use a TEdit to enter passwords. Then instead of the actually entered [[Char]] a PasswordChar is displayed.
21+
22+Little Example:
23+
24+- Create a new GUI application with a TEdit *Edit1* and a TButton *Button1* on the form.
25+- In the Object Inspector set the property PasswordChar of *Edit1* to am asterisk ( * ).
26+- In the event handler of *OnClick* of *Button1* show the entered password: `ShowMessage(Edit1.Text);`.
27+
28+### TextHint
29+
30+Have you used a GUI that has, for example, a Search Box where you can type in a search term. And when unused, that Search Box displays something like "Search Here" so you know what it is for ? Well, TEdit has a property called TextHint for that purpose. Set TextHint to some appropriate text and if the normal 'Text' is empty and the Control is not in use, your Search Here message is displayed. On most Widget Sets, the TextHint is automatically cleared once the user starts typing in there. The exception is GTK2/3, they like to clear things as soon as the Control gets focus.
31+
32+The text from TextHint is displays a little greyer than normal text so the user is aware it is somehow special. Most people find this interface quite intuitive.
33+
34+## Requested Features
35+
36+### Embedding Button/Other Controls in TEdit
37+
38+It's a WinAPI feature to allow embedding any sort of control into a TEdit.
39+
40+Considerations.
41+
42+- The feature is not supported by all Widgetsets natively. For example: for Gtk2/Gtk3 it's not possible, as GtkEntry is not derived from a container. The SpinEdit control is a special kind of control in Gtk2. (This is the main reason on why [[TSpinEdit]] has its own WS class).
43+
44+- The placement of an embedded control (or controls) cannot be set by Left or Top properties (as normally happens). Instead the "Align" property should be used.
45+
46+```delphi
47+button1.Parent:=edit1;
48+button1.Width:=20;
49+button1.Align:=alRight;
50+button1.Constraints.MaxHeight:=18; //edit1.Height;
51+```
52+
53+- The "text" entry field must be limited by some TEdit property
54+
55+
56+OR this should be done automatically by the widgetset.
57+
58+Without such a limitation, the entered text goes "underneath" the embedded button.
59+
60+<figure>
61+<img src="edit1.gif" title="edit1.gif" />
62+<figcaption>edit1.gif</figcaption>
63+</figure>
64+
65+If using pure WinAPI, this can be achieved by using an EM_SETMARGINS message:
66+
67+```delphi
68+Uses
69+ Windows, ...
70+
71+...
72+
73+ SendMessage(Edit1.Handle, EM_SETMARGINS, EC_LEFTMARGIN or EC_RIGHTMARGIN, MakeLParam(0, button1.Width))
74+```
75+
76+<figure>
77+<img src="edit2.gif" title="edit2.gif" />
78+<figcaption>edit2.gif</figcaption>
79+</figure>
80+
81+The Delphi VCL does provide a Margins property, but it is not mapped to EM_SETMARGINS and has a different meaning in the VCL.
82+
83+## See also
84+
85+- [[doc:lcl/stdctrls/tedit.html|TEdit doc]]
86+- [[TMemo]] - A multiline text edit box
87+- [[TLabeledEdit]] - An edit field like a TEdit with a label
88+- [[TMaskEdit]] - An edit field with an edit mask
89+- [[TSpinEdit]] - An edit field for integers defined numbers range limits
90+- [[TFloatSpinEdit]] - An edit field for tiles floating-point numbers with defined numbers range limits
91+- [[TEditButton]] - An edit field like a TEdit with an attached button as a [[TSpeedBtn]]
92+- [[TFileNameEdit]] - An edit field with attached [[TOpenDialog]], to enter a file name
93+- [[TDirectoryEdit]] - An edit field with attached [[TSelectDirectoryDialog]], to enter a file path
94+- [[TDateEdit]] - An edit field with attached [[TCalendarDialog]] (calendar), to enter a date
95+- [[TCalcEdit]] - An input field with attached [[TCalculatorDialog]] (Calculator), to enter a number
96+- [[TTIEdit]] - An RTTI capable input field
97+
98+---
99+*From the Free Pascal wiki, page "TEdit", as it stood on 2022-12-06 after a change by Dbannon, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history