C # Winforms: how to get the design time reference of Form1

I have a form that has a public property

public bool cancelSearch = false;

      

I also have a class that sits 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 I can get the method to recognize the form (this custom class and form1 are in the same namespace).

I only tried Form1. but intellisense doesn't recognize the property. Also I tried to instantiate the form using the form f1 = winSearch.Form1.ActiveForm; but that didn't help either

Any ideas?

0


source to share


4 answers


When you call business logic that needs to know the information, pass the form reference to the method.

Something like.

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

      

then when called from an instance of Form1



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

      

HOWEVER

Indeed, you shouldn't be doing this as it binds data tightly, just create a property in your BL class that takes a parameter and uses it that way.

+2


source


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

Advice

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

Solutions to your problem

BUT if you really want to (BUT it really isn't something to do) you need to pass the link. You can do this by passing the reference in the constructor of your class or using 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
    }
}

      

+1


source


I think you should see how to stop the workflow.

I have a strong feeling that you have a Button.Click event handler that handles your business logic and another Button.Click that sets your excellent search variable. It won't work. The GUI thread that will run your business logic will not see another button pressed. If I'm right you should be using a worker thread.

+1


source


As the other (very quick) answers show, you need to have an instance variable for your intelligence to show you what you need.

Your application does not have a reference to an instance of the main form by default, if you look at your program.cs file you can see that the form is built like this ...

Application.Run(new Form1());

      

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

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

      

Pass the business object reference from your workform as some others have suggested

myBusinessObj.DoThisThing (this);

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

+1


source







All Articles