|
ms
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
set location of Form2 based on Location of Form1?...Form1_Load(...) ... Dim pt As Point pt.X = Form1.Location.X + 500 pt.Y = Form1.Location.Y + 100 Me.Location = pt I am currently translating an old app to C# from VB.Net. I tried this in C#: ...Form1_Load(...) ... Point pt; pt.X = Form1.Location.X + 500; <-- error here pt.Y = Form1.Location.Y + 100; <-- error here this.Location = pt; But I get an error message saying that an object reference is required for a nonstatic field/method/property. Could someone tell me what I need to do here? Thanks Rich *** Sent via Developersdex http://www.developersdex.com *** "Rich P" <rpng***@aol.com> wrote in message VB automagically defines a variable with the same name as the form that you news:u71RMJc%23JHA.4984@TK2MSFTNGP05.phx.gbl... > But I get an error message saying that an object reference is required > for a nonstatic field/method/property. Could someone tell me what I > need to do here? can refer to without ever explicitly creating an instance of that form. C# does not let you do this. You'll need to make a variable (or list, or array) of type Form1 and explicitly instantiate it, then use that variable instead of "Form1". >> VB automagically defines a variable with the same name as the form thatyou can refer to without ever explicitly creating an instance of that form. C# does not let you do this. You'll need to make a variable (or list, or array) of type Form1 and explicitly instantiate it, then use that variable instead of "Form1". << So I have Form1 (primary) and Form2 (secondary). Form2 is small compared to Form1. The goal is to open Form2 in the lower right quadrant of Form1 - thus, I need to know the location of Form1. Here is what I am interpreting your suggestion to be: public static Form1 frm1; //declared in class Form1 Here is where I get confuse: I need to instantiate frm1. I will be opening and closing Form2. Form1 can get moved around. I am thinking I could instantiate frm1 at the event where I show Form2: protected void ShowForm2() [ frm1 = new Form1; Form2.Show;} Then in Form2_Load() I get frm1.Location? This is what I will try. Please correct me if I don't have it straight. Rich *** Sent via Developersdex http://www.developersdex.com *** >> protected void ShowForm2() [frm1 = new Form1; Form2.Show;} << I am having a problem with this method because frm1 is picking up the default location of Form1 and not the current location. I tried to assign the current location but got the error message that location is not a variable. I can get the location using the public property. How do I get the location using a public static form variable? Rich *** Sent via Developersdex http://www.developersdex.com *** Rich P <rpng***@aol.com> wrote:
> Stop trying to pass everything around with global variables:>I am having a problem with this method because frm1 is picking up the >default location of Form1 and not the current location. I tried to >assign the current location but got the error message that location is >not a variable. I can get the location using the public property. How >do I get the location using a public static form variable? class Example1 : Form { public Example1() { Text = "Example 1"; } } class Example2 : Form { Example1 m_attachTo; public Example2(Example1 attachTo) { m_attachTo = attachTo; attachTo.Move += new EventHandler(AttachTo_MoveResize); attachTo.Resize += new EventHandler(AttachTo_MoveResize); Load += new EventHandler(Example2_Load); Text = "Example 2"; } void Example2_Load(object sender, EventArgs e) { Left = m_attachTo.Right; Top = m_attachTo.Top; } void AttachTo_MoveResize(object sender, EventArgs e) { if (m_attachTo.WindowState == FormWindowState.Normal) { Left = m_attachTo.Right; Top = m_attachTo.Top; } } } static void ExampleMethod() { Example1 example1 = new Example1(); example1.Show(); Example2 example2 = new Example2(example1); example2.Show(); } -- --------- Scott Seligman <scott at <firstname> and michelle dot net> --------- A free society is a place where it's safe to be unpopular. -- Adlai Stevenson "Rich P" <rpng***@aol.com> wrote in message If this is the ONLY reason you would need to know anything about Form1 from news:udgGTLm%23JHA.4692@TK2MSFTNGP02.phx.gbl... > So I have Form1 (primary) and Form2 (secondary). Form2 is small > compared to Form1. The goal is to open Form2 in the lower right > quadrant of Form1 - thus, I need to know the location of Form1. Form2, then all you need to do is to create an overload of Form2's constructor which takes a Point object that specifies Form1's current location. Form2 would look like this (hideous class name and all): public Form2() { } public Form2(Point parentLocation) { // Do something with the X and Y properties } Then, from Form1, you'll do this: private void ShowForm2() { Form2 newForm = new Form2(this.Location); newForm.Show(); } Notice that you are creating an instance of Form2 and then using that instance variable to do things--which is the C# way--not simply using Form2.Show()--which is the VB way. Now, the sample I gave you above has a potential flaw: it allows you to create multiple instances of Form2, which may or may not be what you want. If you only want a single instance of Form2, you'll have to make the variable a module-level variable (what C# calls a "field," although I like the VB-ish term "module-level variable" better since I find it more descriptive, albeit wordier). So it would look like this: public class Form1 : Form { private Form2 _form2Instance; private void ShowForm2() { if (_form2Instance == null || _form2Instance.IsDisposed) { _form2Instance = new Form2(this.Location); _form2Instance.Show(); } else { _form2Instance.BringToFront(); } } } If you need to know more things about Form1 from Form2, I still recommend overloading Form2's constructor, this time passing the instance of Form1 to it: // Module-level variable private Form1 _parent; public Form2(Form1 parent) { _parent = parent; } Now you can get everything you want to know about Form1 using the _parent variable. (Well, as long as Form1 exposes data as public or internal....) Well, I figured something out -- I added a public static property pPTto
the form and then added a public static Point var mainPt. I set the value of mainPt.X = this.Location.X and mainPt.Y = this.Location.Y in the Form1_Load event. Then when I call Form2 I read the value of pPT in Form2_Load. That seems to work. Rich *** Sent via Developersdex http://www.developersdex.com *** "Rich P" <rpng***@aol.com> wrote in message That is a total Rube Goldberg solution and will break down if you ever open news:eGsWYxc%23JHA.5064@TK2MSFTNGP03.phx.gbl... > Well, I figured something out -- I added a public static property pPTto > the form and then added a public static Point var mainPt. I set the > value of mainPt.X = this.Location.X and mainPt.Y = this.Location.Y in > the Form1_Load event. Then when I call Form2 I read the value of pPT in > Form2_Load. That seems to work. more than one instance of Form1. You really need to drop some VB habits, not try to reproduce them in C#. "Jeff Johnson" <i.get@enough.spam> wrote in message Preferably all of them...news:exIIbBl%23JHA.4432@TK2MSFTNGP05.phx.gbl... > You really need to drop some VB habits Thank you all for your replies. Yes, I had considered the constructor
thing. I HAVE doen this in VB, but -- being lazy, I liked the VB Form var technique. But it sounds like in C# passing a form to the other form's constructor is the way to go. And yes, I only want one instance of form2. Here is what I have been doing (in VB) which seems to work in C# //actual form names private void radAreaCodes_Click(object sender, EventArgs e) { if (frmAcodes == null) { frmAcodes = new frmAreaCodes(); frmAcodes.FormClosed += frm_Closed; frmAcodes.Show(); } else frmAcodes.BringToFront(); } private void frm_Closed(object sender, FormClosedEventArgs e) { Form frm = (Form)sender; if (frm.Name.Equals("frmAreaCodes")) frmAcodes = null; if (frm.Name.Equals("anotherfrm")) anotherfrm = null; .. } so, I will pass "this" in frmAcodes = new frmAreaCodes(this); I have to do this for multiple forms (all different), thus I use frm_Closed to remove the respective form from the heap (stack ...) once I close that form (is this a VB technique?). Please advise if there is a better technique for this in C# when closing a form or if this technique will suffice Rich *** Sent via Developersdex http://www.developersdex.com *** "Rich P" <rpng***@aol.com> wrote in message I believe what you're doing in the frm_Closed handler is what I was news:OAM3ijn%23JHA.2872@TK2MSFTNGP05.phx.gbl... > I have to do this for multiple forms (all different), thus I use > frm_Closed to remove the respective form from the heap (stack ...) once > I close that form (is this a VB technique?). Please advise if there is > a better technique for this in C# when closing a form or if this > technique will suffice accounting for by testing the IsDisposed property of the variable in the sample code I gave you. When is form is closed it will be disposed, so I checked for that and created a new form if so.
Other interesting topics
OT: missing posts
Linq to Sql Exception Specified cast is not valid LINQ Max Linq to Sql insert fails check console args C# Export Excel to client side Application configuration settings - Do I make global? Remoting, how to create the remote server Logical and Visual Trees show and hide and iframe from code behind not using button click g |
|||||||||||||||||||||||