Passing values between two windows forms

…衆ロ難τιáo~ 提交于 2019-11-29 15:32:39

You could do something like this:

Child form

public string YourText { get; set; }

public TestForm()
{
    InitializeComponent();
}

public void UpdateValues()
{
    someLabel.Text = YourText;
}

Initiate it

var child = new TestForm {YourText = someTextBox.Text};

child.UpdateValues();

child.ShowDialog();

With this approach you don't have to change the Constructor, you could also add another constructor.

The reason for them being empty is that the properties are set after the constructor, you could Also do someting like this to add a bit of logic to your getters and setters, However, I would consider not affecting UI on properties!

private string _yourText = string.Empty;
public string YourText
{
    get
    {
        return _yourText;
    }
    set 
    { 
        _yourText = value;
        UpdateValues(); 
    }
}

In this case, the UI will be updated automaticly when you set the property.

You can use a static variable/method to hold/pass the value of a control (when it gets changed).

You can use form reference or control reference to get and pass values directly.

You can use custom event for that (notifying the code that subscribed).

btw. FormB myForm = new FromB(label.Text); did not work because you are passing by value and the value was empty at the moment of creation of FormB. FormB myForm = new FromB(label); would have worked.

Well one approach to take is to create a singleton class in your application. When you form b loads or the label changes you update the singleton with the value. Then when form a needs the value it can just get the instance of the singleton within your application and it will have that value.

There are probably cleaner ways to do it but just thinking of an easy way to pass information back and forth and store any information needed for both forms.

EDIT: Here is an example of a singleton that I pulled from here:

http://www.yoda.arachsys.com/csharp/singleton.html

public sealed class Singleton
{
    static readonly Singleton instance=new Singleton();

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Singleton()
    {
    }

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return instance;
        }
    }
}

Now all you need to do is put this class in a namespace that is accessible to both forms and then you can call the Instance property of this class and then reference your values. You can add properties to it as well for whatever you want to share. When you want to retrieve those values you would call it like this:

Singleton.Instance.YourProperty
((Form2)Application.OpenForms["Form2"]).textBox1.Text = "My Message";

declare public property varible in second form

Public property somevariable as sometype

and access it in first form using instance

Dim obj as New form2()
obj .somevariable ="value"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!