Solved

Form1 this.Left this.Top Static Get?

Posted on 2011-02-16
6
423 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

734 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