Passing Values from Form A to Form B and vice versa!!

Hi all,

I'm using Visual C# Express 2005. I have two forms, form A and form B. I can pass values from form A to form B but I can't get the values from form B to form A. Any help would be appreaciated.

Thanks in advance!!




Answer this question

Passing Values from Form A to Form B and vice versa!!

  • Bluehunter

    This question has been asked a couple of times on the MSDN forumns. I would try and search first before asking. Most likely you will be able to find the answer. Here is one "Assigning values from one form to the main form"
  • Hemant Kumar

    no problem :)
  • cwlaualex

    Hi all,

    Thanks for all your help. I used the event handler and it work out perfectly for me, now I can pass variables from Form A to Form B and vice versa. It's easy than I thought, especially if you use event handler. Thx for the help guys.

    Jason!!



  • new2SQLRpt

    Check it out here.. http://cyberiafreak.spaces.live.com/blog/cns!410E76C6390C7090!582.entry and let me know if any other way of passing values between forms.

    Mahesh



  • Vyanki

    I would assume that your other form isn't attaching to the event handler and that is why you are getting the null reference error. You should always put

    If( sendParams != null )
    sendParams( ... )

    In your solution you will never get to the method "TransCheckList_sendParams( string info ) because you won't ever call it. You are calling the event from inside the event's handler. I would read up on Events/Delegate to get a better understanding. They can be pretty powerful but it is also pretty easy to use them wrong.


  • JGttttt

    My answer is different from the one in the above link. So I will try to help..

    You should subscribe ( newForm.sendParams += new Form2.parameterPassing(newForm_sendParams); )
    to the event in the MainForm (Form1), where you call form2, not in form2;
    in your try you had subscribed to the event in form that you describe it. So it doesn't work.
    also for the frist exception; If you never subscribe it to anywhere then you get a null reference exception.


  • BJ Custard

    Hi all,

    Thanks for all the reply, I will work on it and post the update on how it goes.

    Thanks guys!!

    Jason



  • ZopoStyle

    Mike his idea is the solution for formA to formB.

    For vice versa you need a design pattern called the Observer Pattern:

    You put this code on formB(The child i suppose)

    #region Observerable

    private ArrayList observers = new ArrayList();

    public void AddObserver(IObserver observer)

    {

    if (!observers.Contains(observer))

    observers.Add(observer);

    }

    public void RemoveObserver(IObserver observer)

    {

    observers.Remove(observer);

    }

    public void Notify(object args)

    {

    foreach (IObserver observer in observers)

    {

    observer.Update(this, args);

    }

    }

    #endregion

    On formA you do

    #region Observer

    public void Update(object subject, object args)

    {

    //Anything you want to do with the args

    }

    #endregion

    So when you have this you need to declare and open formB on the formA by

    FormB frmB = new frmB;

    frmB.AddObserver(this);

    frmB.ShowDialog();

    frmB.Dispose();

    When you do on the FormB .

    This.Notify(//anything you want to send to FormA)

    It will trigger the update event on the formA. There you can use the value.

    Grtz

    Annihil8




  • perf101

    S M N wrote:

    There is a more simple way;

    You can create a custom action event in the child form and subscribe to that event in the parent form using delegates.

    Example ;

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void openNewForm()

    {

    Form2 newForm = new Form2();

    newForm.sendParams += new Form2.parameterPassing(newForm_sendParams);

    }

    void newForm_sendParams(string info)

    {

    MessageBox.Show(info);

    }

    }

    public Form2()

    {

    InitializeComponent();

    }

    public delegate void parameterPassing(string info);

    public event parameterPassing sendParams;

    private void methodYouWantToPassData()

    {

    sendParams("any string");

    }

    Hi,

    I try to test out your code, when I include this code (see below) in my second form, I got this error (see bottom):

    public delegate void parameterPassing(string info);

    public event parameterPassing sendParams;

    private void methodYouWantToPassData()

    {

    sendParams("any string");

    }

    Error:

    Object reference not set to an instance of an object.

    ---------------------------------

    I also tried this code:

    public delegate void parameterPassing(string info);

    public event parameterPassing sendParams;

    public void methodYouWantToPassData()

    {

    this.sendParams += new parameterPassing(TransCheckList_sendParams);

    }

    void TransCheckList_sendParams(string info)

    {

    this.sendParams("test");

    }

    it doesn't give me any errors but it doesn't pass the values, it didn't do anything. If you can, can you help me!

    thanks

    Jason



  • Eisa

    Hello

    Try to create a container class implementing the Singleton Design Pattern, which stores the values passed between the forms.

    Hope this might help.



  • Lawrence 007

    Are the values public or private

    Needless to say they should all be public and then you can call it simply

    frmThisForm.thisObject;

    Good Luck
    Mike


  • Unpro

    Indeed that is also possible.

    The difference between the observer and the singleton is that when a value gets updated, the observer will immediatly change it on formA. It is very handy when you want to show a messagebox or progressbar with data of an other class.

    With the singleton the data will be available in all your application. but you need to get the data manually.

    Grtz

    Annihil8


  • robydx

    There is a more simple way;

    You can create a custom action event in the child form and subscribe to that event in the parent form using delegates.

    Example ;

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void openNewForm()

    {

    Form2 newForm = new Form2();

    newForm.sendParams += new Form2.parameterPassing(newForm_sendParams);

    }

    void newForm_sendParams(string info)

    {

    MessageBox.Show(info);

    }

    }

    public Form2()

    {

    InitializeComponent();

    }

    public delegate void parameterPassing(string info);

    public event parameterPassing sendParams;

    private void methodYouWantToPassData()

    {

    sendParams("any string");

    }


  • Passing Values from Form A to Form B and vice versa!!