views:

117

answers:

3

The case is this, I have two different forms from the same solution/project. What I need to do is to extract the value of a label in Form A and load it into Form B. As much as possible, I am staying away from using this code since it will only conflict my whole program:

FormB myForm = new FromB(label.Text);
myForm.ShowDialog();

What I am trying right now is a class with a property of get and set for the value I wanted to pass. However, whenever I access the get method from FormB, it returns a blank value.

I hope somebody can help me with this. Any other ways to do this is extremely appreciated. :)

    public class Miscellaneous
    {
       string my_id;

       public void SetID(string id)
       {
           my_id = id;
       }

       public string GetID()
       {
           return my_id;
       }
     }
A: 

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
spinon
Can you show me how I can create the singleton class? I'm kinda new to C#'s extensive features. Thanks. :)
Kim Rivera
If you just google singleton pattern you will get a number of examples. But I will see if I can find one real quick to add to my answer.
spinon
Thanks for this. I'm trying to implement this one to see which is better. :) thanks
Kim Rivera
A: 

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.

Filip Ekberg
I tried using the first one you posted and it worked perfectly although I'm still kinda confused with it. Thanks a lot :) I'm trying the second one you gave.
Kim Rivera
It is just "refactored" code, this: `var child = new TestForm {YourText = someTextBox.Text};` is the same as `TestForm child = new TestForm(); child.YourText = someTextBox.Text;` but the first one looks better. Remember that the Constructor is called before you actually set any properties, step through your code with the debugger and see what it does.
Filip Ekberg
You can just do `set { someLabel.Text = value; }` directly. No need to wrap it like that.
Jaroslav Jandek
The second one gives me an error saying: Error 1 The name 'UpdateValues' does not exist in the current context.
Kim Rivera
Okay now I'm actually picking it up. Thanks for the help. I really appreciate it.
Kim Rivera
@Kim, no problem! @Jeroslav, what if you want to use the local variable somewhere else? I rahter not access the label directly from the getter/setter.
Filip Ekberg
@Jeroslav, Filip is right since I'm accessing the variable from somewhere else. Meaning someLabel is in another form. Anyway, no problem with this. Thank you guys for helping out.
Kim Rivera
A: 

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.

Jaroslav Jandek