473,405 Members | 2,185 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

C# Add-in for Outlook to create array of CommandBarButton(s)

4
hi all,
I am writing a add-in for Outlook 2007 using C# and VSTO ( Visual Studio 2008). When the user clicks on each mailitem in the inbox (or any folder) in the active explorer window, the add-in needs to parse the mail and look for "pdf2:xxxx" and create and add as many commandbarbuttons to a CommandBar.

The problem is when the I click on the same mail again the eventhandler for the CommandBarButton gets added again. As a result when the CommandBarButton is clicked the event is called multiple times.

I test the code by sending a mail with many repetitions of "pdf2:". Is my approach wrong ? how to fix this bug ?

Thanks
arnott

Here is the code :

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Xml.Linq;

using Outlook = Microsoft.Office.Interop.Outlook;

using Office = Microsoft.Office.Core;

using System.Windows.Forms;


namespace OutlookAddIn14

{

public partial class ThisAddIn

{

Outlook.Explorer explorer = null;

Office.CommandBar docBar = null;

Office.CommandBarPopup newMenuBar;

private string menuTag = "plugintest14";



//Office.CommandBarButton[] btns;



System.Collections.ArrayList selectedItems = new

System.Collections.ArrayList();

System.Collections.ArrayList pdf2Items = new System.Collections.ArrayList();


private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

explorer = this.Application.Explorers.Application.ActiveExplo rer();



explorer.SelectionChange += new Outlook.

ExplorerEvents_10_SelectionChangeEventHandler

(explorer_SelectionChange);


docBar = explorer.CommandBars.Add(

"Pdf2 Docs", missing, missing, true);



}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)

{

//RemoveMenubar();

}



void explorer_SelectionChange()

{

selectedItems.Clear();

pdf2Items.Clear();



foreach (object selectedItem in explorer.Selection)

{

Outlook.MailItem mailItem = selectedItem as Outlook.MailItem;

Office.CommandBarButton[] btns;



if (mailItem != null)

{

String[] mainStr;

string[] docStr;

char[] delim = { ' ', '\n', ',' };

char[] dlm = { ':', '/'};



mainStr = mailItem.Body.Split(delim, System.StringSplitOptions.RemoveEmptyEntries);

foreach (string mt in mainStr)

{

if (mt.StartsWith("pdf2:"))

{

docStr = mt.Split(dlm, System.StringSplitOptions.RemoveEmptyEntries);

pdf2Items.Add(docStr[docStr.Length-1]);

}



}

//MessageBox.Show(String.Format("mlink Count : {0} ", pdf2Items.Count));

if (pdf2Items.Count == 0)

{

docBar.Visible = false;

}

if (docBar != null && pdf2Items.Count != 0)

{

try

{

docBar.Visible = false;

docBar.Delete();

docBar = explorer.CommandBars.Add(

"Pdf2 Docs", missing, missing, true);

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

int btnCnt = 0;

btns = new Office.CommandBarButton[pdf2Items.Count];

//btns.Initialize();


foreach (string mdoc in pdf2Items)

{

Office.CommandBarButton tBtn;

tBtn = (Office.CommandBarButton)docBar.FindControl(Office .MsoControlType.msoControlButton, missing, mdoc + " - " + btnCnt, missing, false);

if (tBtn == null)

{

btns[btnCnt] = (Office.CommandBarButton)docBar.Controls.Add(

Office.MsoControlType.msoControlButton, missing,

missing, missing, true);

// btns[btnCnt].Click -= new Office._CommandBarButtonEvents_ClickEventHandler(B utton_Click);

btns[btnCnt].Click += new Office._CommandBarButtonEvents_ClickEventHandler(B utton_Click);

btns[btnCnt].Caption = mdoc;

btns[btnCnt].Tag = mdoc + " - " + btnCnt;

btns[btnCnt].Style = Office.MsoButtonStyle.msoButtonCaption;

btns[btnCnt].FaceId = btnCnt;

//btns[btnCnt].DescriptionText = "Document : " + mdoc;

//btns[btnCnt].TooltipText = "Document : " + mdoc;



}

btnCnt++;

}

docBar.Position = Microsoft.Office.Core.MsoBarPosition.msoBarFloatin g;

//docBar.Left = 1000;

docBar.Left = this.Application.ActiveExplorer().Left + this.Application.ActiveExplorer().Width - 200;

//docBar.Protection = Microsoft.Office.Core.MsoBarProtection.msoBarNoMov e;

docBar.Top = this.Application.ActiveExplorer().Top + 200;

docBar.Width = 5 * btns[0].Width;

docBar.Visible = true;

}

else

{

btns = null;

}

}

}



}






void Button_Click(Office.CommandBarButton ctrl, ref bool cancelDefault)

{



MessageBox.Show("clicked : " + ctrl.Caption);

count++;

}
Oct 23 '08 #1
1 4670
arnott
4
bump !

Is there a way to check if the eventhandler is already added to the button.click object and not add it ?

Thanks
Oct 23 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Stefan Mueller | last post by:
With the following code I can add a new row to an existing table. That really works great. Many thanks to all who helped me so far. But my problem is that the added cells do somehow not have the...
5
by: Mike L | last post by:
I have two questions. 1) What is the code to check if datagrid already has a datasource? When the user clicks "ADD" button I want to add a record to the data grid, if the user clicks "ADD"...
4
by: Maarten | last post by:
i have the folowing code to setup my datagrid collumns Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load gridsetup() end sub
8
by: Yuk Tang | last post by:
I am tearing my hair out over this, since I can't see what I'm doing wrong (duh, if I knew, I wouldn't be asking the question). I am adding Field items to a Field Collection, but for some reason...
0
by: herman404 | last post by:
Hi everyone, I'm trying to dynamically add 3 button columns to a DataGrid object that I have on an ASP.net webpage, but I don't see any documentation on how to do it. I've seen plenty of examples...
3
by: Greg Scharlemann | last post by:
I'm not sure the best way to accomplish this... my hunch is with javascript, but I'm not sure if using server side code (PHP) would be easier. I'm adding people to a database. People have a...
1
by: Sri Nanduri | last post by:
Hi , I am new to JS. Please help me to resolve my issue. 1.When the form loads there are two DropDown boxes and a ADD button. 2.When I click the add button it will add two DropDown boxes and a add...
0
by: anureddy | last post by:
help me how to add datagridview columns to another table,using windowsapplications. and set the displaymember and value member datagridviewcomboboxcolumn. i used this below code but that not...
0
acoder
by: acoder | last post by:
Problem The select object's add method doesn't append options to the end of the list. Browser Internet Explorer Example The Javascript code for appending an option element at the end of a...
2
by: Question123 | last post by:
<root> <Element> <add key="1" value="1"/> <add key="2" value="2"/> <add key="3" value="3"/> <add key="4" value="4"/> </Element> <Items>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.