Solved

Button Background Image Not Updating Programmatically

Posted on 2008-10-17
9
996 Views
Last Modified: 2011-10-19
I didn't really notice this until after the project was finished, but I have a series of five buttons down the side of a program that the background changes when you click on them. If I leave the code in the main form, the backgrounds on the buttons change depending on which button you click, but if I move the code to the module and reference the form in the subs the button backgrounds no longer change.

I pasted the code for the My Panel button. All five buttons do the same thing.
Working code (in frmMain):

 

  Public Sub ShowMyPanelPart()

    Me.cmdMyPanel.BackgroundImage = My.Resources.buttonHelpDeskOn

    Me.cmdHelpDesk.BackgroundImage = My.Resources.buttonHelpDeskOff

    Me.cmdTasksProjects.BackgroundImage = My.Resources.buttonTasksProjectsOff

    Me.cmdAssets.BackgroundImage = My.Resources.buttonAssetsOff

    Me.cmdSettings.BackgroundImage = My.Resources.buttonSettingsOff

    m_HelpDeskPart.Visible = False

    m_TasksProjects.Visible = False

    m_Assets.Visible = False

    m_Settings.Visible = False

    m_MyPanelPart.Visible = True

  End Sub

 

Not Working code (in module):

 

  Public Sub ShowMyPanelPart()

    Dim main as frmMain

    main.cmdMyPanel.BackgroundImage = My.Resources.buttonHelpDeskOn

    main.cmdHelpDesk.BackgroundImage = My.Resources.buttonHelpDeskOff

    main.cmdTasksProjects.BackgroundImage = My.Resources.buttonTasksProjectsOff

    main.cmdAssets.BackgroundImage = My.Resources.buttonAssetsOff

    main.cmdSettings.BackgroundImage = My.Resources.buttonSettingsOff

    m_HelpDeskPart.Visible = False

    m_TasksProjects.Visible = False

    m_Assets.Visible = False

    m_Settings.Visible = False

    m_MyPanelPart.Visible = True

  End Sub

Open in new window

0
Comment
Question by:RaineyM
  • 5
  • 3
9 Comments
 
LVL 13

Expert Comment

by:TechTiger007
Comment Utility
Inside your module you are creating a new instance of your form and assigning colors to this new instance once the control goes out of the method this new instance of form is gone.
What you can do is pass form as an input parameter to the module and inside the module modify the color of th form buttons


Public function ShowMyPanelPart(dim main as frmMain)
    main.cmdMyPanel.BackgroundImage = My.Resources.buttonHelpDeskOn
    main.cmdHelpDesk.BackgroundImage = My.Resources.buttonHelpDeskOff
    main.cmdTasksProjects.BackgroundImage = My.Resources.buttonTasksProjectsOff
    main.cmdAssets.BackgroundImage = My.Resources.buttonAssetsOff
    main.cmdSettings.BackgroundImage = My.Resources.buttonSettingsOff
    m_HelpDeskPart.Visible = False
    m_TasksProjects.Visible = False
    m_Assets.Visible = False
    m_Settings.Visible = False
    m_MyPanelPart.Visible = True
  End function

your form should be passing "Me" when it calls this function


0
 

Author Comment

by:RaineyM
Comment Utility
OK, that helped on one part, but now the subs that change the button images that are called from the user controls inside a panel that are loaded from the buttons aren't working either.

For instance, I have a user control that loads inside a panel that loads data from a database into a listview (details view). When you double-click an item, it loads another user control in the panel allowing you to edit the data and it changes the button's images over on the side of the program. Everything is working except the changing of the button images.

When you click on the item in the listview, the Help Desk's button's image is suppose to change to the white background similar to the My Panel's above, but it doesn't because the double-click action is on the listview in the user control, not the main form like the buttons.
Image1.jpg
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
It looks like you are using VB.Net 2005 (or above).

Assuming that frmMain is your "Startup Object", you can just do this in your Module:
Public Sub ShowMyPanelPart()

    frmMain.cmdMyPanel.BackgroundImage = My.Resources.buttonHelpDeskOn

    frmMain.cmdHelpDesk.BackgroundImage = My.Resources.buttonHelpDeskOff

    frmMain.cmdTasksProjects.BackgroundImage = My.Resources.buttonTasksProjectsOff

    frmMain.cmdAssets.BackgroundImage = My.Resources.buttonAssetsOff

    frmMain.cmdSettings.BackgroundImage = My.Resources.buttonSettingsOff

    frmMain.m_HelpDeskPart.Visible = False

    frmMain.m_TasksProjects.Visible = False

    frmMain.m_Assets.Visible = False

    frmMain.m_Settings.Visible = False

    frmMain.m_MyPanelPart.Visible = True

End Sub

Open in new window

0
 

Author Comment

by:RaineyM
Comment Utility
frmMain is not my "Startup Object".
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
Comment Utility
=(

Ok.  How are you displaying frmMain?  Do you ever only have ONE instance of it showing.  If so, then you can display it using what is known as the default instance.  This is done by using the NAME of the Form WITHOUT the "New" keyword.

This creates a New Instance:

    Dim f As New frmMain
    f.Show()

This shows the Default Instance:

    frmMain.Show()

You can access the default instance from ANYWHERE in your project in this manner.

If you can use the default instance instead, then you can use my earlier proposed code.

If not, then you would need to pass a reference to the form as TechTiger007 proposed.
0
 

Author Closing Comment

by:RaineyM
Comment Utility
I'm updating a program from VB6 and this is my first foray into VB.NET. I should have gotten this but I got tunnel-vision and wanted to defer to the experts.
0
 

Author Comment

by:RaineyM
Comment Utility
This did it. I have a splash screen where the user has to log in and I was using a New Instance to call frmMain and when I switched it from:

    Dim main As New frmMain
    main.ShowDialog()

to:

   frmMain.ShowDialog()

and used Idle_Mind's above code, it worked like a charm. Thanks from a VB.NET n00b!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
No problem...

Your last comments though need to be addressed as well.  =\

Your main app is being kept "alive" because the form is displayed with ShowDialog()?

So the splash screen is your Startup Object?  You should leave frmMain as the Startup Object!

Use the Application.Startup() event to display your Splash/Login form.  From there you can cancel the application or allow it to continue to the "main" form specified in Project --> Properties.

PLEASE read this overview:
http://msdn.microsoft.com/en-us/library/w3xx6ewx.aspx

Specifically the Startup() Event which first BEFORE the startup object is displayed:
http://msdn.microsoft.com/en-us/library/t4zch4d2.aspx

Here is some pseudo code to illustrate:


Namespace My
 

    ' The following events are availble for MyApplication:

    ' 

    ' Startup: Raised when the application starts, before the startup form is created.

    ' Shutdown: Raised after all application forms are closed.  This event is not raised if the application terminates abnormally.

    ' UnhandledException: Raised if the application encounters an unhandled exception.

    ' StartupNextInstance: Raised when launching a single-instance application and the application is already active. 

    ' NetworkAvailabilityChanged: Raised when the network connection is connected or disconnected.

    Partial Friend Class MyApplication
 

        Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup

            Dim login As New frmLogin

            If login.ShowDialog <> DialogResult.OK Then

                e.Cancel = True ' Login failed: shut down the app...

            End If
 

            ' Allow the app to continue normally...

            ' ...to the form specified as the "Startup Object" in Project --> Properties

        End Sub
 

    End Class
 

End Namespace

Open in new window

0
 

Author Comment

by:RaineyM
Comment Utility
Thanks. I'll look at that.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now