Solved

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

Posted on 2006-06-14
8
206 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
[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
  • 3
8 Comments
 
LVL 86

Expert Comment

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

Author Comment

by:abennett10
ID: 16905429
2003
0
 
LVL 86

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 86

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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

736 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