473,770 Members | 5,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.ActiveFor m.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 12052
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 classConstructo r(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*****@arserr c.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.ActiveFor m.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.Mai nForm = 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*****@arserr c.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.ActiveFor m.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
2949
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 into a variable array in my main form, main (form1.frm). How do I code a control to get these images into an array in my main form from the images.frm? TIA
3
1718
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 must i cast it in some way? Thanx, Giulio
3
17907
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
3770
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 have it run in the main thread. I have read many examples and tried numerous approaches based on delegates all of which will launch the method, but never in the main thread, always in the secondary thread. Is what I'm trying to accomplish...
8
1330
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 Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Clic Cursor = System.Windows.Forms.Cursors.WaitCurso Dim EndItem As New For EndItem.ShowDialog( Cursor = System.Windows.Forms.Cursors.Defaul...
7
3389
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 with the code below. It seems to work(!!!) in that when coding the second form I can see the DisplayStatusMsg of the main form. During debug the code runs through & seemingly executes the call without error. But!...The message is not displayed....
4
3310
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 a db of scientific article citations and data extracted from the papers. When I first made this db I knew nothing of normalization, and that has been a MAJOR pain, as you can well imagine. I have worked to get this normalized and I have just one...
3
3944
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 don't know the name of the instance of it since it was generated by the VS C# Express 2005 designer. In program.cs, I notice that there is the line: Application.Run(new FormMain()); I guess I could replace this with: FormMain fMain = new FormMain()...
6
2323
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 making this harder than it needs to be, but seem to be buffaloed at this point. thanks to all.... -
0
9432
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10232
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8891
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7420
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3974
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.