none
Office.CommandBarButton click event not firing when clicked RRS feed

  • Question

  •         public void addDeptContact(TWUserGroupInfo thisDept,int idx)
            {
                int ContactLen = thisDept.ModelUsers.Length;
    
                Office.CommandBarPopup thispop;
                thispop = (Microsoft.Office.Core.CommandBarPopup)pop_contacts.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup, missing, missing, missing, missing);
                thispop.Caption = thisDept.Deparment + "(" + ContactLen + ")";
                thispop.BeginGroup = false;
                thispop.Enabled = true;
                //thispop.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
                thispop.Width = 150;            
    
                
                Office.CommandBarPopup[] thisDeptContacts = new CommandBarPopup[ContactLen];
    
                for (int i = 0; i < ContactLen; i++)
                {
                    thisDeptContacts[i] = (Microsoft.Office.Core.CommandBarPopup)thispop.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlPopup, missing, missing, missing, missing);
    
                    string cap = "&" + thisDept.ModelUsers[i].UserNameFirstSpell + " " + thisDept.ModelUsers[i].UserName;
                    if (thisDept.ModelUsers[i].Tel != "") cap += " :" + thisDept.ModelUsers[i].Tel;
                    if (thisDept.ModelUsers[i].Mobile != "") cap += " :" + thisDept.ModelUsers[i].Mobile;
                    
                    thisDeptContacts[i].Caption = cap;
    
                    thisDeptContacts[i].BeginGroup = false;
                    thisDeptContacts[i].Enabled = true;
                    //thispop.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
                    thisDeptContacts[i].Width = 150;
    
                    addContactDetail(thisDeptContacts[i], thisDept.ModelUsers[i]);
                }
    
                Office.CommandBarButton btnSyncDept;
    
                btnSyncDept = (Microsoft.Office.Core.CommandBarButton)thispop.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, missing, missing, missing, true);
                btnSyncDept.Caption = "sync";
                btnSyncDept.DescriptionText = ""+idx;
                btnSyncDept.BeginGroup = true;
                btnSyncDept.Width = 150;
                btnSyncDept.Style = Office.MsoButtonStyle.msoButtonIconAndCaption;
                btnSyncDept.Picture = PublicToolkits.icon_sync;
                btnSyncDept.Click += new _CommandBarButtonEvents_ClickEventHandler(syncButton_Click);
    
            }
    
    
            public void addContactDetail(Office.CommandBarPopup thisContactPop, T_User thisUser)
            {         
                addContactItem("Name", thisUser.UserName, thisContactPop);
                addContactItem("Ename", thisUser.EnglishName, thisContactPop);
                addContactItem("sex", thisUser.Sex, thisContactPop);
                addContactItem("email", thisUser.Email, thisContactPop);
                addContactItem("mobile", thisUser.Mobile, thisContactPop);
                addContactItem("ext", thisUser.Tel, thisContactPop);
                addContactItem("MSN", thisUser.MSN, thisContactPop);            
            }
            public void addContactItem(string fieldName, string fieldValue, Office.CommandBarPopup thisPop)
            {
                if (fieldValue != null && fieldValue != "")
                {
                    Office.CommandBarButton contactItem;
                    contactItem = (Microsoft.Office.Core.CommandBarButton)thisPop.Controls.Add(Microsoft.Office.Core.MsoControlType.msoControlButton, missing, missing, missing, true);
                    contactItem.Caption = fieldName + ": " + fieldValue; // +"-" + maxevent;
                    contactItem.Width = 150;
    		contactItem.Click += new _CommandBarButtonEvents_ClickEventHandler(syncButton_Click);
    	     }
    
            }
    
    
            private void syncButton_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref bool CancelDefault)
            {
    
                    MessageBox.Show("Caption:"+Ctrl.Caption);
                          
            }

    I had build a add-in for outlook2007.Now i want put company all contact on the popmenu at the commandbar, but i met the menu event losting/unstable issue.some time the click was not go the "_CommandBarButtonEvents_ClickEventHandler"  "syncButton_Click" event function. the contact was up to 200 people. had up to 1000 menu was use the same event function. please help to check whether i use the wrong way to call "_CommandBarButtonEvents_ClickEventHandler". thanks


    • Edited by kony_han Saturday, May 26, 2012 1:30 AM remark remove
    Saturday, May 26, 2012 1:30 AM

Answers

  • Hi kony_han

    You need to declare the CommandBar objects at the class level, not in the individual methods. If you put them in the methods, then the objects go out-of-scope when the method ends. Eventually, .NET  will garbage collect them (when is indeterminate). Once they've been garbage collected, the click events will no longer work.


    Cindy Meister, VSTO/Word MVP

    Saturday, May 26, 2012 6:35 AM
    Moderator

All replies

  • Hi kony_han

    You need to declare the CommandBar objects at the class level, not in the individual methods. If you put them in the methods, then the objects go out-of-scope when the method ends. Eventually, .NET  will garbage collect them (when is indeterminate). Once they've been garbage collected, the click events will no longer work.


    Cindy Meister, VSTO/Word MVP

    Saturday, May 26, 2012 6:35 AM
    Moderator
  • Hi Cindy

    Thanks for your helps. Addin-in working now ... 

    Thursday, May 31, 2012 8:09 AM