abhimanu

I have created a custom form that hold a button that will help me to retrieve the contact details of the person.( My implementation of Address Book)

I have customed New Message form under IPM.Note and added a new button .

How to add a event handler for the button using c# .

I have saved the form with .oft extension.

Thanks in advance



Re: Visual Studio Tools for Office How to add event handler on Custom Form

X4U

Hello Dparora,

you can use the Inspector and CommandBars to get access to the Buttons.

I have a sample here:

http://www.x4u.de/Programming/Outlook/Codesnippets/tabid/61/Default.aspx

Also this is maybe of interrest to you.

http://www.x4u.de/Default.aspx tabid=226

Have a look at the X4U-Tools sample.

In OL2007 you can extend the Ribbon UI.

http://msdn.microsoft.com/msdnmag/issues/07/02/RibbonX/default.aspx loc=en

Hope this helps,

greets, Helmut






Re: Visual Studio Tools for Office How to add event handler on Custom Form

Dparora

Hi Helmut,

Thanks a lot for your reply.

the problem with my project is i am customizing the form and then saving it as .oft and not creating any controls at runtime.

So i want a method to get the event for the command button that I have added statically on the NewMail Form.

thanks in advance.

div





Re: Visual Studio Tools for Office How to add event handler on Custom Form

Sue Mosher - Outlook MVP

You're building a VSTO add-in, right (That's what this forum is for.) You need to follow Helmut's suggestion and use CommandBars or the ribbon, depending on the Outlook version. The button on your form will fire no events.





Re: Visual Studio Tools for Office How to add event handler on Custom Form

Dparora

Hi Sue,

Thanks a lot for your reply.

I am doing the following...

1. Have customized a NEw MAil form using the Action->Form -> Design Form and saved it as .oft. I have placed a button near to To comand button that once clicked will launch a window form.

2. I am loading this form in my startup event of Outlook using CreateItemFromTemplate

3. I want whenever a user click this custromized button it lauch my customize address book using windows form.

I am Using VSTO 2005 and working on Outlook 2003 in C#, so might not be able to use Ribbon.

Kindly help me out on this.

Is it like this that if we customize the outlook form , all the controls placed on it will not raise any event.

Thanks in advance





Re: Visual Studio Tools for Office How to add event handler on Custom Form

Sue Mosher - Outlook MVP

Dparora wrote:

Is it like this that if we customize the outlook form , all the controls placed on it will not raise any event.

Yes, thats what Helmut and I have been trying to tell you. You should be creating your button on Inspector.CommandBars, not on the form surface.





Re: Visual Studio Tools for Office How to add event handler on Custom Form

Dparora

Hi Sue,

Thanks a lot for your reply.

Will try to do through inspector.commandbars.

Thanks.

Div





Re: Visual Studio Tools for Office How to add event handler on Custom Form

X4U

Hi Sue & Div,

sorry - I was too late.

You can solve this problem by:

Wrapping the Inspector

Get the Forms20.Userpage object by getting _Inspector.ModifiedFormpages["NameofTheForm"]

then get access to the Forms20.CommandButton object by saying userPage.GetItemByName("nameofthebutton") as....

Then subscribe to the click event.

You can access every event of each control and every event of the formpage.

I can post a small sample tonight.

Hope this helps,

greets - Helmut.






Re: Visual Studio Tools for Office How to add event handler on Custom Form

Sue Mosher - Outlook MVP

Do you have that working reliably, Helmut I've seen reports of people having problems with that technique, so I'll be interested in learning from your sample.





Re: Visual Studio Tools for Office How to add event handler on Custom Form

X4U

Hi Sue,

I'm using this technique for Years now and havn't any problems.

It's the same like using CommandBarButtons or Items.

If you don't hold a reference - the control goes out of scope and you waon't receive any events.

I will send you a small sample solution offline.

Greets, Helmut




Re: Visual Studio Tools for Office How to add event handler on Custom Form

Dparora

Hi Helmut,

Thanks a lot ..

can you please share the code sample...

Regards

Div





Re: Visual Studio Tools for Office How to add event handler on Custom Form

Dparora

Hi Helmut,

Since I have saved the form as .oft will I be able to use the above method.

