473,885 Members | 2,463 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring a reference on one form to another form?

Hi All,

I have an app that presents a small main form when run. When a
particular button is clicked, I'd like to briefly display another small
form directly below the main form, regardless of where on the screen
the main form is currently located.

I'm trying to work out how, from the class that displays the second
form, I reference the x and y coords of the main form so I can set
these for the second form when it is displayed?

So, in generic terms, I'm trying to work out how I reference, from a
class, properties and methods of an already displayed / instantiated
form?

Any help appreciated!

Regards,

pt

Sep 25 '06 #1
7 10625
Hi,

For a form you can use the owner property, that gives you the change to get
very easy there.
(While from an owner to a child of that is of course a normal reference)

http://msdn2.microsoft.com/en-us/library/decz3b5c.aspx

I hope this helps,

Cor

"planetthoughtf ul" <pl************ **@gmail.comsch reef in bericht
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
Hi All,

I have an app that presents a small main form when run. When a
particular button is clicked, I'd like to briefly display another small
form directly below the main form, regardless of where on the screen
the main form is currently located.

I'm trying to work out how, from the class that displays the second
form, I reference the x and y coords of the main form so I can set
these for the second form when it is displayed?

So, in generic terms, I'm trying to work out how I reference, from a
class, properties and methods of an already displayed / instantiated
form?

Any help appreciated!

Regards,

pt

Sep 25 '06 #2
Cor Ligthert [MVP] wrote:
Hi,

For a form you can use the owner property, that gives you the change to get
very easy there.
(While from an owner to a child of that is of course a normal reference)

http://msdn2.microsoft.com/en-us/library/decz3b5c.aspx

I hope this helps,

Cor
Hi Cor,

Sorry to be stupid, but I'm still having problems understanding how
this works. Let me explain a little further, in case it becomes clearer
where I'm getting lost.

I have a mainform on which I have a text field and a button.

When text is entered into the text field and the button is clicked, I
create a class that primarily breaks up the text entered in the text
field on the main form and saves it to a database, depending on several
factors.

