C#Winforms:如何获取Form1设计时的引用

时间:2022-09-02 12:35:51

I have a form that has a public property

我有一张有公共财产的表格

public bool cancelSearch = false;

I also have a class which is sitting in my bll (business logic layer), in this class I have a method, and in this method I have a loop. I would like to know how can I get the method to recognise the form (this custom class and form1 are in the same namespace).

我还有一个坐在我的bll(业务逻辑层)的类,在这个类中我有一个方法,在这个方法中我有一个循环。我想知道如何让方法识别表单(此自定义类和form1在同一名称空间中)。

I have tried just Form1. but the intellisense doesn't recognise the property. Also I tried to instantialize the form using Form f1 = winSearch.Form1.ActiveForm; but this too did not help

我试过Form1。但智能感知不承认财产。我还尝试使用Form f1 = winSearch.Form1.ActiveForm来实例化表单;但这也没有帮助

Any ideas?

4 个解决方案

#1


2  

When you are calling the business logic that needs to know the information pass a reference of the form to the method.

当您调用需要知道信息的业务逻辑时,将表单的引用传递给方法。

Something like.

public class MyBLClass
{
    public void DoSomething(Form1 theForm)
    {
        //You can use theForm.cancelSearch to get the value
    }
}

then when calling it from a Form1 instance

然后从Form1实例调用它

MyBlClass myClassInstance = new MyBlClass;
myClassInstance.DoSomething(this);

HOWEVER

Really you shouldn't do this as it tightly couples the data, just make a property on your BL class that accepts the parameter and use it that way.

实际上你不应该这样做,因为它紧密耦合数据,只需在BL类上创建一个接受参数的属性并以这种方式使用它。

#2


1  

I think you should look at how to stop a workerthread.

我想你应该看看如何制止一个问题。

I have a strong feeling that you have a Button.Click event handler that runs your business logic and another Button.Click that sets your cancelSearch variable. This won't work. The GUI thread which would run your business logic, won't see the other button being clicked. If I'm right you should very much use a worker thread.

我有一种强烈的感觉,你有一个运行业务逻辑的Button.Click事件处理程序和另一个设置你的cancelSearch变量的Button.Click。这不行。运行业务逻辑的GUI线程不会看到其他按钮被点击。如果我是对的,你应该非常使用工作线程。

#3


1  

Your question is really not clear. You might want to edit it.

你的问题真的不清楚。您可能想要编辑它。

Advice

The form shouldn't pass to your business logic layer...

表单不应传递到您的业务逻辑层...

Solutions to your problem

