Friday, August 7, 2009

Getting Started with VST: WinForms and Controls - Part 2

Well, getting back on track with our series, today I will show you how to add the toolbar and status bar button controls in addition to adding the code that will allow our WinForm to be displayed as an menu option under the Additional menu on the Customer Maintenance window.

1) In the Toolbox window, choose Button



2) Click on the toolbar area of the window to place the button control. Then change the properties as indicated in the picture below:



Note that the ButtonType on dexButtonProvider property is implemented as part of the Microsoft Dexterity Shell assembly (Microsoft.Dexterity.Shell.dll). Also, note that when I set the value to ToolbarWithSeparator the button immediately acquired the "look and field" of a typical Dexterity toolbar button.

3) Now, let's add the image to the Ok button. To do this, we can click on the image property and select the picture resource that best suit our needs:



In this case, I selected the Toolbar_Post image in the Resources file. Now, you can choose to add the actual checkmark image as part of the existing resources, but that is not a topic of this article.

4) On your own, add the window help button and the window note present and window note absent buttons to the status bar. For these controls, you will want to change the ButtonType on dexButtonProvider property to StatusArea and remove clear the text in the Text property. Don't forget to stack the the note buttons! The final result should look like the picture below:



5) Now that we have finalized the window layout, let's add some code to create the equivalent of a Dexterity form trigger. This will create an Additional menu on the Customer Maintenance window, allowing us to open the form. In VST, Dexterity form triggers are known as Menu Handler events and need to be registered in the Initialize() method in the GPAddIn.cs class.

GPAddIn.cs

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Dexterity.Bridge;
using Microsoft.Dexterity.Applications;
using Microsoft.Dexterity.Applications.DynamicsDictionary;

namespace GPWinForm
{
public class GPAddIn : IDexterityAddIn
{
// Add a variable reference to the RM Customer Maintenance form
static RmCustomerMaintenanceForm CustomerMaintenanceForm = Dynamics.Forms.RmCustomerMaintenance;

// Keep a reference to the rmCustomerHobbies WinForm
static rmCustomerHobbies rmCustomerHobbiesForm;

public void Initialize()
{
CustomerMaintenanceForm.AddMenuHandler(OpenRMCustomerHobbies, "Customer Hobbies", "H");
}

static void OpenRMCustomerHobbies(object Sender, EventArgs e)
{
// check to make sure the form is not either empty or already created
if (rmCustomerHobbiesForm == null)
rmCustomerHobbiesForm = new rmCustomerHobbies();
else
if (rmCustomerHobbiesForm.Created == false)
rmCustomerHobbiesForm = new rmCustomerHobbies();

rmCustomerHobbiesForm.Show();
rmCustomerHobbiesForm.Activate();

}
}
}

A couple things to note. In this script, we added static references to the Customer Maintenance form and rmCustomerHobbies WinForm to facilitate interacting with these throughout our code:


// Add a variable reference to the RM Customer Maintenance form
static RmCustomerMaintenanceForm CustomerMaintenanceForm = Dynamics.Forms.RmCustomerMaintenance;

// Keep a reference to the rmCustomerHobbies WinForm
static rmCustomerHobbies rmCustomerHobbiesForm;

We also added the menu handler event to assist in displaying a menu option to call our WinForm from the Customer Maintenance window. The OpenRMCustomerHobbies event handler method is the equivalent of a trigger processing procedure in Dexterity:


CustomerMaintenanceForm.AddMenuHandler(OpenRMCustomerHobbies, "Customer Hobbies", "H");

In Dexterity, the open form statement does a few things: it opens the form, activates the form, and open the main window of that form, if the AutoOpen property of the window is set to True . In VST, we can accomplish the same in two steps, by invoking the Show() and Activate() class methods of the form object.


rmCustomerHobbiesForm.Show();
rmCustomerHobbiesForm.Activate();

With the instructions provided in Part 1 of the WinForms and Controls series, we can proceed to build and deploy our assembly (I did not change the assembly name for this project). Once you load GP, open the Customer Maintenance window (Cards > Sales > Customer) to test the solution. You should now see the Additional menu option, along with our Customer Hobbies entry.


If you select the option, you should now see our form. Granted, we haven't added any code to manage whether a record was selected or not prior to our form being opened, we did not add any code to preset the Customer ID and Customer Name fields on our WinForm based on the same field values on the Customer Maintenance window, and certainly have not added any code to retrieve or store data based on the selected customer. All this will be a part of our next article.

Downloads

GPWinForm solution - C# - Click here

Related Articles

Getting Started with VST: WinForms and Controls - Part 1
Getting started with VST: "Hello World!" - The Video
Getting started with VST: "Hello World!" project
Getting started with Visual Studio Tools for Microsoft Dynamics GP -- Adventures of a Microsoft Dexterity developer

Until next post!

MG.-
Mariano Gomez, MVP
Maximum Global Business, LLC
http://www.maximumglobalbusiness.com/

5 comments:

James McGarvey said...

Really great tutorials!

I'm looking forward to the next installment.

Seeing this has prompted me to get VS2008 installed and the VST.

I'm looking forward to see how you work with the table needed to store the information.

Great work!
James

Ric said...

Hi Mariano! This is a great article. I was just wondering, would you be able to call a third party window that was created in dexterity from your winform was created using VST for GP? Thanks!

Mariano Gomez said...

Ric,

Thanks for the follow up question. It is possible to open a third party window written in Dex from your VST application. In order to do so you will need to DAG the third party dictionary and reference the resulting assembly in your VST project.

Take a look at my article http://dynamicsgpblogster.blogspot.com/2009/11/microsoft-dynamics-gp-technical_11.html. Download the conference material and review the documentation on how to create assemblies for third party dictionaries.

MG.-

ArBiNd said...

Thanx a lot for this article. Can U please demonstrate how to work with GP tables, i.e., fetching and insertion of data to and from the GP windows.
Waiting for ur valuable reply....

Arbind

Mariano Gomez said...

Arbind,

I have since demonstrated these techniques at the Microsoft Dynamics GP Technical Conference 2010 and Microsoft Dynamics Convergence Atlanta 2010. Please download the materials from any of these sessions on the Learning Resources page of this blog.

MG.-
Mariano Gomez, MVP