Finding a SmartPart in a WorkItem

Topics: CAB & Smart Client Software Factory
Mar 14, 2007 at 4:43 AM
Maybe we aren't supposed to do this... I add some SmartPart in my ModuleController class, then when different command or event are fired I show those views. In the past I would supply an ID of the view when adding to the WorkItem, but recently I migrated my app to a brand new SCSF Smart Client Application. I noticed there wasn't a ViewNames constant class created, this made me think I was doing things wrong.

Anyway, long story short, here is some code that I have to find a SmartPart - it's ugly, but considering WorkItem.SmartParts.FindByType<>() returns ICollection... I didn't have many options.

[EventSubscription(EventTopicNames.FirmwareModuleClicked)]
public void OnFirmwareModuleClicked(object sender, EventArgs e)
{
    //  Find the view and show it in the shell content workspace
    ICollection<FirmwareDeckView> views =
        WorkItem.SmartParts.FindByType<FirmwareDeckView>();
 
    foreach(FirmwareDeckView view in views)
    {
        WorkItem.Workspaces[WorkspaceNames.RightWorkspace].Show(view);
        break;
    }
}

Anyone else doing this? Is there a better way? Do you all supply and store IDs for your views so you can retrieve them later?

Thanks,
Steve
Mar 15, 2007 at 5:14 PM
Edited Mar 15, 2007 at 5:16 PM
I am doing something similar.

        private System.Windows.Forms.Control FindView(WorkItem workItem, string name)
        {
            foreach (System.Collections.Generic.KeyValuePair<string, Object> pair in workItem.SmartParts)
            {
                System.Windows.Forms.Control control = (System.Windows.Forms.Control)pair.Value;
                System.Diagnostics.Debug.WriteLine(control.GetType().FullName + " " + control.Name);
                if (control.Name == name)
                {
                    return control;
                }
            }
            foreach (System.Collections.Generic.KeyValuePair<string, WorkItem> pair in workItem.WorkItems)
            {
                System.Windows.Forms.Control control = FindView(pair.Value, name);
                if (control != null)
                {
                    return control;
                }
            }
            return null;
        }