如何从表单1中的Form2调用方法

时间:2014-09-03 06:10:48

标签: c# forms location

我在表格2中有一个方法

public void set_location(int lx, int ly)
    {
        this.Location = new Point(lx, ly);
    }

当事件发生时,需要从Form1调用

 private void Form1_LocationChanged(object sender, EventArgs e)
    {
        loc_x = this.Location.X;
        loc_y = this.Location.Y;

    }

我将Form2初始化为新线程

   private void Form1_Load(object sender, EventArgs e)
    {

        Thread newThread = new Thread((ThreadStart)delegate { Application.Run(new Form2()); });
        newThread.Start(); 
    }

如何调用方法set_location(x,y);来自Form1?

3 个答案:

答案 0 :(得分:1)

首先,第二个帖子没有任何好处。

最简单的方法是在form2的构造函数

中传递对form1的引用
private void Form1_Load(object sender, EventArgs e)
{    
    Form2 form2 =new Form2();
    form2.Show(this);
}


private Form1 Parent {get;set;}
public Form2(Form1 form)
{
    this.Parent = form;
}

然后你可以正常引用它的属性

this.Parent.Form1_LocationChanged(this, null);

在回复您的评论时,还要在form1中保留对Form2的引用,然后只需调用

private void Form1_LocationChanged(object sender, EventArgs e)
{
    ...
    this.form2.set_location(loc_x, loc_y);
}

答案 1 :(得分:0)

public static void set_location(int lx, int ly)
{
    this.Location = new Point(lx, ly);
}

请告诉我这项工作是否

答案 2 :(得分:0)

  

Program.cs的

static class Program
    {
        public static Form GlobalMainForm;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            GlobalMainForm = new Form1();
            Application.Run(GlobalMainForm);
        }
    }
  

Form1.cs的

public partial class Form1 : Form
    {
        public Form2 Form2GlobalInstance { get; set; }
        public Form1()
        {
            InitializeComponent();
            Form2GlobalInstance = new Form2();
            Thread newThread = new Thread((ThreadStart)delegate { Application.Run(Form2GlobalInstance); });
            newThread.Start();
            Load += delegate
            {
                var frm2Location = Form2GlobalInstance.GetLocation();
                MessageBox.Show("location.x=" + frm2Location.X + " location.y=" + frm2Location.Y);
            };
        }

    }
  

Form2.cs

public partial class Form2 : Form
{

    public Form2()
    {
        InitializeComponent();
    }

    public Point GetLocation()
    {
        return new Point(Location.X, Location.Y);
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        MessageBox.Show("location.x=" + Program.GlobalMainForm.Location.X + " location.y=" + Program.GlobalMainForm.Location.Y);
    }
}
  

更新1:

static class Program
{
    public static Form MainForm;

    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        MainForm = new Form1();
        Application.Run(MainForm);
    }
}

<强> From2:

public void Form1_LocationChanged(object sender, EventArgs e)
{
        loc_x =Program.MainForm.Location.X;
        loc_y = Program.MainForm.Location.Y;    
}

OR 在新线程中运行方法的方法 LINK