tags:

views:

85

answers:

8

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

A: 

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

Fredrik Leijon
A: 

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`
anishmarokey
+1  A: 

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.

John K
I tried public partial class FormB(thing t) : Form Gave me error, sorry im new to this
John
I added a tutorial link that looks like it's relatively easy to try. Just follow through its steps and you will likely gain insight. Oddly enough, out of all the resources on the Internet, it took me some time to find something that looked like a good tutorial. I hope this is it.
John K
Nevermind, its working, THANKS
John
A: 

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

RyuuGan
Because there are much better ways to communicate between classes than to effectively make everything global.
Ed Swangren
If you have multiple instances of a Form then you're effectively screwed by going static; unless you put some wild handling in place, at which point it would likely be easier just to use instance fields, properties and/or methods.
John K
@Jon K, Sorry I am not agree with you, that use of static fields create problem against multiple instances of forms.
mahesh
@Jon K, It depend how you utilize static fields between multiple forms?. since long I have utilized statice fields between multiple forms. I don't face any kind of problem on it.
mahesh
A: 

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();
    }
}
Pritesh
A: 

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
    }
}
Ed Swangren
What about if FormA is not initiated since OP has not written in the question that he is initiating FormB from FormA. Than Also you can not use events.
saurabh
+1 Good reasoning I prefer the use of events but it sounds like this user is just trying to get off the ground with winforms stuff.
John K
@saurabh: Huh? @John K: Yeah, you may be right. Passing a reference to the mainform is not the end of the world, but it can get nasty later on and it induces tight coupling between controls. Not bad to get used to using events from the start.
Ed Swangren
@Ed. Agreed, yours is the best answer for the reasons you stated.
John K
@Ed, can you explain in short of maintenance problems against using of static fields?. e.g.
mahesh
@Ed, How kind of maintanance problem you faced against using of static fields?
mahesh
http://c2.com/cgi/wiki?GlobalVariablesAreBad
Ed Swangren
@Ed, Lots thanks to provide me guidance, I had read it and found that there is nothing mention on it that global variable are not to use.
mahesh
@Ed, as per my point of view he is just suggests that utilize it be careful. there is advantages and disadvantages. Yes it is also part of other type variable. there is also advantages and disadvantages.
mahesh
@Ed, what we do with it?. we have to stop utilize it or utilize it with better manner?
mahesh
@Ed, you seem to have made fan. :)
John K
@mahesh: Uh, what? The title of the article is "Global Variables are Bad" and it goes no to list multiple reasons why they should be avoided if possible. What were you reading?
Ed Swangren
@John K, Thanks for calling me made, I wants to invite both of you in my question forum which will going to ask with in a hour by me please come. and please don't be down voted it's for discussion which will pour out the knowledge.
mahesh
@John K and @Ed, and it is useful for others also like my type made people. so I welcome both of you.
mahesh
Please be note that The question discussion on "SO"
mahesh
A: 

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?

Rami Shareef
A: 

Hello john........ Most easiest way is bellow

string returnValue = Application.OpenForms["OpenFormName"].Controls["ControlName"].Text 
Pritesh