The Dynamics AX Options from Tools
menu allows users to define their preferred fonts and sizes for various
application areas. In custom functionality, it might also be necessary
to add similar options allowing configuring font properties.
In this recipe, we will add an option to select a font in the Form setup form in the Accounts receivable
module for sales invoice layout. The code in this recipe could be used
in conjunction with the code that reads this parameter and actually
changes invoice font.
How to do it...
1. Open the CustFormLetterParameters table in AOT.
2. Add a new field:
Property
|
Value
|
---|
Type
|
String
|
Name
|
InvoiceFontName
|
ExtendedDataType
|
FontName
|
3. Add one more field:
Property
|
Value
|
---|
Type
|
Integer
|
Name
|
InvoiceFontSize
|
ExtendedDataType
|
FontSize
|
4. Open the CustFormLetterParameters form in AOT, and add a new display method to the CustFormLetterParameters data source:
display Name displayInvoiceFont(
CustFormLetterParameters _custFormLetterParameters)
{;
if (CustFormLetterParameters.InvoiceFontName)
{
return strfmt(
"%1, %2",
_custFormLetterParameters.InvoiceFontName,
_custFormLetterParameters.InvoiceFontSize);
}
return "";
}
5. Add a new group to the Invoice tab page right after the GroupInvoice group:
Property
|
Value
|
---|
Name
|
GroupFont
|
Caption
|
Font
|
Columns
|
2
|
Columnspace
|
0
|
6. Add a StringEdit control to the newly created group:
Property
|
Value
|
---|
Name
|
InvoiceFont
|
AutoDeclaration
|
Yes
|
Label
|
Font name & size
|
DataSource
|
CustFormLetterParameters
|
DataMethod
|
displayInvoiceFont
|
7. Add a new Button to the same group:
Property
|
Value
|
---|
Name
|
InvoiceFontLookup
|
ButtonDisplay
|
Image only
|
NormalResource
|
2633
|
8. Override clicked() on the InvoiceFontLookup button with the following code:
void clicked()
{
container font;
;
font = WinAPI::chooseFont(
element.hWnd(),
SysFontType::ScreenFont,
CustFormLetterParameters.InvoiceFontName,
CustFormLetterParameters.invoiceFontSize);
if (conlen(font))
{
CustFormLetterParameters.InvoiceFontName =
conpeek(font,1);
CustFormLetterParameters.InvoiceFontSize =
conpeek(font,2);
InvoiceFont.update();
}
}
9. Here is how it looks in AOT after all the modifications have been done:
10. To test the results, open Accounts receivable | Setup | Forms | Form Setup, and click on the Font lookup button on the Invoice tab page:
11. Upon its closure, the lookup fills in the Font name & size field with the selected values:
How it works...
First, we create two new fields in the CustFormLetterParameters table for storing font name and size. We use standard FontName and FontSize extended data types to make sure that the fields inherit the correct properties.
Next, we modify the CustFormLetterParameters form. We will be adding the Font name & size control, which is bound to the display method displayInvoiceFont(). The method resides on the CustFormLetterParameters
form data source and is responsible for displaying font name and size
in one line separated by a comma. In this way, we save some form layout
space instead of displaying font name and size in separate fields.
Dynamics AX does not have a standard font control so we "fake" it by placing a StringEdit field followed by a Button
control together in one form group. We need to set few of the group's
properties to make sure it has two columns, and there is no space
between group elements, so the user will see those two controls as one.
The last thing to do is to
modify the button. To make sure that it looks exactly like existing
Dynamics AX font controls, we change its properties so that the
appearance changes to a small three dot button. The clicked() method has to be overridden with the code that calls the lookup. Here, we use chooseFont() method of WinAPI application class to display the standard Windows font selection dialog. This method accepts four arguments:
1. Current window handler.
2. Selection between screen or printer font.
3. Current font name, which will be preselected in the lookup.
4. Current font size, which will be preselected in the lookup.
The chooseFont()
method returns a container of two elements, where the first one is font
name and the second one is a size. Note that although the font
selection dialog contains Font style section, the style is not returned by this method.