Button Images Not Showing When Using XP Style Forms

G

Guest

Good Afternoon:
I was hoping if someone could share some light on a problem that I'm facing.
When I create a Visual Basic.Net program and I use the XP style for my Window
Forms and buttons; if I add images to my menu buttons they don't show when
the form is created. But, if I don't use the XP style the images show on my
buttons correctly. Can someone please explain to me what the problem is and
how to get around it? Here is how I'm calling my forms:
Public Sub Main()

Dim frmSplash As New fclsSplash
Dim mainForm As New fclsMain

System.Windows.Forms.Application.EnableVisualStyles()
System.Windows.Forms.Application.Run(frmSplash)

Select Case vfClass.fdisplay
Case 0
System.Windows.Forms.Application.Run(SqlLogin)
Case 1
System.Windows.Forms.Application.Run(mainForm)
End Select

It's my mainForm that holds the menu buttons with the images. Do anyone
know what's going on? Thanks.
 
M

Mattias Sjögren

It's my mainForm that holds the menu buttons with the images. Do anyone
know what's going on?

Just like the documentation says, when you set the button's FlatStyle
property to System the Image isn't displayed.




Mattias
 
M

Mick Doherty

What type of button is a MenuButton?

If it's a toolbar button then you need to add Application.DoEvents after
Application.EnableVisualStyles, or supply a manifest file instead of calling
Application.EnableVisualStyles.

If it's a standard Button, then System Buttons do not show an Image. I
modified the Button class to get around this and you will find the source
for this button on my site. Note that when set to Flatsyle.System the button
still wont show an Image, but Visual Styles will be applied to
Flatstyle.Standard buttons.
http://dotnetrix.co.uk/buttons.html
 
G

Guest

Mick,

I have the same problem in VB.net. I can see the image in the design window
on the toolbar button. But, when I build and run the application the image
doesn't show up. I don't know where the Application.EnableVisualStyles
statement is.

Can you help?


Thanks.

Dave Green
(e-mail address removed)
 
D

Dave

I'm sorry I no longer have the reference to an article on MSDN a few weeks ago that states there is a bug using image buttons in
VS.NET due to the serialization order of code.

The fix, if I remember correctly, was to go into the hidden area of code that the designer serializes and move any code that adjusts
the buttons bounds or location to appear before the image property is set.

The problem with this "quick fix" is that designer changes may revert back to the error-producing code. At this time I don't
believe there is a fix for the latest version of NET. Maybe 2005 will have fixed that issue.
 
M

Mick Doherty

Assuming your startup form is called Form1 add the following code to the
form.

Shared Sub main()
Application.EnableVisualStyles
Application.DoEvents
Application.Run(New Form1)
End Sub
 
G

Guest

Thanks Mick. Unfortunately, this form is not the startup form. I'm not sure
if that is why it doesn't work. Should that make a difference?


Dave
 
M

Mick Doherty

Actually, I'm not entirely sure what the cause of your problem is, I
answered a bit too quickly earlier.
Since you didn't know where EnableVisualStyles was called I should assume
that this is not a WinXP issue.

I've seen several variations of this problem and the most difficult to sort
out, was in an app that started without a GUI, but displayed a dialog box
with a toolbar at specified events. I've never seen this issue on a non
Visual Styles Enabled app, but that does not mean it doesn't happen.

Can you recreate the problem in a small project, or give the steps required
to reproduce it?
 
D

Dave

Did you attempt to try the fix I posted previously on this thread?

Locate the designer-serialized code in your code-behind. It will probably be in a #region block named, "Windows Form Designer
generated code".

In the region, find the method named, "InitializeComponent".

In the method there will be a declartion for your control. Find the area of the method that sets the properties on your control
(The appropriate section should be marked with a comment).

Cut and Paste the line of code that sets the image of the button as the last line of the section.

See if that does it.

Like I said, the article states that this is a bug. No sense driving yourself crazy over this if you can identify the problem as a
bug early on.
 
M

Mick Doherty

If that is the problem, then the code could be cut from the
InitializeComponent() method and pasted to Sub New() after the
InitializeComponent() call. That way the designer is not going to mess with
it every time you rebuild the project.
 

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