What am I doing wrong here????


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: What am I doing wrong here????

  1. #1
    Join Date
    Jul 2004
    Posts
    366

    Question What am I doing wrong here????

    I want to change the one lable I have on screen. This code doesn't error, it just doesn't make the changes I want it to make. I've tried passing in the lable I'm working with, that worked! BUT why doesn't this work???

    Thanks all....


    Private Function ChangeLabel() As Label

    Dim lblLabel As New Label
    lblLabel.Text = "5342"
    lblLabel.ForeColor = Drawing.Color.Red

    Return lblLabel

    End Function

    Private Sub frmTest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Label1.Text = "jrfksd"
    Label1.ForeColor = Drawing.Color.Blue

    Label1 = ChangeLabel()

    End Sub

  2. #2
    Join Date
    Feb 2004
    Location
    Longueuil, Québec
    Posts
    577
    I've tried passing in the lable I'm working with, that worked!
    Passing your Label as a parameter was the right way to do it. It worked. So why try something else???

    But anyway...

    ------

    ... let us understand why your code does nothing.

    What you are doing in your code is create a New Label. A new Label is invisible by default, so you have to set its Visible property to True in order to see it on the screen.

    But even if you do so, you will have another surprise. A new Label is always located at position 0,0. You would thus have to also set it's position on the screen.

    And you would have another surprise. You would see that you end up with 2 Labels. The old Label would still be there, but you would not be able to make any change to it.

    A variable is not an object, it is simply a pointer, a way to communicate with the object. When you reassign a variable, the old object (the old Label in your case) stays in memory (the old Label stays on the screen), but your variable is now used to communicate with the new Label, with no way of reconnecting with the previous Label (not really true, but reconnecting would not be a straightforward task).

    When you do

    Code:
    Label1 = ChangeLabel()
    you simply connect the variable with the new Label. But since your new Label is not Visible, you think that nothing happened.

    -----

    I think that what you wanted to do is the following, which I thing you have tried:

    Code:
    Private Sub ChangeLabel(ByVal lblLabel As Label) 
    
    lblLabel.Text = "5342"
    lblLabel.ForeColor = Drawing.Color.Red
    
    End Sub
    
    Private Sub frmTest_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    Label1.Text = "jrfksd"
    Label1.ForeColor = Drawing.Color.Blue
    
    Label1 = ChangeLabel()
    
    End Sub
    Jacques Bourgeois
    JBFI
    http://www3.sympatico.ca/jbfi/homeus.htm

Similar Threads

  1. Listview --- What am I doing wrong?
    By jcb1269 in forum .NET
    Replies: 2
    Last Post: 01-15-2010, 08:29 AM
  2. Can't find error, whats wrong here?
    By Neutron in forum Java
    Replies: 1
    Last Post: 12-20-2005, 02:50 AM
  3. What's wrong with this?
    By Alan Bell in forum .NET
    Replies: 1
    Last Post: 04-01-2002, 10:02 PM
  4. please tell me i'm wrong!
    By mark in forum .NET
    Replies: 9
    Last Post: 03-31-2002, 10:14 AM
  5. arguments are of the wrong type
    By Richard Kalis in forum VB Classic
    Replies: 1
    Last Post: 03-20-2002, 11:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center