Recently used in the development of the UpDown this control, but because it was not used before, so very unfamiliar, so I wrote a simple demo to learn UpDown and its combination of tedit usage.
Introduction to preliminary common functions
The present (2015.08.07) only involves its simple usage, because the only thing I need to use right now is that the systematic use and knowledge will be added later when needed.
The content currently involved includes the use of UpDown components with Tedit components , andthe position properties of UpDown .
First look at the operation of this program:
Figure 1 The interface at the start of the program
Figure 2 Effect of program operation
Simply explain to the program: to achieve the two addend add, you can either directly enter a number in two edit boxes, or through the UpDown component to step (step number is 1) to increase or decrease the number in the edit box , and then click the button will show the added information below.
Program Analysis:
This example involves the program to make extremely simple, but need to explain, because this is a small demo, so I may not pay attention to the components, variables of the naming specification, this is very bad habit, in the real project is absolutely forbidden, in fact, should also be from the usual small demo writing time to pay attention to, So again with my this paragraph code for a negative!
Unit unit1;interfaceuses Windows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, C Omctrls;type TForm1 = Class (Tform) Ud1:tupdown; Edt1:tedit; Lbl1:tlabel; Lbl2:tlabel; Lbl3:tlabel; Edt2:tedit; Ud2:tupdown; Lbl4:tlabel; Lbl5:tlabel; Lbl6:tlabel; Lbl7:tlabel; Btn1:tbutton; Lbl8:tlabel; Procedure Btn1click (Sender:tobject); Procedure Formcreate (Sender:tobject); Private {Private declarations} public {public declarations} End;var form1:tform1;implementation{$R *.dfm}pro Cedure tform1.formcreate (sender:tobject); Begin UD1. Associate:= edt1; When the form is created, by setting the UD1 's associate property to EDT1 and connecting the two components together, you can increase or decrease the number of EDT1 by UD1. Associate:= EDT2; function Ibid. end;procedure tform1.btn1click (sender:tobject); var Res:integer; Plus1:integer; Plus2:integer;begin plus1:= strtoint (edt1. Text); You can use this method to get the value in the edit box//tedit The Text property is String type plus2:= ud2. Position; Because UD2. Associate:= EDT2; So Ud2 and EdT2 are linked together, so you can also get the value in the edit box in this way//UpDown Position property is smallint type res:= Plus1 + plus2; Lbl4. caption:= IntToStr (PLUS1); Lbl6. caption:= IntToStr (PLUS2); Lbl8. caption:= IntToStr (res); end;end.
The code is simple, and the knowledge points that need to be explained are also explained in the code by annotations.
Thinking about the object inspector of the component
To say more, I was explicitly declaring ud1 in a method created by the form. Associate:= edt1; As well as the combination of UD2 and EDT2, there is a way to UD1 and edt1 for example, is in the graphical interface design interface to select Ud1, and then go to the Object inspector this configuration interface to configure UD1 related properties, of course, you can configure UD1 associate property, set it to EDT1 so that it does not need to be declared in the form's creation method by code. (You can either get the value in the edit box through the Edt1 's Text property, but note that it is a string type, or you can get the value in the edit box through the Ud1 position method, position is the smallint type
Here is the object inspector is to illustrate, there may be times when you look at some code, found that a lot of things are not in the code, but the runtime seems to be effective, perhaps because it is in the object inspector inside the configuration, Instead of the explicit description in the code.
For another example of Object inspector, see the Tedit, tlable, and other components of the Delphi graphical interface to manually drag and drop a fixed size, but show a different situation after compilation
Limit Tedit to enter only numbers
In conjunction with Tedit and UpDown, it is often necessary to restrict the input of integers only in tedit, and to prevent users from mistakenly entering Chinese characters or letters, you need to implement this restriction in your code.
Here are the restrictions only in the edit box to enter the number (decimal point, '-' number, number) method, such as the limit can only enter a positive integer, the limit can only enter an integer ... Method can refer to the implementation.
Procedure tform1.edit1keypress (Sender:tobject; var key:char); var edt:tedit; STR, StrL, strr:string; P:integer;begin //Get the current text content, and be careful to remove the selection (because it will be overwritten). EDT: = Tedit (Sender); str: = Edt.text; if Length (EDT). SelText) <> 0 THEN begin StrL: = Leftstr (Edt.text, EDT. SelStart); STRR: = Rightstr (Edt.text, Length (Edt.text)-EDT. Selstart-edt. SelLength); STR: = StrL + strr; End; Limit input number/decimal point/BACKSPACE if not (Key in [#8, #13, #127, '. ', '-', ' 0 ' ... ') 9 '] then Key: = #0; Limit only one decimal point if Key = '. ' Then BEGIN P: = Pos ('. ', EDT. Text); If p > 0 then Key: = #0; End; The limit can only be entered in the first bit and only one '-' if Key = '-' THEN BEGIN if EDT. SelStart > 0 Then Key: = #0; P: = Pos ('-', EDT. Text); If p > 0 then Key: = #0; end;end;//to uses Strutils unit if there is a lot of tedit in the program to do this limit, of course, you do not have to write code for each control, the event is assigned to the same process.
Note that the Tform1.edit1keypress method is not directly you can declare in the Code Editor, you need to go through the edit box component of the object inspector inside, find events->onkeypress this event, such as
Then double-click on this event, the declaration of the method will appear in the Code Editor, then you can edit it, and then see the edit box component of the object inspector inside the onkeypress event, there is already a method, such as:
This is also a method that many other components use when writing the events of a component.
Introduce more commonly used properties and methods of UpDown
The---UpDown of the Delphi control and its use with tedit (such as restricting the tedit to enter only numbers)