Solved

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

Posted on 2006-06-14
8
200 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

23 Experts available now in Live!

Get 1:1 Help Now