Solved

Form1 this.Left this.Top Static Get?

Posted on 2011-02-16
6
418 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:pointeman
Comment Utility
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
Comment Utility
"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
Comment Utility
Thanks all..........
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now