|
-
May 14th, 2013, 06:37 AM
#1
Thread Starter
Member
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?
-
May 14th, 2013, 06:44 AM
#2
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?
-
May 14th, 2013, 06:53 AM
#3
Thread Starter
Member
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!
-
May 14th, 2013, 07:27 AM
#4
Re: Label back colour changing problem
What code did you use to display Form1 is what I'm asking.
-
May 14th, 2013, 08:25 AM
#5
Thread Starter
Member
Re: Label back colour changing problem
 Originally Posted by jmcilhinney
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
-
May 14th, 2013, 09:02 AM
#6
Re: Label back colour changing problem
-
May 14th, 2013, 09:18 AM
#7
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
 
-
May 14th, 2013, 09:37 AM
#8
Thread Starter
Member
Re: Label back colour changing problem
 Originally Posted by Shaggy Hiker
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?
-
May 14th, 2013, 11:45 AM
#9
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
 
-
May 15th, 2013, 04:02 AM
#10
Thread Starter
Member
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?
-
May 15th, 2013, 11:35 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|