Results 1 to 11 of 11

Thread: Label back colour changing problem

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Angry Label back colour changing problem

    Hello

    I want to change a labels back color on form1 via a button on form 2, both of which are open.

    The code i tried for this is:

    Code:
    Form1.lbl11.BackColor = Color.Green
    But this dosent work, so i assumed its not passing it to the other form correctly so i tried a class on form 1

    Code:
    Public Sub Changegreen()
    
            lbl11.BackColor = Color.Green
            
        End Sub
    Calling it on form2 by

    Code:
    Call form1.Changegreen()
    But this dosent change the color either, ive put a message box in the class to popup which it does so it is running just the color not changing. The only way it will change color is by a click/double click on another item on form1

    What am i missing?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Label back colour changing problem

    Is Form1 the startup form for the application? If not, how did you display it? Is that code in the Click event handler of a Button?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: Label back colour changing problem

    No i have a main menu (the startup form) Which has a button that opens the form that i want to change the label on and that form has the function to open the form im changing the labels color from, if that makes sense!

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Label back colour changing problem

    What code did you use to display Form1 is what I'm asking.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: Label back colour changing problem

    Quote Originally Posted by jmcilhinney View Post
    What code did you use to display Form1 is what I'm asking.
    This :

    Code:
    Private Sub btntoday_Click(sender As System.Object, e As System.EventArgs) Handles btntoday.Click
            Dim f2 As Todayview2
            f2 = New Todayview2
            f2.Visible = True
            Me.Hide()
        End Sub

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Label back colour changing problem

    Then it should be:-
    vbnet Code:
    1. f2.ChangeGreen()
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Label back colour changing problem

    The problem you were having, which JMC was getting at, was that you were using the default instance of the form. Default instances were added with the 2005 edition and have been causing confusion ever since. What the default instances is, is this: If you have a form called Form1, then the compiler (effectively) quietly adds this line:

    Public Form1 As New Form1

    That's not what it really does, as it does something slightly more efficient, but that is effectively what it does. It creates a new instance of the form with the same name as the class itself. However, you didn't show the default instance, as you generally should not. Instead, you created your own instance called f2 and showed that. The default instance is a totally different object from the one you showed, and it is not being displayed, so changes made to it are not visible because the form itself is not visible.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: Label back colour changing problem

    Quote Originally Posted by Shaggy Hiker View Post
    The problem you were having, which JMC was getting at, was that you were using the default instance of the form. Default instances were added with the 2005 edition and have been causing confusion ever since. What the default instances is, is this: If you have a form called Form1, then the compiler (effectively) quietly adds this line:

    Public Form1 As New Form1

    That's not what it really does, as it does something slightly more efficient, but that is effectively what it does. It creates a new instance of the form with the same name as the class itself. However, you didn't show the default instance, as you generally should not. Instead, you created your own instance called f2 and showed that. The default instance is a totally different object from the one you showed, and it is not being displayed, so changes made to it are not visible because the form itself is not visible.
    Ah ok, thanks. So how do i open the form so i can make changes to it?

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Label back colour changing problem

    Well, you almost have it already. The problem is that this line:

    Dim f2 As Todayview2

    is inside a method, so f2 is a local variable to that method and is inaccessible to any code anywhere else. If you want to access it from somewhere else you can just move it out of the method, but I would suggest that you actually make these three changes:

    1) Move the variable declaration outside of any method, as noted.

    B) Change from Dim to Private. There is no significant reason to do this, but Dim is generally not used other than for local variables. You CAN use if for a form level variable, it just isn't generally done, so you might as well change to Private (which you can't use for local variables).

    III) Give the variable a more meaningful name. The name you used, f2, is fine as a local variable that is going to be used in such a brief method. I tend to use nf (for New Form) myself, in those situations. However, if you make the variable a form level variable, such uninformative names are a bad idea, as you will soon forget what you are looking at.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2007
    Posts
    61

    Re: Label back colour changing problem

    Brilliant thanks all working now!

    Why did the class within the form that contained the label not change the color of the label though? In my head its no different then the button in that form that worked?

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Label back colour changing problem

    That's a good question. You didn't show a class, though, you showed a sub. If that sub was part of a class, that would likely be the reason, but if you actually meant sub or method rather than class, and you wrote it exactly as you showed (just plain label11.Backcolor, not Form1.Label11.Backcolor, or any other variation), then the problem has to do with where the method was found or how it was called, or something like that, because the method itself isn't a problem.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width