This example creates a DFU-like utility for a given file.
It first displays this screen
Then, given a valid customer number displays this update panel
Its intent to show some basic techniques for working with data a fat-client Windows UI.
Using System
Using System.Text
Using System.Text.RegularExpressions
Using System.Windows.Forms
BegClass Form1 Extends(System.Windows.Forms.Form) Access(*Public) Partial(*Yes)
As a general rule, /region
and /endregion
obfuscate your code and
should be avoided like Paul McCartney’s solo music. Read more
about collapsible regions here
and here
/region Default Constructor
BegConstructor Access(*Public)
Required for Windows Form Designer support
InitializeComponent()
EndConstructor
/endregion
The database connection and diskfiles are declared in AVR ‘mainline’ code.
DclDB pgmDB DBName("*Public/Cypress")
DclDiskFile CustByNumber +
Type(*Update) +
Org(*Indexed) +
Prefix(Cust_) +
File("Examples/CMastNewL1") +
DB(pgmDB) +
ImpOpen(*No) +
AddRec(*Yes)
Declare anything you want global to the rest of this class here. But do try to keep global variables to a minimum. The road to programming hell is paved with global variables.
BegSr Form1_Load Access(*Private) Event(*this.Load)
DclSrParm sender *Object
DclSrParm e System.EventArgs
// Occurs when form is first loaded
// Put form "startup" code here (ie open files)
Override the runtime database. You might not need/want to do this–but do understand that you can
pgmDB.DBName = '*Public/Cypress'
Show connected DB name on form title bar
*This.Text = *This.Text + ' - ' + pgmDB.DBName
labelMessage.Text = String.Empty
Connect pgmDB
Open CustByNumber
EndSr
BegSr Form1_FormClosing Access(*Private) Event(*this.FormClosing)
DclSrParm sender Type(*Object)
DclSrParm e Type(System.Windows.Forms.FormClosingEventArgs)
// Occurs when form is closing
// Put form "housecleaning" code here (ie close files)
Close all files and disconnect the DB when this form closes.
Close *All
Disconnect pgmDB
EndSr
BegSr btnOK_Click Access(*Private) Event(*this.btnOK.Click)
DclSrParm sender Type(*Object)
DclSrParm e Type(System.EventArgs)
Copy fields from inputs to record format fields
PopulateRecordFromUI()
Update CustByNumber
labelMessage.Text = String.Format('Customer #{0} updated', maskedTextBoxCMCustNo.Text)
Reset UI
ResetUI()
EndSr
BegSr linklabelCancel_LinkClicked Access(*Private) Event(*this.linklabelCancel.LinkClicked)
DclSrParm sender Type(*Object)
DclSrParm e Type(System.Windows.Forms.LinkLabelLinkClickedEventArgs)
ResetUI()
EndSr
Find customer by clicking “find” link
BegSr linklabelFindCustomer_LinkClicked Access(*Private) Event(*this.linklabelFindCustomer.LinkClicked)
DclSrParm sender Type(*Object)
DclSrParm e Type(System.Windows.Forms.LinkLabelLinkClickedEventArgs)
Cust_CMCustNo = maskedTextBoxCMCustNo.Text
FindCustomer(Cust_CMCustNo)
EndSr
Find customer by pressing Enter key
BegSr maskedTextBoxCMCustNo_KeyDown Access(*Private) Event(*this.maskedTextBoxCMCustNo.KeyDown)
DclSrParm sender Type(*Object)
DclSrParm e Type(System.Windows.Forms.KeyEventArgs)
ClearErrorMessage()
Cust_CMCustNo = maskedTextBoxCMCustNo.Text
If e.KeyCode = Keys.Enter
e.Handled = *True
e.SuppressKeyPress = *True
FindCustomer(Cust_CMCustNo)
EndIf
EndSr
BegFunc FindCustomer Type(*Boolean)
Return True if record found Return False if record not found
DclSrParm CustomerNumber Like(Cust_CMCustNo)
DclFld Result Type(*Boolean)
Read customer record.
Chain CustByNumber Key(Cust_CMCustNo)
If CustByNumber.IsFound
If customer found
panelCustomerNumber.Enabled = *False
panelDataForm.Visible = *True
PopulateUIFromRecord()
LeaveSr *True
Else
If customer not found
labelMessage.Text = String.Format('Customer #{0} not found', maskedTextBoxCMCustNo.Text)
maskedTextBoxCMCustNo.Focus()
maskedTextBoxCMCustNo.SelectAll()
LeaveSr *False
EndIf
EndFunc
BegSr PopulateRecordFromUI
Cust_CMName = txtCMName.Text.Trim()
Cust_CMAddr1 = txtCMAddr1.Text.Trim()
Cust_CMCity = txtCMCity.Text.Trim()
Cust_CMState = txtCMState.Text.Trim()
Cust_CMPostCode = txtCMPostCode.Text.Trim()
CMFax is numeric so RemoveNonNumericCharacters removes any non-numeric characters before saving the value
Cust_CMFax = RemoveNonNumericCharacters(txtCMFax.Text)
Cust_CMPhone = txtCMPhone.Text.Trim()
EndSr
BegSr PopulateUIFromRecord
txtCMName.Text = Cust_CMName.Trim()
txtCMAddr1.Text = Cust_CMAddr1.Trim()
txtCMCity.Text = Cust_CMCity.Trim()
txtCMState.Text = Cust_CMState.Trim()
txtCMPostCode.Text = Cust_CMPostCode.Trim()
Input fields in .NET Windows forms apps are always text. They need to be converted to and from source and target values. Cust_CMFax is a numeric value. The .NET’s ToString() method used to format the numeric value to a desired string format. Read about .NET’s custom numeric format strings here
txtCMFax.Text = Cust_CMFax.ToString("(000) 000-0000")
txtCMPhone.Text = Cust_CMPhone.Trim()
EndSr
BegSr ResetUI
panelDataForm.Visible = *False
maskedTextBoxCMCustNo.Text = String.Empty
maskedTextBoxCMCustNo.Focus()
panelCustomerNumber.Enabled = *True
EndSr
BegSr ClearErrorMessage
labelMessage.Text = String.Empty
EndSr
BegFunc RemoveNonNumericCharacters Type(*String)
Use a regular expression to remove all non-numeric characters from an input string. Read more about regular expressions here
DclSrParm Value Type(*String)
[^0-9] is the regular expression that searches a string for all occurrences of non-numeric values
LeaveSr Regex.Replace(Value, "[^0-9]", String.Empty)
EndFunc
EndClass