In that class, I also display a form (let's call it "Form2") to confirm
how the text in the text field was handled. Because my main form is a
small window, I want Form2 to be displayed below it, so in the class I
need to be able to retrieve the x,y coords of my mainform, so I can use
these to set the Left and Top properties of Form2 when I instantiate
it.

I hope I've explained myself a little better, and apologies for not
being more explicit originally.

All the best,

pt

Sep 25 '06 #3
Hi,

If you declare in your mainform the used form as

Form2 frm2 = new Form2();
frm2.Owner = this;

Than you can use it in form2 as
Points mypoints = ((Form1)this.Ow ner). etc.;
(I did not check if it was Points, just as sample)

I hope this gives an idea,

Cor
"planetthoughtf ul" <pl************ **@gmail.comsch reef in bericht
news:11******** **************@ h48g2000cwc.goo glegroups.com.. .
Cor Ligthert [MVP] wrote:
>Hi,

For a form you can use the owner property, that gives you the change to
get
very easy there.
(While from an owner to a child of that is of course a normal reference)

http://msdn2.microsoft.com/en-us/library/decz3b5c.aspx

I hope this helps,

Cor

Hi Cor,

Sorry to be stupid, but I'm still having problems understanding how
this works. Let me explain a little further, in case it becomes clearer
where I'm getting lost.

I have a mainform on which I have a text field and a button.

When text is entered into the text field and the button is clicked, I
create a class that primarily breaks up the text entered in the text
field on the main form and saves it to a database, depending on several
factors.

In that class, I also display a form (let's call it "Form2") to confirm
how the text in the text field was handled. Because my main form is a
small window, I want Form2 to be displayed below it, so in the class I
need to be able to retrieve the x,y coords of my mainform, so I can use
these to set the Left and Top properties of Form2 when I instantiate
it.

I hope I've explained myself a little better, and apologies for not
being more explicit originally.

All the best,

pt

Sep 25 '06 #4

Cor Ligthert [MVP] wrote:
Hi,

If you declare in your mainform the used form as

Form2 frm2 = new Form2();
frm2.Owner = this;

Than you can use it in form2 as
Points mypoints = ((Form1)this.Ow ner). etc.;
(I did not check if it was Points, just as sample)

I hope this gives an idea,
Hi Cor,

I think I'm beginning to understand. It's best to instantiate a
secondary form from within your main form, rather than from within a
'worker' class.

Still, it seems strange that I can't find a syntax to refer to an
object that has been created in my application, no matter where I
happen to be in the code.

I had expected to be able to:

- start up main form
- instantiate class on click of button on main form
- refer to properties of main form from within class
- use properties determined from main form to open form2 from within
class

For the life of me though, I can't figure out how to do this.

Instead, it looks like I need to:

- start up main form
- instantiate class on click of button on main form
- determine properties of main form from click of button on main form
- instantiate form2 from click of button on main form

All the best,

pt

Sep 25 '06 #5
planetthoughtfu l <pl************ **@gmail.comwro te:
I think I'm beginning to understand. It's best to instantiate a
secondary form from within your main form, rather than from within a
'worker' class.
Not necessarily - but if the 'worker' class needs to know about the
form, it will need a reference to it.
Still, it seems strange that I can't find a syntax to refer to an
object that has been created in my application, no matter where I
happen to be in the code.
Think of the form as an object (which it is). You could have multiple
instances of the form - how would the code know which one you meant?

If you're absolutely sure that you'll only ever have one instance of
the form, you could create a static property in the form, with a static
variable backing it, and set the static variable's value to "this" in
the constructor, eg:

class MyForm : Form
{
static MyForm instance;

public static MyForm Instance
{
get { return instance; }
}

public MyForm()
{
instance = this;
}

...
}

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 25 '06 #6

Jon wrote:
Think of the form as an object (which it is). You could have multiple
instances of the form - how would the code know which one you meant?

If you're absolutely sure that you'll only ever have one instance of
the form, you could create a static property in the form, with a static
variable backing it, and set the static variable's value to "this" in
the constructor, eg:

class MyForm : Form
{
static MyForm instance;

public static MyForm Instance
{
get { return instance; }
}

public MyForm()
{
instance = this;
}

...
}

Hi Jon,

Thanks for this! That was very helpful! And, yes, in this particular
application at least, the main form will only be instantiated once, and
any other forms in the application may be instantiated more than once,
but only once at a time, if you get my meaning.

Thank you again!

pt

Sep 25 '06 #7
planetthoughtfu l <pl************ **@gmail.comwro te:
Thanks for this! That was very helpful! And, yes, in this particular
application at least, the main form will only be instantiated once, and
any other forms in the application may be instantiated more than once,
but only once at a time, if you get my meaning.

Thank you again!
No problem. I should have said, by the way, that using static variables
like this just to get access to something which could have been passed
around instead isn't very nice in terms of object orientation. It adds
a restriction which may be okay now but annoying later on. It's worth
trying to think about whether there are other ways round it, but don't
let it stop you from getting your work done :)

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 26 '06 #8

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

Similar topics

11
8826
by: Vanessa | last post by:
Hi, I would like to know whether there's any way for me to pass an object by reference to another form? Regards Vanessa
29
4065
by: Friday | last post by:
Sorry if this is the wrong group. I tried to find the one I thought would be most relevant. I'm an old PHP guy, who knows little about asp and NOTHING about asp.net, but need to learn at least enough to convert a favorite PHP script to work on an ASP.NET site. I'm experimenting with simple example scripts that will help me learn how to implement each "piece" of the puzzle.
4
1473
by: Vaughn | last post by:
When I pass a reference to my current form, frm_x, when I create and show another form, frm_y, do I have access to all of the public methods, controls, members, etc.. of frm_x from frm_y? In my code, I pass the reference like this: private void btn_ListEmp_Click(object sender, System.EventArgs e) { frm_EmpList frm_empList = new frm_EmpList(this); frm_empList.MdiParent = this.MdiParent; frm_empList.Show();
12
11981
by: Bill Todd | last post by:
Form B needs to call a public method of Form A. How can form B get a reference to the instance of Form A using its name or any other method? Is there a collection of form instances that can be searched? -- Bill
2
2606
by: Suzanne | last post by:
Hi all, I'm reposting this message as I'm experiencing this problem more and more frequently : I really hope someone out there can help me as I've been tearing my hair out on this one for a good while and I'm getting really frustrated now! My problem is this - my custom controls periodically disappear from my
18
1673
by: Nathan | last post by:
If you're wondering why I post so many questions, it's because I want to make an entry in the Guinness Book of World Records. But because I post so many, I try to make them simple. Here is (I hope) a simple one: I've declared a "control array" just after the WFD generated code like this: Dim LabelArray() as Label = {Label0, Label1, Label2, Label3, Label4, Label5} But when I try to do something like this:
15
2976
by: CR | last post by:
I've noticed that the trend these days is to declare variables in the middle of code instead of at the top. What is the advantage of this? It seems like it makes it hard to reuse variables. Here is how all the examples I've seen so far create an OleDbCommand Object: Dim cmd as new OleDbCommand("Select * FROM Table1",cnn) I had to figure out that it was the same as this:
3
2191
by: mark | last post by:
When I declare an array as double(,) then try to use it I get an error: "Object reference not set to an instance of an object." I have found that I can redim the array and all is well. Is my approach proper here or is the a better way for setting the instance of this specific format for an array. -- mark
8
7528
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine, like this: for(var i=0; i<list; i++) { var name = list; }
0
9956
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9799
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
10770
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
10871
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
10427
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9592
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...
0
7139
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4627
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

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.