Solved

Form1 this.Left this.Top Static Get?

Posted on 2011-02-16
6
424 Views
Last Modified: 2012-06-27
I need to allow other forms to 'get' the Form1 location.

Q. How do I reference this?

[From1.cs]
        internal static int LEFT
        {
            get { return this.Left; }
        }

        internal static int TOP
        {
            get { return this.Top;}
        }
0
Comment
Question by:pointeman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 34912650
Instead of a static field, you need to pass an actual reference to your instance of Form1 INTO the other forms so they can access it.

Can you explain more how this will be used?  This will help us determine which direction the information actually needs to "flow"...
0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 125 total points
ID: 34912884
I have two forms, Form1 and Form2, each with one button. First form opens second one. Second one - shows location of the first form. See the code. I didn't use Left and Top as they hide form's properties (control properties)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TwoForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm2 = new Form2(this);
            frm2.Show();
            this.Hide();
        }
        internal int LeftPosition
        {
            get{ return this.Location.X;}

        }
        internal int TopPosition
        {
            get { return this.Location.Y; }

        }
    }
}

/==============================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TwoForms
{
    public partial class Form2 : Form
    {
        Form1 _parent;
        public Form2(Form1 parentForm)
        {
            InitializeComponent();
            _parent = parentForm;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Left = " + _parent.Left + "; Top = " + _parent.Top);
            
        }
    }
}

Open in new window

0
 
LVL 8

Assisted Solution

by:crysallus
crysallus earned 125 total points
ID: 34912902
Normally I do this by passing Form1 as an argument to the constructor when creating new forms, assuming that you are creating the other forms from within Form1, using 'this'. eg.

SettingsForm settingsForm = new SettingsForm(this);

Open in new window


Which means the constructor for SettingsForm looks like

private Form1 mainForm;
public SettingsForm(Form1 form1)
{
    mainForm = form1;
    ...
}

Open in new window


Then you can access Form1 anywhere within SettingsForm, using the mainForm variable eg. mainForm.Left etc.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:pointeman
ID: 34918298
All very interesting solutions.

I do need to change my initial question slightly. I'm opening Form2 (custom message box) from within a class, NOT a win form. So I'm trying to get the class to pull the Left, Top params of Form1.

I don't know if this makes a diffence!

I'm combating dual-monitor installations where Main WinForm is positioned on monitor2, but popup forms always position on monitor1. Also discoved that CenterParent is not always a good solution.

I have also passed params to other win forms in similarly like so:

[Form1]
      Form2 f2 = new Form2(this.Left, this.Top);
      f2.Show();

[Form2]
     internal partial class Form2 : Form
    {
        internal Form2(int left, int top)
        {
            InitializeComponent();

            this.StartPosition = FormStartPosition.Manual;
            this.Location = new Point(left + 50, top + 50);
        }
    }
 
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 34918377
"I'm opening Form2 (custom message box) from within a class, NOT a win form. So I'm trying to get the class to pull the Left, Top params of Form1."

So where was the class created from?  Sounds like you need to pass a reference to Form1 into the Class,...so later that information and/or reference can then be passed on to Form2.
0
 

Author Closing Comment

by:pointeman
ID: 34918717
Thanks all..........
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

628 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