0

I am passing data between 2 windows forms in c#. Form1 is the main form, whose textbox will receive the text passed to it from form2_textbox & display it in its textbox (form1_textbox).

First, form1 opens, with an empty textbox and a button, on clicking on the form1_button, form2 opens. In Form2, I entered a text in form2_textbox & then clicked the button (form2_button).ON click event of this button, it will send the text to form1's textbox & form1 will come in focus with its empty form1_textbox with a text received from form2.

I am using properties to implement this task. FORM2.CS

public partial class Form2 : Form
{
    //declare event in form 2
    public event EventHandler SomeTextInSomeFormChanged;

    public Form2()
    {
        InitializeComponent();

    }
    public string get_text_for_Form1
    {
        get { return form2_textBox1.Text; }
    }

    //On the button click event of form2, the text from form2 will be send to form1:

    public void button1_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1();
        f1.set_text_in_Form1 = get_text_for_Form1;

    //if subscribers exists
    if(SomeTextInSomeFormChanged != null)
    {
        SomeTextInSomeFormChanged(this, null);
    }

    }

}

FORM1.CS

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public string set_text_in_Form1
        {
            set { form1_textBox1.Text = value; }
        }

        private void form1_button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.Show();
            f2.SomeTextInSomeFormChanged +=new EventHandler(f2_SomeTextInSomeFormChanged);  
        }

        //in form 1 subcribe to event
        Form2 form2 = new Form2();

        public void f2_SomeTextInSomeFormChanged(object sender, EventArgs e)
        {
            this.Focus();

        }
    }

Now, in this case I have to again SHOW the form1 in order to automatically get the text in its textbox from form2, but I want that as I click the button on form2, the text is sent from Form2 to Form1, & the form1 comes in focus, with its textbox containing the text received from Form2.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
sqlchild
  • 8,754
  • 28
  • 105
  • 167
  • 3
    @sqlchild , don't do this way , use events and delegates , thats the right way of doing it , don't expose property's through forms – kobe Feb 23 '11 at 07:52
  • possible duplicate of [passing data between two forms using properties](http://stackoverflow.com/questions/5087934/passing-data-between-two-forms-using-properties) – Cody Gray - on strike Feb 23 '11 at 07:57
  • 1
    You already posted this question once. Please edit your original question if you'd like to add additional information, rather than posting a new one. – Cody Gray - on strike Feb 23 '11 at 07:57
  • your coding style is very-very ugly.. you must conform to common .net coding convension http://msdn.microsoft.com/en-us/library/ms229045.aspx if you think someone would ever work with your code. – Alexander Beletsky Feb 23 '11 at 08:03
  • @gov, whats the harm in using properties? – sqlchild Feb 23 '11 at 08:37
  • @gov, i want to do with properties , can anybody guide me with the code – sqlchild Feb 23 '11 at 08:37
  • Is form 2 a simple form of a single textbox entry and like an "Ok" button to accept the value or not which closes the form? Is that its sole purpose? – DRapp Feb 23 '11 at 14:30

1 Answers1

4

I know this is a (really) old question, but hell..

The "best" solution for this is to have a "data" class that will handle holding whatever you need to pass across:

class Session
{
    public Session()
    {
        //Init Vars here
    }
    public string foo {get; set;}
}

Then have a background "controller" class that can handle calling, showing/hiding forms (etc..)

class Controller
{
    private Session m_Session;
    public Controller(Session session, Form firstform)
    {
        m_Session = session;
        ShowForm(firstform);
    }

    private ShowForm(Form firstform)
    {
        /*Yes, I'm implying that you also keep a reference to "Session"
         * within each form, on construction.*/
        Form currentform = firstform(m_Session);
        DialogResult currentresult = currentform.ShowDialog();
        //....

        //Logic+Loops that handle calling forms and their behaviours..
    }
}

And obviously, in your form, you can have a very simple click listener that's like..

//...
    m_Session.foo = textbox.Text;
    this.DialogResult = DialogResult.OK;
    this.Close();
//...

Then, when you have your magical amazing forms, they can pass things between each other using the session object. If you want to have concurrent access, you might want to set up a mutex or semaphore depending on your needs (which you can also store a reference to within Session). There's also no reason why you cannot have similar controller logic within a parent dialog to control its children (and don't forget, DialogResult is great for simple forms)

Izzy
  • 1,764
  • 1
  • 17
  • 31