Solved

Controling the text property of a label control on a different form !!!!!!!

Posted on 2006-06-14
8
201 Views
Last Modified: 2010-04-23
Hello all

Anytime I try and set the text property a label control on a diferent form, I get this error :

Object reference not set to an instance of an object.

Here is my code :

F1.lblRfrTime.Text = IntTime
        Me.Close()

"IntTime is a string variable"

How can I resolve this quick ?

Thanks!
0
Comment
Question by:abennett10
  • 3
8 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16905327
What version VB.Net are you using?
0
 

Author Comment

by:abennett10
ID: 16905429
2003
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 16905602
You need a reference to the actual instance of the form you want to update.

What is the relationship of the forms to each other?

If "F1" created by the current form? or vice versa?

Or does something else create both instances?
0
 
LVL 5

Expert Comment

by:t_itanium
ID: 16906458
you can do that by:

declare the second form in the first:

ie
dim form2 as Form2() ' by doing so you r creating an instance of the other form..
form2.label.text="sdsd"

not sure about the syntax

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 16906493
In VS2005, the concept of a "default instance" was brought back into the mix.  This works exactly the same way as it did in VB6...you can use the Forms name to reference the default instance and it can be accessed from anywhere.

VS2002/VS2003 had no such feature.

In those versions you must have a reference to the actual instance of the form you want to deal with.

If you just created the form then you already have an instance.  As in...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim f1 As New Form1
        f1.Show()
        f1.TextBox1.Text = "something"
    End Sub

But if you are attempting to go the other way, as in updating the form that created "f1" in the previous example, from "f1" itself then you need to pass in a reference to that form.  One way to do this is to use the Owner() property:

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim f2 As New Form2
            f2.Owner = Me
            f2.Show()
        End Sub

    End Class

    Public Class Form2
        Inherits System.Windows.Forms.Form

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim f1 As Form1 = CType(Me.Owner, Form1)
            f1.TextBox1.Text = "Form2 calling..."
        End Sub

    End Class

If you don't want to use Owner() then you can pass in a Reference like this:

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim f2 As New Form2
            f2.f1 = Me
            f2.Show()
        End Sub

    End Class

    Public Class Form2
        Inherits System.Windows.Forms.Form

        Public f1 As Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Not (f1 Is Nothing) Then
                f1.TextBox1.Text = "Form2 calling..."
            End If
        End Sub

    End Class

Another approach is to use the Singleton pattern.  This is useful if you will only have ONE instance of your form open at any time.  See my discussion and example here:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21845440.html
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

15 Experts available now in Live!

Get 1:1 Help Now