BUT if you really want to (BUT it's really not something to do), you need to pass the reference. You can do it by passing the reference in the constructor of your class, or by a property.

但如果你真的想要(但它真的不是要做的事情),你需要传递参考。您可以通过在类的构造函数中传递引用或通过属性来执行此操作。

Method with Constructor

public class YourClass
{
    private Form1 youFormRef;

    public YourClass(Form1 youFormRef)
    {
        this.youFormRef = youFormRef;
    }
    public void ExecuteWithCancel()
    {
        //You while loop here
            //this.youFormRef.cancelSearch...
    }
}

Method with Property

public class YourClass
{
    private Form1 youFormRef;

    public int FormRef
    {
        set
        {
            this.youFormRef = value;
        }

        get
        {
            return this.youFormRef;
        }
    }

    public void ExecuteWithCancel()
    {
        //You while loop here
            //this.youFormRef.cancelSearch
    }
}

#4


1  

As the other (very quick) responses indicate, you need to have an instance variable in order to get your intellisence to show you what you need.

正如其他(非常快速)响应所示,您需要拥有一个实例变量,以便让您的智慧向您展示您需要的内容。

Your app by default does not have a reference to the main form instance, if you look at your program.cs file you will see that the form is constructed like so...

默认情况下,您的应用程序没有对主窗体实例的引用,如果您查看program.cs文件,您将看到窗体的构造如此...

Application.Run(new Form1());

so you have a couple options, you could create a global var (yuck) and edit your program.cs file to use it..

所以你有几个选项,你可以创建一个全局var(yuck)并编辑你的program.cs文件来使用它。

Form1 myForm =  new Form1();
Application.Run(myForm);

Pass a reference to the business object from your running form like some others have suggested

像其他人建议的那样,从正在运行的表单中传递对业务对象的引用

myBusinessObj.DoThisThing(this);

or find your form in the Application.OpenForms collection and use it.

或者在Application.OpenForms集合中找到您的表单并使用它。

#1


2  

When you are calling the business logic that needs to know the information pass a reference of the form to the method.

当您调用需要知道信息的业务逻辑时,将表单的引用传递给方法。

Something like.

public class MyBLClass
{
    public void DoSomething(Form1 theForm)
    {
        //You can use theForm.cancelSearch to get the value
    }
}

then when calling it from a Form1 instance

然后从Form1实例调用它

MyBlClass myClassInstance = new MyBlClass;
myClassInstance.DoSomething(this);

HOWEVER

Really you shouldn't do this as it tightly couples the data, just make a property on your BL class that accepts the parameter and use it that way.

实际上你不应该这样做,因为它紧密耦合数据,只需在BL类上创建一个接受参数的属性并以这种方式使用它。

#2


1  

I think you should look at how to stop a workerthread.

我想你应该看看如何制止一个问题。

I have a strong feeling that you have a Button.Click event handler that runs your business logic and another Button.Click that sets your cancelSearch variable. This won't work. The GUI thread which would run your business logic, won't see the other button being clicked. If I'm right you should very much use a worker thread.

我有一种强烈的感觉,你有一个运行业务逻辑的Button.Click事件处理程序和另一个设置你的cancelSearch变量的Button.Click。这不行。运行业务逻辑的GUI线程不会看到其他按钮被点击。如果我是对的,你应该非常使用工作线程。

#3


1  

Your question is really not clear. You might want to edit it.

你的问题真的不清楚。您可能想要编辑它。

Advice

The form shouldn't pass to your business logic layer...

表单不应传递到您的业务逻辑层...

Solutions to your problem

BUT if you really want to (BUT it's really not something to do), you need to pass the reference. You can do it by passing the reference in the constructor of your class, or by a property.

但如果你真的想要(但它真的不是要做的事情),你需要传递参考。您可以通过在类的构造函数中传递引用或通过属性来执行此操作。

Method with Constructor

public class YourClass
{
    private Form1 youFormRef;

    public YourClass(Form1 youFormRef)
    {
        this.youFormRef = youFormRef;
    }
    public void ExecuteWithCancel()
    {
        //You while loop here
            //this.youFormRef.cancelSearch...
    }
}

Method with Property

public class YourClass
{
    private Form1 youFormRef;

    public int FormRef
    {
        set
        {
            this.youFormRef = value;
        }

        get
        {
            return this.youFormRef;
        }
    }

    public void ExecuteWithCancel()
    {
        //You while loop here
            //this.youFormRef.cancelSearch
    }
}

#4


1  

As the other (very quick) responses indicate, you need to have an instance variable in order to get your intellisence to show you what you need.

正如其他(非常快速)响应所示,您需要拥有一个实例变量,以便让您的智慧向您展示您需要的内容。

Your app by default does not have a reference to the main form instance, if you look at your program.cs file you will see that the form is constructed like so...

默认情况下,您的应用程序没有对主窗体实例的引用,如果您查看program.cs文件,您将看到窗体的构造如此...

Application.Run(new Form1());

so you have a couple options, you could create a global var (yuck) and edit your program.cs file to use it..

所以你有几个选项,你可以创建一个全局var(yuck)并编辑你的program.cs文件来使用它。

Form1 myForm =  new Form1();
Application.Run(myForm);

Pass a reference to the business object from your running form like some others have suggested

像其他人建议的那样,从正在运行的表单中传递对业务对象的引用

myBusinessObj.DoThisThing(this);

or find your form in the Application.OpenForms collection and use it.

或者在Application.OpenForms集合中找到您的表单并使用它。