473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Main form instance name

In a simple Windows forms application, the main form is
called Form1. Within this form's class, I refer to the
the running instance of this main class as "this" as in
this.Text = "NEW TEXT"

I want to do something like change this Text on the Form1
window from within another class. Trying
Form1.Text = "TEXT FROM CLASS"
or
Form1.ActiveForm.Text = "TEXT FROM CLASS"
lead to errors such as "object reference not set to an
instance of an object". This makes sence because Form1 is
the class name and not the instance name. However, what
is the instance name, the name of the instance that is
referred to as "this" within the main class?

Thanks for any input.
Nov 15 '05 #1
2 11969
You ar eright, your problem is that you are trying to access Form1 which is
the actual class and not the object. The default Main() creates an
annonymous object of Form1. Therefore, you dont actually have a handle on
the form.
I would suggest that you pass a handle to your form in your classes'
constructor (ie: public classConstructor(Form1 handleToForm1) and save it as
a local variable of type Form1. That way, any public variables would be
accessible. Or safer yet, if you know your only going to change the Text
value, just send a handle to that variable.

hope this helps

Marco
"Bill D" <wd*****@arserrc.gov> wrote in message
news:06****************************@phx.gbl...
In a simple Windows forms application, the main form is
called Form1. Within this form's class, I refer to the
the running instance of this main class as "this" as in
this.Text = "NEW TEXT"

I want to do something like change this Text on the Form1
window from within another class. Trying
Form1.Text = "TEXT FROM CLASS"
or
Form1.ActiveForm.Text = "TEXT FROM CLASS"
lead to errors such as "object reference not set to an
instance of an object". This makes sence because Form1 is
the class name and not the instance name. However, what
is the instance name, the name of the instance that is
referred to as "this" within the main class?

Thanks for any input.

Nov 15 '05 #2
In your 2nd for (or class), you will need to create a field to hold a
reference to your Form1 object.

One way to do this is to create a property in the 2nd class and then have
Form1 assign itself to that property.

In 2nd class, do something like this:

private Form1 mainForm;
public Form1 MainForm
{
get{return mainForm;}
set{mainForm = value;}
}

In your Form1 class (maybe in the ctor), do something like this:
secondClass.MainForm = this;

Then in your 2nd class you can access all the public properties of Form1
thru the private mainForm field (now a member of your 2nd class).

In order to make it even cleaner, I would probably make one or both of these
classes singletons.
Going one step further, the design issue to consider is that you probably
don't want your 2nd class to have to know much or anything about Form1. I'm
just guessing, because you didn't say what your second class was. But the
problem with the above suggestion is that you don't really want both classes
to be dependent on each other and one way you can fix this is with
singletons, but I'm probably getting too far from your original question
with that discussion.

HTH
Mountain

"Bill D" <wd*****@arserrc.gov> wrote in message
news:06****************************@phx.gbl...
In a simple Windows forms application, the main form is
called Form1. Within this form's class, I refer to the
the running instance of this main class as "this" as in
this.Text = "NEW TEXT"

I want to do something like change this Text on the Form1
window from within another class. Trying
Form1.Text = "TEXT FROM CLASS"
or
Form1.ActiveForm.Text = "TEXT FROM CLASS"
lead to errors such as "object reference not set to an
instance of an object". This makes sence because Form1 is
the class name and not the instance name. However, what
is the instance name, the name of the instance that is
referred to as "this" within the main class?

Thanks for any input.

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: JC | last post by:
How do I extract data from a form into the main form for processing? For instance, I have several image files in a form called images(images.frm), indexed from 0 to 99, and I want to import these...
3
by: Giulio Mastrosanti | last post by:
Which is the instance name of the starting form of an application? I want to access his controls and variables from another form but can't find the way... Which is the property I can use and...
3
by: Ray Stevens | last post by:
How do you access properties of the main program's class from another form? There does not apear to be an instance variable that can be used.
3
by: Dale Lundgren | last post by:
I have a c# class library that launches a Win Form in a secondary thread. From the Form (now running in the secondary thread) I need to be able to start a method that is defined in the class and...
8
by: Elena | last post by:
I have a form with a main menu. From that menu, I want to select another form. I have the code behind it, yet when the form appears it's blank. It is not the one I've created This is the code...
7
by: Terry | last post by:
I have a Mainform with a Statusbar. When opening another form or doing some processing I want to display info in the Statusbar of the Mainform. I have read a lot of articles on this & have come up...
4
theaybaras
by: theaybaras | last post by:
Hi everyone, You've all been such a huge help to me since joining, and I'd just like to take a second to let you know how much I appreciate it! That said, I have another supplication! ;) I have...
3
by: Jon | last post by:
My main form opens up another form, and from this other form, I'd like to access things in the main form. The problem is that although I know the name of the class of the main form (FormMain) I...
6
by: forest demon | last post by:
i have a custom control that gets loaded at runtime. i need to be able to access a property of a control thats part of the main form, through the clcik event of the custom control. i may be...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.