TabWorkspace with images

Topics: CAB & Smart Client Software Factory
Sep 19, 2006 at 8:52 AM
originally posted by: alexsantos


Does anybody know how to show a smartPart in the TabWorkspace with image in the title ? Like normal tabControl .

Alexnaldo Santos
Sep 19, 2006 at 9:29 AM
originally posted by: JKraft4PIT

What i did was i added the image properties to the TabSmartPartInfo class for the ones i use.

Then inside the setTabProperties method of TabWorkspace I added code to set the image.

So all you have to do when showing a part is send your TabSmartPartInfo object like this.

Dim mySmartPart As myView
Dim mypartinfo As New TabSmartPartInfo
mypartinfo.ImagePropertyToSetImage = MyImage
Dim myTabSpace As TabWorkspace = Workspaces("MyTabSpace")
myTabSpace.Show(mySmartPart, mypartinfo)

The "ImagePropertyToSet" is the portion you want to show. Either the image list, or image index or both, or a actual image or what ever you want to do to set the image on the tab.
Dec 21, 2006 at 2:51 PM
originally posted by: mcstarVWT

I'm a little confused as to how you did this. First, there is no Image property on the TabSmartPartInfo class. Also, inside the SetTabProperties method (in the TabWorkspace.cs), we are dealing with a TabPage. This has an ImageIndex and an ImageKey, but this only works if the images are loaded into an attached ImageList. Can you post some code showing your mods to the TabWorkspace to clear this up?

Dec 22, 2006 at 8:30 AM
originally posted by: mcstarVWT

I went ahead and tackled the problem of showing icons/images in the TabWorkspace, so if anyone else needs it, here's my fix. The only thing I do not like about this fix is that it requires a few modifications to the CAB source code. (Note to CAB development team: This could have been prevented if all the TabWorkspace private methods had been made protected instead of private, or if some key methods had been marked abstract, as then we could have extended the TabWorkspace more easily. Of course we could create a completely new tabWorkspace object as well.)

So, here are the changes to the CAB objects...
First, edit the TabSmartPartInfo.cs to add a new Property to hold the image index....

private int imageIndex = -1;
/// <summary>
/// Get/Set the image index for a tab
/// </summary>
public int ImageIndex
get { return imageIndex; }
set { imageIndex = value; }

Now, edit TabWorkspace.cs. Add the following line just before the end of the try statement in the SetTabProperties method.

page.ImageIndex = smartPartInfo.ImageIndex;

That's all the modifications needed to the tab code. Now to use it, first you need to specify an ImageList for the TabWorkspace. I let my Modules do this in their ModuleInit so that the image list is only added for modules that will actually use it.

Here's an example of setting the ImageList in a ModuleInit.Load method....

// Create an image list for the upperTabWorkspace
TabWorkspace twUpper = (TabWorkspace)rootWorkItem.Workspaces"upperTabWorkspace";
ilUpperTab = new ImageList();
twUpper.ImageList = ilUpperTab;

Next, I usually let my workItems be responsible for adding the icons they need to the ImageList and keeping track of the indexes they might need.

Here's an example of a workitem method that loads an icon from the manifest resource and adds it to the imageList. (this method is called from the workitem's init)

internal void AddIcons(System.Windows.Forms.ImageList ilUpperTab)
Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream("MQModule.Resources.houseadd32.ico");
Image img = Image.FromStream(s);
this.addAddressIconIndex = ilUpperTab.Images.Count - 1;


Finally, in the workItem's show, set the TabSmartPartInfo to use the imageIndex....
public void Show(IWorkspace workspace)
TabSmartPartInfo inf = new TabSmartPartInfo();
inf.Title = "New Geo Address";

inf.ImageIndex = addAddressIconIndex;
//inf.ControlBox = true;
workspace.Show(fa, inf);

That's it. Now the icons work. Perhaps the CAB team could consider adding this type of functionality to the existing TabWorkspace??? Anyone agree?

One note, if you use the ImageList in more than one work item (for the same TabWorkspace) you will want to check to see if the ImageList is null before setting it, otherwise the sample code will overwrite the ImageList each time and you'll loose your icons.