Solution module and forms manager

Add-in Express™ Support Service
That's what is more important than anything else

Solution module and forms manager
Keep selected form when change solution folder in my solution module 
Kotrak


Guest


Hello,

I'm implementing small test Outlook Add-In project with forms manager and solution module. I have two ADXOLForm instances as forms collection items in my forms manager (WebViewPanes, Cached one instance for all folders). In my AddinInitialize method I add all paths related with solution folders to each form collection items in my forms manager (property FolderNames).

On my ribbon I have to buttons whick activates one of the two forms, the code below:



private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)
{
    ActivateForm<ADXOlForm1>(adxOlFormsCollectionItem1);
    this.CurrentView = CurrentForm.Form1;
}

private void adxRibbonButton2_OnClick(object sender, IRibbonControl control, bool pressed)
{
    ActivateForm<ADXOlForm2>(adxOlFormsCollectionItem2);
    this.CurrentView = CurrentForm.Form2;
}

private T ActivateForm<T>(ADXOlFormsCollectionItem item)
    where T : ADXOlForm
{
    if (item != null)
    {
        var form = item.GetCurrentForm() as T;
        if (form != null)
        {
            form.Activate();
            return form;
        }
    }

    return null;
}



The CurrentView is my flag which keeps information about last view that user choose by clicking button.
For default I have selected root solution folder and when I change the active form via ribbon button everything works. The problem is when I change the selected solution folder - it always shows first item from forms manager. I want to show last view that I keep in my CurrentView object.

I tried using BeforeFolderSwitch event:



private void FormsManager_ADXBeforeFolderSwitch(object explorerObj, ADXOlFormsCollectionItem SrcItem, object SrcFolder, ADXOlFormsCollectionItem DstItem, object DstFolder)
{
    if (SrcItem != null && DstItem != null)
    {
        switch (this.CurrentView)
        {
            case CurrentForm.Form1:
                DstItem = adxOlFormsCollectionItem1;
                break;

            case CurrentForm.Form2:
                DstItem = adxOlFormsCollectionItem2;
                break;
        }
    }
}



But it does not work. Could you give me some advice how to solve my problem?

Best reagrds,
Kotrak
Posted 13 Feb, 2015 11:54:11 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Kotrak,

Consider a couple of flags that you use to decide whether the first or second form should be shown. The form to be shown will be the top form in the list of WebViewPane forms for this folder; the other form is prevented from being shown immediately. You control this by setting ADXOlForm.Visible in the ADXOlForm.ADXBeforeFormShow event; true-show; false - don't show. In this event handler, as soon as you decide that the form will not be shown, you start a System.Windows.Forms.Timer or call ADXOlForm.ADXPostMessage. In the Timer.Tick or ADXOlForm.ADXPostMessageReceived event, you show the other form using ADXOlForm.Show. The flags mentioned above must allow the form to get loaded this time; i.e. this time you need to set ADXOlForm.Visible=true in the ADXOlForm.ADXBeforeFormShow event.

This loads one of the forms first and allows the user to switch to the other form.


Andrei Smolin
Add-in Express Team Leader
Posted 16 Feb, 2015 08:18:24 Top
Kotrak


Guest


Hello Andrei, thank you for your reply.
When I was reading your post for the first time, your solution appeared to be complictaed, but after short research I decided to switch between forms by changing FormClassName property on forms collection item programmatically (now I have only one form collection item for all ADXOlForms). Everything works as expected, thank you.

I was wondering if I have to dispose current ADXOlForm before I change the FormClassName property or GC do this work for me. I'm not sure how your mechanisms work and I'm afraid about some meory leaks, for example creating a lot of instances of one form that I can't see via FormInstanceCount property (I still use "one instance for all folders" caching strategy).

Current Method for switching forms:

public void ActivateOutlookView<T>(ADXOlFormsCollectionItem item)
    where T : ADXOlForm
{
    Outlook.Explorer explorer = null;
    try
    {
        if (item != null)
        {
            item.FormClassName = FormPath.GetViewNamespace<T>(); 
            // FormPath is the class with extensions written by me
            explorer = AddinModule.CurrentInstance.OutlookApp.ActiveExplorer();
            item.ApplyTo(explorer);
        }
    }
    finally
    {
        ComExtension.ReleaseComObjects(explorer);
    }
}


Best regards,
Kotrak
Posted 19 Feb, 2015 05:18:57 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Kotrak,

Changing the ADXOlFormsCollectionItem.FormClassName property causes the ADXOlFormsCollectionItem to destroy all instances. Since you have Cached = one instance for all folders, the above means that the form instance will not be preserved; instead, it will be recreated anew.


Andrei Smolin
Add-in Express Team Leader
Posted 20 Feb, 2015 05:41:35 Top