?
Solved

Button Background Image Not Updating Programmatically

Posted on 2008-10-17
9
Medium Priority
?
1,072 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 13

Expert Comment

by:TechTiger007
ID: 22742767
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
ID: 22743334
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 86

Expert Comment

by:Mike Tomlinson
ID: 22743446
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RaineyM
ID: 22743528
frmMain is not my "Startup Object".
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22743588
=(

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
ID: 31649754
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
ID: 22743750
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 86

Expert Comment

by:Mike Tomlinson
ID: 22743845
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
ID: 22744399
Thanks. I'll look at that.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

752 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