User:Mullinrj/Sandbox

From Vectorlab
Jump to: navigation, search
Underconstruction.png
This vcor page is currently under construction.

By: Raymond Mullin. Last edit: 2015.12.22 (YYYY.MM.DD)

In the category:Under construction you'll find a list of all pages currently marked as under construction.

Create a Modern Dialog with 3 text fields and a little error checking. By Raymond Mullin

{ 23 February 2007 - ©2007 Raymond Mullin. All rights reserved. }
{ Create a Modern Dialog with 3 text fields and a little error checking. }
PROCEDURE Sample3itemDialog;
CONST
	{ *** StaticText fields *** }
	kST_X = 11;
	kST_Y = 12;
	kST_Q = 13;
	
	{ *** EditText fields *** }
	kET_X = 21;
	kET_Y = 22;
	kET_Q = 23;

VAR
	dlogID, dlogResult :Longint; 	{ These are required for running the Modern Dialog. }
	X, Y, Q :Real;						{ These will hold the answers after the dialog closes. }


	function DefineDialog(DialogName :String) :Longint;
	{	Define the structure of the dialog and return an ID to reference it later. 
		This does not draw the dialog on the screen.
	
		there are 4 sections for defining a Modern Dialog:
		*** Section 1 - Create items
		*** Section 2 - Position items
		*** Section 3 - Align items
		*** Section 4 - Define Help text for items
		
		Notes : 
			Section 3 - Align Items is optional for simple dialogs, but necessary for complex ones.
			Section 4 - Define Help text for items is optional always, but strongly recommended.
	}
	
	Var
		dlogID :Longint;
	Begin
		{ *** Section 1 - Create items *** }
		dlogID := CreateLayout(DialogName, True, 'OK', 'Cancel');	{ create dialog window and ID }
		
		{ define the labels for the TextEdit fields }
		CreateStaticText(dlogID, kST_X, 'X:', 2);		{ Creates first item, but does not place it in dialog }
		CreateStaticText(dlogID, kST_Y, 'Y:', 2);		{ Creates second item ... }
		CreateStaticText(dlogID, kST_Q, 'Q:', 2);		{ Creates third item ... }
		
		{ define the EditText fields }
		CreateEditText(dlogID, kET_X, '', 16);		{ X }
		CreateEditText(dlogID, kET_Y, '', 16);			{ Y }
		CreateEditText(dlogID, kET_Q, '', 16);		{ Q }


		{ *** Section 2 - Place items *** }
		{ Labels for EditText fields }
		{ The first item - always goes in upper left corner. }
		SetFirstLayoutItem(dlogID, kST_X);			{ X: }
		SetBelowItem(dlogID, kST_X, kST_Y, 0, 0);		{ Y: }
		SetBelowItem(dlogID, kST_Y, kST_Q, 0, 0);		{ Q: }
		
		{ EditText Fields }
		SetRightItem(dlogID, kST_X, kET_X, 0, 0);		{ X - place to right of X: label. }
		SetRightItem(dlogID, kST_Y, kET_Y, 0, 0);		{ Y - place to right of Y: label. }
		SetRightItem(dlogID, kST_Q, kET_Q, 0, 0);		{ Q - place to right of Q: label. }


		{ *** Section 3 - Align EditText items *** }
		AlignItemEdge(dlogID, kET_X, 3, 1, 0);		{ X }
		AlignItemEdge(dlogID, kET_Y, 3, 1, 0);		{ Y }
		AlignItemEdge(dlogID, kET_Q, 3, 1, 0);		{ Q }
		

		{ *** Section 4 - Define Help strings *** }
		{ Help strings are not needed - but don't leave home without them. Meaning - always include them. }
			
		{ This item (0) is undocumented, but nice to know }
		SetHelpString(0, 'This displays when the cursor is in the dialog window, but not over any dialog item.');

		{ help string for OK button. }
		SetHelpString(1, 'Click this button to ACCEPT dialog settings.');
		
		{ help string for CANCEL button }
		SetHelpString(2, 'Click the button, or press ESC key, to CANCEL dialog and do nothing.');
		
		{ item specific help strings must be less than 256 characters. }
		SetHelpString(kET_X, 'Help for your first item - X.');
		SetHelpString(kET_Y, 'Help for your second item - Y.');
		SetHelpString(kET_Q, 'Ignore the noise.');

		DefineDialog := dlogID;		{ return the ID when you exit this routine. }
	End;		{ DefineDialog }



	procedure DialogDriver(var item: Longint; data :Longint);
	{	This routine is called by the Modern Dialog each time your dialog receives an event.
		Events consist of: 
			- typing a character,
			- deleting or pasting text, 
			- clicking on or tabing into a new field (NEW in version 12.5), 
			- clicking and changing a control. 
		The dialog manager quits when you exit this procedure with ITEM equal to 1 or 2. 
		When this routine exits with any other value for ITEM, the dialog manager waits for another event. 
	}
	Begin
		case item of
			SetupDialogC: begin
				{ Do all setup here. Initialize variables, set controls, etc.}
				{ This item runs ONCE before you get control of the dialog. }

				{ set initial field value to 3 - fields are always text, even numeric ones. }
				SetField(kET_X, Num2StrF(3));

				{ set initial field value to 2.2 - fields are always text, even numeric ones. }
				SetField(kET_Y, Num2StrF(2.2));

				{ set initial field value to Q - fields are always text, even numeric ones. }
				SetField(kET_Q, 'must be a #');		
			end;
			
			1: begin		{ OK was pressed, collect all data from dialog and get ready to exit. }
				{ This line moves first value to X }
				if ValidNumStr(GetField(kET_X), X) then

					{ This line moves second value to Y }
					if ValidNumStr(GetField(kET_Y), Y) then

						{ This line moves third value to Q }
						if ValidNumStr(GetField(kET_Q), Q) then begin
							{ *** do something here, if needed. *** }
						end
						else item := kET_Q	{ change item if Q data was wrong. }
					else item := kET_Y		{ change item if Y data was wrong. }
				else item := kET_X;			{ change item if X data was wrong. }
					
				 { Return cursor to offending field. }
				if (Item<>1) then SelField(Item);

				{ reset value to Q. }
				if (Item=kET_Q) then SetField(kET_Q, 'must be a #');
				end;

			2: begin end; 			{ Cancel button }
			
			kET_X, kET_Y: begin		{ First and second fields }
				{ 
				Not necessary to do anything here, but you can if you want to.
				This item will execute each time you enter this field, 
				or type or delete a character in this field.
				}
				end;
				
			kET_Q: SYSBEEP; 		{ Third field - BEEP - Just for grins }
		end;		{ case }
	End;		{ DialogDriver }



BEGIN
	{ defines the dialog to be called later in the program }
	dlogID := DefineDialog('Dialog Name');
	
	{ OK, this is later. Run the dialog until the OK or CANCEL button is pressed. }
	if VerifyLayout(dlogID) then 
		dlogResult := RunLayoutDialog(dlogID, DialogDriver);
		
	{ display values in the MESSAGE window when program completes. }
	if (dlogResult = 1) then 	{ 1 means OK button was pressed. }
		message('X = ', X, '   Y = ', Y, '    Q = ', Q);
		
END;		{ Sample3itemDialog }
Run (Sample3itemDialog);