Toolbar's, ImageLists, and Manifest Resources

A

anthony.duerr

I have encountered a problem (most certainly a .NET bug), that, for the
life of me, I cannot figure out how to work around.

Using Visual Studio 2003, with enabled XP Visual Styles.

There are two possible scenarios here, the first one works properly,
while the 2nd one doesn't.

Scenario 1
----------
* Application opens with a notifyicon instead of a form window.

* User double clicks the notifyicon to show the main form window

* The main form window properly displays the toolbar buttons with
their respective images from the assigned ImageList. The ImageList is
populated from calls to
Assembly.GetExecutingAssembly().GetManifestResourceStream("ns.file.ext")
-----------

Scenario 2
----------
Everything is the same as in Scenario 1, except for the following:

* Application opens with a notifyicon, same as above.

* A Timer event triggers the opening of another form window, and the
user has *not* double clicked the notifyicon to open the "main" window.


* When double clicking the notifyicon and opening the main form window,
the buttons on the toolbar do not display their images. They are
blank. This is the problem.
-----------

Also, If there is a timer tick that triggers the secondary window AFTER
the user has already opened and closed the main window, and the user
procedes to open the main window again, everything still displays as it
should.

Other than forcing the main window to open (which I do not want to do),
I absolutely cannot find a workaround for this problem. I have been
trying absolutely everything that I can think of for hours to no avail.
Any insight would be appreciated.
 
M

Mick Doherty

How are you starting the App with a NotifyIcon?
When are you loading the ImageList?
Are visual styles actually Enabled?

I just built a quick test project starting my app with an
ApplicationContext.
I fill the ImageList in the Forms load method.
I showed the form by DoubleClicking on the NotifyIcon and all was well.

I restarted the App and allowed a Timer to show the Form.
Visual Styles were not Enabled, but the Images showed up on the Toolbar
Buttons.

I then removed the EnableVisualStyles and DoEvents calls and added a
Manifest Resource to the project.
Visual Styles are now enabled and the Icons display just fine in the toolbar
whether the form is loaded by doubleClick or timer.
 
A

anthony.duerr

This is my main() method:

static void Main()
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.Application.DoEvents();
Loader load = new Loader();
System.Windows.Forms.Application.Run();
}

Loader() then loads the notifyicon in its constructor and sets up its
click events and menu. The main form is also instantiated in the
Loader() constructor.

The ticker is a variable in the Loader() class, and is initialized on
declaration in the class header.

The main form loads the toolbar buttons from the manifest resource
stream in its OnLoad event. I have also tried arranging this to load
up the toolbar button images in the forms instantiation, but that does
not work, either.

Note that I have created a PictureBox and used the manifest resource
stream to grab one of the icons and display it in the picture box
successfully - in the same OnLoad() method, while it didn't display in
the toolbar button.

If I'm being unclear in anything, the full source code and application
can be obtained on source forge - http://sf.net/projects/ontime
 
M

Mick Doherty

Looks like Application.EnableVisualStyles is doing nothing when your app
loads a form via the timer, as can be seen by looking at the radio buttons
and checkboxes on your config form.

Remove the calls to EnableVisualStyles and DoEvents from your main() method.
Rebuild your app.
Open OnTime.Exe with Visual Studio.
Import a *.manifest file into the resource table (Resource Type =
RT_MANIFEST)
Change the Id from 101 to 1.

The downside to this method is that you must add the manifest resource every
time you rebuild the app, but visual styles will work and your Icons will
show in the toolbar.
 
A

anthony.duerr

Mick,
That only happens when the timer event is the first thing to show a
form window. In my personal code, I overcame that by initializing
frmMain() in Loader's constructor, instead of when it was first being
shown. The EnableVisualStyles() also seems to be affected by this
whole weird form issue in this regard. I was able to work around that,
but I still haven't been able to work around the button images....
 
M

Mick Doherty

Adding a Manifest Resource only takes seconds and will fix it.

If you don't want to do that, then you'll have to make your ImageList a
global variable, instead of local to the form, and then assign the imagelist
to the toolbar in the load event.
In your Main() method you need to Initialize a new FrmMain, Initialize and
fill the imagelist, then show and hide/dispose the FrmMain.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top