Can't get images on toolbar buttons when running

Hi everyone, 

I have a VB.net application that has a toolbar with several buttons. 

The first two buttons are the standard "save" and "print", with those typical images. 

My problem is they show up and work fine in the form designer, but when I run the application the images assigned to the buttons do not appear. 

Can anyone tell me why this would be  

Thanks.


Answer this question

Can't get images on toolbar buttons when running

  • NX74205sc

    Hey everyone,

    I figured out the issue in case you were wondering and couldn't sleep at night. :-)

    The issue is apparently, somehow Microsoft's Visual Studio corrupted the code. That is, code typically generated by the form-generator was scattered all over the place. I suspect this happened because I defined subs and functions ABOVE the form design code "region". 

    When I created a new form, copied & pasted all the elements from the old form onto the new, and then copied the code piece by piece, everything is now working fine.

    Not even sure if "code corruption" is the issue, but that's my best guess given what I was able to do to solve the issue.

    Thanks everyone!

  • KevinOB

    You might need to execute the index changing code AFTER the form has loaded.

    As a test, add a timer to your form with an interval of 500 and enabled set to false.  Move the code you used from from1_load to Timer1_Elapsed and add a final line of code to set Timer1.Enabled = False.  The last line of Form1_Load should then be Timer1.Enabled = True.

    See if changing the index after the form has been displayed fixes the issue.

  • Archimedez

    Sounds like the XP Visual Styles bug.

    Add Application.DoEvents() after Application.EnableVisualStyles in sub main().

    If you're images have an Alpha Channel, then either load the Imagelist during or after Form Load, or supply a manifest file/resource so that they draw the alpha correctly.

  • GatorBait58

    Thanks for your suggestion and code. 

    All the properties come up correctly. 
    Under "Buttons", it has "Collection" listed.

    When I click on "Collection", the 5 buttons (0 - 4) are listed. The first two, the "save" and "print", have the images displayed in the properties window. The next button is a divider. The last two buttons were "blank" pending icons for them. 

    Under the ToolBarButton properties, all are "enabled" and "visible" for each is set to "true". 

    Still the images don't show.

    But get this... while changing the images in the ImageList (that is, changing the image in the "ImageIndex"), and then putting them back the way they were originally, the images magically appear on my toolbar buttons. 

    When I do this same technique in Visual Studio designer, save, and run the program, the images still DO NOT appear. But when the properties window comes up in the code you provided and I change the images again, they appear. 

    Any ideas on what's going on

    Thanks!



  • Linn White

    If it's not XP then I'm not sure that this fix applies.

    ...but to answer your query on sub main, assuming your startup form is called MainForm:

    Shared Sub Main()
        Application.DoEvents()
        Application.Run(New MainForm)
    End Sub

  • Donald D. Drake

    Firemyst,

    I think Mick just assumed that you were using a Sub Main() to start your app.  Since you're not, you can execute the required code in Sub New() instead.

    Expand the Region " Windows Form Designer generated code " and modify it as below:


        Public Sub New()
            MyBase.New()

            'This call is required by the Windows Form Designer.
            InitializeComponent()

            'Add any initialization after the InitializeComponent() call
            
            Application.EnableVisualStyles()
            
            'Here is where you add DoEvents()
            Application.DoEvents()

        End Sub



    I should add that the examples I've seen on using visual styles all mention using the Sub Main() method of starting your app.  But I've tried the code above and it does work; I don't know if there are any pitfalls to using it though.

  • FredP

    This might be a really stupid question... but where is "main" kept  Keep in mind that's I'm a newbie. 

    The application that's causing us troubles has no "main" method anywhere that we can find. It's one class file, which contains one form and all the form's elements, so I assume "main" is in System.Windows.Forms.Form. 

    When I put the following in our class file:
    <Code>
    Shared Sub Main()
            Application.DoEvents()
        End Sub
    </CODE>

    The application doesn't run the form. So We obviously aren't including everything that should be in the "main" sub.

    We're also running on Windows 2000, not XP.

    Thanks.

  • Gurpreet GIll

    How are you setting the images at design time   You should have an ImageList that's filled with your icons.  Do you have any code that modifies the ImageList at runtime   If so, be sure the list contains images.

    Are you changing any properties of the toolbar or toolbarbuttons at runtime   You might want to use a routine similar to the following to check the properties of your toolbar at runtime:

        Private Sub ShowProperties(ByVal Target As System.Object)
            Dim ff As New Form
            Dim p As New PropertyGrid

            p.SelectedObject = Target
            p.Location = New Point(0, 0)
            p.Width = ff.ClientSize.Width
            p.Height = ff.ClientSize.Height
            p.Anchor = AnchorStyles.Left Or AnchorStyles.Bottom Or AnchorStyles.Right Or AnchorStyles.Top

            Try
                ff.Text = Target.Name & " Properties"
            Catch ex As Exception
                ff.Text = Target.GetType.Name & " Properties"
            End Try

            ff.Controls.Add(p)
            ff.Show()
        End Sub

    Just code to your form to call ShowProperties() and pass it your ToolBar object.  You might make another call and pass the ImageList as well.

  • Nirupama


    Application.DoEvents should come after Application.EnableVisualStyles, but since you're not running on XP, it will not solve your problems.

    Have you tried this on another machine  It could be something other than .net causing the problem. For Instance, there is a known problem with McAffeee AntiVirus 8.0i for which there is an update. I don't think that this is your problem since that bug causes text to be transparent, but it could be something similar.

    Since changing ImageIndex at runtime causes the buttons to show the images, could you not simply assign the buttons imageindex at form load.

    Private Sub Form_Load(ByVal Sender As Object, ByVal e As EventArgs) Handles MyBase.Load
        For Each Item As ToolBarButton In ToolBar1.Buttons
            Item.ImageIndex = ToolBar1.Buttons.IndexOf(Item)
        Next
    End Sub

  • IvanHB

    I put this in my code as suggested:
    <CODE>
    Shared Sub Main()
            Application.DoEvents()
            Application.EnableVisualStyles()
            Application.Run(New Form1)
        End Sub
    </CODE>
    and it didn't work. 

    Also, the images that aren't showing up were gifs with some transparent spots. Using Adobe, I tried saving and reloading those images as i) non-transparent gifs, ii) jpgs, and iii) bmps. 

    They all work fine in the Visual Studio form designer. But again, once I press F5 and run the app, none of them show on the buttons. 

    If anyone else has any suggestions, we would appreciate it. 

    Thanks to all who've posted thus far.

  • jmqu1k


    ...were the toolbarbuttons ImageIndex set to none at designtime

    The only other thing I can think to try is to load the Imagelist at form_load. This cures the problem on XP without the need for DoEvents, so it may solve your problem.

    Here's a handy routine that I use for this purpose.

    Sub FillImageListFromResource(ByVal il As ImageList, ByVal fileExtension As String)
        Dim assy As System.Reflection.Assembly = Me.GetType.Assembly
        Dim Images As String() = assy.GetManifestResourceNames
        Array.Sort(Images)
        For Each ResourceName As String In Images
            If ResourceName.EndsWith(fileExtension) Then
                Dim bmp As Image = Image.FromStream(assy.GetManifestResourceStream(ResourceName))
                il.Images.Add(bmp)
                bmp.Dispose()
            End If
        Next
    End Sub


    Now simply add a folder to Solution Explorer and fill it with images.
    Rename the images so that they can be sorted into the order in which you wish to use them and make sure they all have the same filextension (i.e. Image01.gif, Image02.gif)
    Set the Build Action of each image to Embedded Resource
    and then call the routine at Form_Load (i.e. FillImageListFromResource(ImageList1, ".gif"))

  • Satya Priya

    Hi there,

    I have tried it on another machine... this one running Windows 2000 Server. Same issue. My company hasn't and isn't going to make the switch to XP anytime soon, so any sort of test on that platform is out.

    I tried your code and have inserted it as follows:

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For Each Item As ToolBarButton In ToolBar1.Buttons
                Item.ImageIndex = ToolBar1.Buttons.IndexOf(Item)
            Next
    End Sub


    Still doesn't solve the issue as when the application loads those toolbar buttons come up blank. 

    Perhaps there's some obscure combination of properties such that when set the buttons don't appear on startup  

  • Morgan Oslake - MSFT

    Thanks for this! Unfortunately, it still doesn't solve the problem. 

    It's just as before... images show up fine until I hit "F5" to run the application. 

    I still have the properties window come up and display when executing the app. When it does, all the buttons and images associated with them are listed correctly. 

    But if I go in, select another image index, and then immediately select the original image index, hit "ok" so the properties window goes away, the images immediately show up on the toolbar in the running application. 

    I don't suppose there's a way to attach images to postings so I could show you guys what's going on

  • Can't get images on toolbar buttons when running