c# – How to call functions of the same object from different form? – Education Career Blog

I am using winform with c#.

I have formA and formB. formB is a smaller window that have a textbox and a button to save whatever is written in the textbox.

So Im in formB calling a save function from a class that was define in formA. How would I do it ?

Thanks,

J

,

Form A

If this object is in Form A like so:

// In Form A
MyThing thing = new MyThing();

then create a public property on your form to access it from anywhere else like so:

 // still in Form A

public void InvokeSave() {
    myThing.Save();
}

Form B

From inside Form B you can call the form A method like so:

// Inside Form B

void myButton_Click(..) {
    formAReference.InvokeSave();
}

You will have to keep a reference to the original form somewhere – that’s what I named formAReference


There are various ways to pass objects and make calls between winforms. For example, just google “passing objects between winforms” and choose a situation that seems right for your app. Remember, a winform is a .NET object, so the same concepts that apply to passing data and messages between pure .NET objects also apply to winforms.

Tutorial/Example at Code Project

Summary

You have successfully accessed your object composited into Form A through a public method on Form A, from over inside Form B. You can access that method from anywhere.

,

Send a reference to the class holding the function to formb. something like new FormB(ReferenceClass Reference)

,

create object to formA class. If the method declared is not private you can access.

formA aObj = new formA(); 
aObj.SaveMethod(); // consider `SaveMethod()` is a public in `formA`

,

So far I have seen people suggesting the use of static fields and passing around references to your main form. There is really no reason to do it this way. Passing a reference to your mainform may be fine for a small, throwaway app, but for a ‘real’ application it can cause maintenance problems.

This is a good place to use events. Expose an event in your subform class called SaveData or whatever. Your mainform class can handle this even when it creates the child form and update the UI as needed from there. No static data, no unneeded breakage of encapsulation.

An example:

class SaveDataEventArgs : EventArgs
{
    public readonly string Data;
    public SaveDataEventArgs( string data )
    {
        Data = data;
    }
}

class ChildForm
{
    public event EventHandler<SaveDataEventArgs> SaveData

    void button_Click( ... )
    {
        OnSaveData( new SaveDataEventArgs( textbox1.Text );
    }

    protected virtual void OnSaveData( SaveDataEventArgs e )
    {
        EventHandler<SaveDataEventArgs> del = SaveData;
        if( del != null )
        {
            SaveData( this, e );
        }
    }
}

class Form1 : Form
{
    void ShowChildForm( )
    {
        using( ChildForm frm = new ChildForm() )
        {
            frm.SaveData += frm_SaveData;
            frm.ShowDialog();
        }
    }

    void frm_SaveData( object sender, SaveDataEventArgs e )
    {
        label1 = e.Data;  // data from the child form, do what you need to do with it
    }
}

,

UPDATE:
you can try following code.
Code for FormA:
(write this code in any method of FormA eg.any button on FormA you can write it in click event of that button)

            StringBuilder bankName = new StringBuilder();
            FormB objFormBsaveBank = new FormB(bankName);
            objFormBsaveBank.ShowDialog();

           // Text in textbox of the FormB can be access from here and can be stored in any variable
           string bankname = bankName.ToString();

Code for FormB:

public class FormB : Form
   {

    StringBuilder bankName;

    // Constructore for FormB
    public FormB(StringBuilder bankName)
    {
        InitializeComponent();
        this.bankName = bankName;
    }

    // click event for button which is on the FormB
    private void button1_Click(object sender, EventArgs e)
    {
        // name of textbox which is on FormB is "BankNameTextBox"
        this.bankName.Append(BankNameTextBox.Text);           
        this.Close();
    }
}

,

Hay John…, If I got you right.. than why not just create a new instance of that class (defined in formA) inside your formB and then call the save method..?

Did I got you right?

,

Hello john……..
Most easiest way is bellow

string returnValue = Application.OpenForms"OpenFormName".Controls"ControlName".Text 

,

why not to use static fields? it is easy way to transfer message. When you click to the save button it will save it to static class and after that you’ll read it from formB

Leave a Comment