如何从C#中的另一个表单获取变量值[重复]

时间:2022-07-20 23:40:11

This question already has an answer here:

这个问题在这里已有答案:

I have 2 forms. Form 1 is mainForm and form 2 is WordSearch. I want to pass the value of newSystemRec from WrodSearch to mainForm. My both forms have access to datalayer.cs file. From where I get my newSystemRec vale in WordSearch form. this is my WordSearch code:

我有两种形式。表单1是mainForm,表单2是WordSearch。我想将newSystemRec的值从WrodSearch传递给mainForm。我的两个表单都可以访问datalayer.cs文件。从我在哪里获得WordSearch形式的newSystemRec值。这是我的WordSearch代码:

public enum Category { Schematic, Component }

public partial class WordSearch : Form
{
    private DataLayer dataLayer;
    private MainForm mainForm;
    public event EventHandler<JumpToEventArgs> JumpTo;
    public event EventHandler ModeChanged;
    public event EventHandler SystemChanged;
    public event EventHandler<SimEventArgs> SimChanged;
    private SystemMode systemMode;
    private p2p p2pMode, originalMode;
    private Int32 systemRec;
    private string fileName;
    public Int32 projectRec { get; set; }

    public WordSearch(Category category, String title)
    {
        InitializeComponent();
        this.dgSearch.Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
        this.Icon = Properties.Resources.TDXm;
        if (category == Category.Schematic)
            SetDefaultCategory(true, false);
        else
            SetDefaultCategory(false, true);            
        dataLayer = new DataLayer(false);
        rbRefDes.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
        rbComp.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
        rbSchematic.CheckedChanged += new System.EventHandler(this.rb_CheckedChanged);
        rdoTrace.Click += new EventHandler(this.rbTraceAndSim);
        rdoSim.Click += new EventHandler(this.rbTraceAndSim);
        lblModel.Text = title;
    }

    private void WordSearch_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (fileName == null) return;
        if (originalMode != p2pMode)
        {
            if (ModeChanged != null)
                ModeChanged(this, EventArgs.Empty);
        }
        //Need to pass the value below
        Int32 newSystemRec = dataLayer.GetSystemType(fileName, p2pMode);
        if (systemRec != newSystemRec)
        {
            if (SystemChanged != null)
                SystemChanged(null, EventArgs.Empty);
        }
    }

and this is my mainForm:

这是我的主要形式:

    public Int32 newSystemRec { get; set; }

    public MainForm()
    {
        InitializeComponent();           
        InitializeP2Trace();           
        dataLayer.InsertHandler(this.Handle);
        Args = Environment.GetCommandLineArgs();
        MethodInvoker mi = new MethodInvoker(EmsJump);
        this.dgComp.Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
        this.dgSchematic.Style.VerticalAlignment = C1.Win.C1TrueDBGrid.AlignVertEnum.Center;
        dgSchematic.Splits[0].ColumnCaptionHeight = 28;
        dgComp.Splits[0].ColumnCaptionHeight = 28;
        xmlPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "temp.xml");
        tsMarkup.Location = new Point(355, 0);
        if (Args != null)
            mi.Invoke();
    }

    private void wordSearch_SystemChanged(object sender, EventArgs e)
    {
        //How to get the value of newSystemRec???
        Int32 newSystemRec = 
        newSystemRec--;
        Int32 index = newSystemRec;
        switch (index)
        {
            case 1:
                systemMode = SystemMode.Hydraulic;
                cbSystem.SelectedIndex = 1;
                break;
            case 0:
            default:
                systemMode = SystemMode.Electrical;
                cbSystem.SelectedIndex = 0;
                break;
        }
        if (p2pMode == p2p.Trace)
            tabP2Trace.TabVisible = true;
    }

2 个解决方案

#1


0  

Your code doesn't show how the two forms are instantiated, but basically what you could do is exposing the value as a form's property and access it from your other form. Of course in order to do that there has to be a context in which both form instances are known so that one can access the other:

您的代码没有显示如何实例化这两个表单,但基本上您可以做的是将值作为表单的属性公开并从其他表单访问它。当然,为了做到这一点,必须有一个上下文,其中两个表单实例都是已知的,以便一个人可以访问另一个:

public int NewSystemRec
{
    get
    {
        // return value here
    }
}

#2


0  

You can create a property on WordSearch like :

您可以在WordSearch上创建属性,如:

private int newSystemRec;

And access it with a Property :

并使用属性访问它:

public int NewSystemRec
{
    get
    {
        this.newSystemRec;
    }
}

exemple :

例如:

MainForm mainForm = new mainForm()
someControlOnForm1.Text = mainForm.NewSystemRec;

You can also use constructor :

你也可以使用构造函数:

public MainForm(int newSystemRecFromWordSearch ) {
 int newSystemRec = id;
}

and use it on WordSearch

并在WordSearch上使用它

var form = new MainForm(newSystemRec);

Hope it helps !

希望能帮助到你 !

#1


0  

Your code doesn't show how the two forms are instantiated, but basically what you could do is exposing the value as a form's property and access it from your other form. Of course in order to do that there has to be a context in which both form instances are known so that one can access the other:

您的代码没有显示如何实例化这两个表单,但基本上您可以做的是将值作为表单的属性公开并从其他表单访问它。当然,为了做到这一点,必须有一个上下文,其中两个表单实例都是已知的,以便一个人可以访问另一个:

public int NewSystemRec
{
    get
    {
        // return value here
    }
}

#2


0  

You can create a property on WordSearch like :

您可以在WordSearch上创建属性,如:

private int newSystemRec;

And access it with a Property :

并使用属性访问它:

public int NewSystemRec
{
    get
    {
        this.newSystemRec;
    }
}

exemple :

例如:

MainForm mainForm = new mainForm()
someControlOnForm1.Text = mainForm.NewSystemRec;

You can also use constructor :

你也可以使用构造函数:

public MainForm(int newSystemRecFromWordSearch ) {
 int newSystemRec = id;
}

and use it on WordSearch

并在WordSearch上使用它

var form = new MainForm(newSystemRec);

Hope it helps !

希望能帮助到你 !