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
Solved

Form1 this.Left this.Top Static Get?

Posted on 2011-02-16
6
421 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
6 Comments
 
LVL 85

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 29

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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

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 85

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 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