Also I am not able to see the class as Form20.

Do I need to include some namespace for this as I have already included Sytem.Windows.Form,.

Thanks

Div





Re: Visual Studio Tools for Office How to add event handler on Custom Form

X4U

Hello Sue and Dpaora,

here is a small Snippet that shows you the basic technic that I'm using to access Form-Controls.

You need an Inspector-Wrapper that checks for your MessageClass.

If you want to use Forms2.0 you have to add the Reference to the COM Library Forms20.

But be carefull - in the COM References Dialog are 2 Forms20 Libraries visible.

After adding the Reference to your project, check that the path points to the Global Assembly Cache folder.

Then you know you have the correct one.

If you have wrapped your Form, you can get the Controls.

Code Snippet

/// <summary>

/// This Method connects the FormPage Controls and registers for the EventHandlers

/// </summary>

private void ConnectFormPages()

{

// Reference to Forms20.dll

// Watch out -> use the DLL that pints to GAC Folder -> uses the PIA

// The modified Form Page is a Forms 2.0 UserForm

Forms20.UserForm modifiedFormPage;

// The Forms collection

Outlook.Pages pages;

try

{

// No Inspector -> Exit

if (_Inspector == null) return;

// get the Pages collection

pages = (Outlook.Pages)_Inspector.ModifiedFormPages;

// get the modified page page[index] or page["Pagename"]

modifiedFormPage = pages["Allgemein"] as Forms20.UserForm; //Common

// did we found our Form

if (modifiedFormPage != null)

{

// get the desired Controls...

// you can access Active-X Controls too

// The Standard-Controls are all Forms 2.0 Controls

_FullNameButton = modifiedFormPage.Controls._GetItemByName("FullNameButton") as Forms20.CommandButton;

// Register for the events

_FullNameButton.Click += new Microsoft.Vbe.Interop.Forms.CommandButtonEvents_ClickEventHandler(_FullNameButton_Click);

}

}

catch (System.Exception ex)

{

MessageBox.Show(new OutlookWin32Window (_Inspector.Application.ActiveWindow ()),ex.Message);

}

finally

{

// release objects from memory

modifiedFormPage = null;

pages = null;

}

}

/// <summary>

/// The Click eventhandler for the "Name" button.

/// </summary>

void _FullNameButton_Click()

{

CheckName();

}

/// <summary>

/// Attaches the current Data to the Form and

/// Shows the modified Check Name Form.

/// </summary>

private void CheckName()

{

using (new EnableThemingInScope(true))

{

NameCheck nameChecker = new NameCheck();

nameChecker.CreateControl();

nameChecker.Salutation = ((Outlook.ContactItem)_Item).Title string.Empty;

nameChecker.Title = ((Outlook.ContactItem)_Item).User1 string.Empty;

nameChecker.Academic = ((Outlook.ContactItem)_Item).User2 string.Empty;

nameChecker.Firstname = ((Outlook.ContactItem)_Item).FirstName string.Empty ;

nameChecker.Stand = ((Outlook.ContactItem)_Item).User3 string.Empty;

nameChecker.Lastname = ((Outlook.ContactItem)_Item).LastName string.Empty;

if (nameChecker.ShowDialog(new OutlookWin32Window(_Inspector.Application.ActiveWindow())) == DialogResult.OK)

{

((Outlook.ContactItem)_Item).Title = nameChecker.Salutation;

((Outlook.ContactItem)_Item).User1 = nameChecker.Title;

((Outlook.ContactItem)_Item).User2 = nameChecker.Academic;

((Outlook.ContactItem)_Item).FirstName = nameChecker.Firstname;

((Outlook.ContactItem)_Item).User3 = nameChecker.Stand ;

((Outlook.ContactItem)_Item).LastName = nameChecker.Lastname;

}

}

}

@div:

Hope this helps,

greets, Helmut






Re: Visual Studio Tools for Office How to add event handler on Custom Form

X4U

Hello,

in general I add a reference to the COM objects and include it in the sources like this:

Code Snippet

using Outlook = Microsoft.Office.Interop.Outlook;

using Office = Microsoft.Office.Core;

using Forms20 = Microsoft.Vbe.Interop.Forms;

Hope this helps,

greets, Helmut