Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Form1 this.Left this.Top Static Get?

Posted on 2011-02-16
6
Medium Priority
?
428 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 1000 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 500 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 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

688 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