Building the AccountViewer form

Now that you have your business objects set up, you will begin to build the AccountViewer form. The AccountViewer form is the main form in the sales force application. It presents the full details of an account in the sales force application to the user. This form is a rather large form, so you will need to split its interface into six different tabs using the .NET Compact Framework Tab control.

Design a form similar to the next screenshot. Create the General and Address tabs first.

Building the AccountViewer form

Define the following list of items in the Status, Reception, and Source Combobox controls.

Combobox

List of items

Status

Contacted

Qualified

Unqualified

Reception

Cold

Warm

Hot

Source

Referrals

Advertisement

In the menu bar of this form, you will need to add the Save and Cancel menu items to save and discard changes made to the account record. In the Save item click event, call the BaseAccount.Validate() function to validate the data keyed in. If the validation fails, display the error message to the user.

The Cancel button click event simply discards changes made to the account details.

public void btnSave_Click(System.Object sender, System.EventArgs e)
{
AccountBindingSource.EndEdit();
if (_lead.Validate()==false)
{
String _validationMessage = _lead.GetValidationResult();
MessageBox.Show(_validationMessage,"Validation error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation
,MessageBoxDefaultButton.Button1);
}
}
public void btnCancel_Click(System.Object sender, System.EventArgs e)
{
AccountBindingSource.CancelEdit();
this.DialogResult =
System.Windows.Forms.DialogResult.Cancel;
this.Close();
}

Data binding .NET controls to your business objects

The .NET Compact Framework provides the BindingSource control, which allows you to set up data binding on the form. Drag a BindingSource control to the AccountViewer form. You can set up data binding using the following code. The first two lines of code pass the business object to the BindingSource control. The subsequent lines of code define the mapping between your business objects and your controls.

public void SetupBindings()
{
if (_account != null)
{
AccountBindingSource.DataSource = typeof(BaseAccount);
AccountBindingSource.Add(_account);
txtFirstName.DataBindings.Add(new Binding("Text",
AccountBindingSource, "FirstName", true));
txtLastName.DataBindings.Add(new Binding("Text",
AccountBindingSource, "LastName", true));
//Data binding a combobox
txtLastName.DataBindings.Add(new
Binding("SelectedIndex",
AccountBindingSource, "Status", true));
.
.
.
}
}

In the same fashion, establish the data bindings for the other controls on the AccountViewer form.

Launching the AccountViewer form

At this point, you have an AccountViewer form that is bound to the BaseAccount class. In order to use the form for data entry, you will need to feed it a live BaseAccount object.

As you found out earlier, the BaseAccount class also acts as a wrapper for the Dataset object. You can't simply instantiate a new BaseAccount object without having the Dataset in the first place.

The global NewLead() method solves this problem for us. It grabs an empty dataset (containing the data schema) from the data tier. You can use the GetAccountDetails() function you've created in Chapter 2 to do this. It creates a new row in that dataset and encapsulates it using the LeadAccount object.

public LeadAccount NewLead()
{
DataSet _newAccountDataset;
LeadAccount _newAccount;
DataRow _newRow;
_newAccountDataset =
GlobalArea.PluginManager.GetActivePlugin.GetAccountDetails(null);
_newRow = newAccountDataset.Tables["Accounts"].NewRow();
_newAccount = new LeadAccount(_newRow);
_newAccount.AccountGUID = Guid.NewGuid();
return _newAccount;
}

The global SaveNewAccount() method does the opposite. It takes a Baseaccount object containing the user's changes and commits it to the database via the SetAccountDetails() function you created in Chapter 2.

public bool SaveNewAccount(BaseAccount AccountObject)
{
DataSet _accountSet;
AccountObject.MappedDataset.Tables["Accounts"].Rows.Add
(AccountObject.MappedDatarow);
_accountSet = AccountObject.MappedDataset;
return
GlobalArea.PluginManager.GetActivePlugin
.SetAccountDetails
(AccountObject.AccountGUID, _accountSet);
}

You need to launch the AccountViewer form, create a new lead object, pass it to the form, and upon the user clicking the Save button, commit the changes in this object back to the database. You can do all this in the ShowDialog() function of your NavigationService class:

public static DialogResult ShowDialog(string DialogName, object Arg1)
{
DialogResult _dialogResult = DialogResult.Cancel;
switch (DialogName)
{
.
.
.
Case "New Account":
AccountViewer _lead = new AccountViewer();
_lead.Account =
GlobalArea.Application.NewLead();
_lead.SetupBindings();
_lead.TitleBar = "New lead";
_dialogResult = _lead.ShowDialog();
if (_dialogResult == DialogResult.OK)
{
GlobalArea.Application.SaveNewAccount
(_lead.Account);
}
_lead.Close();
_lead.Dispose();
_lead = null;
break;
}
return _dialogResult;
}

Testing the AccountViewer form

You can test the AccountViewer form at this point, assuming you have set up and registered your data source appropriately. Launch the sales force application. When you double-click on the New Account icon, you will see the AccountViewer form appear. Test validation by skipping the required fields (such as First Name) and attempting a save. You should see the appropriate validation error message appear.

If you key in all the required fields and attempt a save, you will notice that the form will automatically close. If you query your database using the Query Analyzer or Msql tool, you will notice a new record created in the Accounts table.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.238.20