简体   繁体   中英

How do you set a variable value from another form?

c# .Net 3.5 visual studio 2008, windows xp

I have a main form in a project, given a specific set of circumstances another form is instantiated and displayed to the user:

Form frmT = new frmTargetFolder(expName, this);
        frmT.Show();

As you can see, I am passing a reference to the new form from the current one. My question is, what do I have to do to a method so that it is exposed to the new form, the same for a variable?

I have tried defining the functions as public, but I can't seem to access them, also I have written a Get and Set method for a variable, again how do I expose these functions and methods to other forms?

public void hit()
    {
        MessageBox.Show("hit it");
    }


bool setOverRide
    {
        get
        {
            return OverRide;
        }
        set 
        {
            OverRide = value;

        }
    } 

The main form is called frmDataXfer and the form, form which I am trying to call the functions and methods of frmDataXfer is called frmTargetFolder, an instance of which is created in the frmDataXfer and referenced as frmT.

Thanks, R.

it's already discussed something similiar to ur question here..

Anyway to access a variable of a form class, simply make the variable public and you can access it using an object of that class

Simple Example:

class Test : Form
{
    ___public int variable = 10; // visible!
    ___public Test() {}
}

This may help further.

您需要将frmDataXfer的实例存储为frmTargetFolder类中的字段,然后在该实例上调用方法。

Your setOverride property is not marked public . Aside from the fact that properties shouldn't be named this way ( setOverride sounds like the name of a method, not a property), the default access modifier for class members is private . You need to add the public modifier to all members that you want to make public.


Edit:

Looking at the code again, the problem is clearly here:

Form frmT = new frmTargetFolder(expName, this);
frmT.Show();

Writing this, you'll only get the methods/properties that are members of the Form class. That doesn't include any of the members you added to the specific form class. Your code needs to be written as:

frmTargetFolder frmT = new frmTargetFolder(expName, this);
frmT.Show();

If you declare it as the correct type, then you can code against the new properties.

Just remember that forms are first-and-foremost classes like any other, they just inherit from System.Windows.Forms.Form to give it special UI functions.

So, that being said, any public (or internal in the same project) field, property or method is accessible provided you have an instance of the object.

You don't need to store a form as a field to be able to reference it. You could just pass it as a parameter to a method call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM