Click to See Complete Forum and Search --> : Form1 and Form2 Scope


demise
September 16th, 2009, 04:49 AM
I did a quick search and found nothing on this subject, so I hope I don't repost or anything.

Scenario:

I have Form1, and Form2. I want data from text boxes on Form2 to be placed inside a Class node (which Form1 has declared).

I haven't used GUI's in C# much, so I dont know the best way to go about this.

In Form1 I have declared like such:

private HolderSLL List;
private Node _head;
private Node _current;

and I have been working with the _head and _current etc through Form1 (firstly, is this ok?) Secondly, I want when I am in Form2 and want to Hit a button and essentially add the text in the boxes into a new node. (But this is out of scope). I was was thinking of doing a return with a string of arrays, but I get an error 'Wrong type' Yet.... I am still unsure if this is the correct way anyway. All advice is hugely appreciated. Thank you very much.


P.S - Here is what I was attempting to do (Wrong or Right, I don't know.)


private String[] btnAccept_Click(object sender, EventArgs e)
{
String[] Info = new String[4];
Info[0] = txt1.Text;
Info[1] = txt2.Text;
Info[2] = txt3.Text;
Info[3] = txt4.Text;
Info[4] = txt5.Text;
return Info;
}

dannystommen
September 16th, 2009, 05:48 AM
You need to use events.

Declare a event in form2, when you show form2, form1 should subscribe to this event. In the btnAccept_Click event, you should trigger the event and do something with the data in form1.

When you are new to events, do some googling and you will find lots of good examples

Ajay Vijay
September 16th, 2009, 12:15 PM
Set the reference to other form, through the use of constructor or by Set calls:
class Form1
{
Form2 form;

public void Form1(Form2 frm2)
{
form=frm2;
}
...
...
}Then use 'form'!

demise
September 17th, 2009, 03:49 AM
Thanks guys! I will give these a go when I install VS and my project onto Windows 7 again.

Cheers!

demise
September 20th, 2009, 01:05 PM
Hey, sorry to be a pain. I managed to get around to giving this a go. I seem to be doing something wrong though.. Not quite sure what, as I've never touched Forms before.

Same problem as above really..

I have a frmMain and a frmAdd.

I want frmMain to update something (run code) if an event in frmAdd has been triggered (Button Press).

I cant get my head around it and I tried the above mentioned method by the posters, but.. I must be doing it wrong. :/

BigEd781
September 20th, 2009, 04:35 PM
class frmMain : Form
{
void ShowAddForm( )
{
using ( frmAdd form = new frmAdd( ) )
{
form.SomeEvent += form_SomeEvent;
form.ShowDialog( );
}
}

void form_SomeEvent;( object sender, EventArgs e )
{
// handle event from your add form here
}
}

Arjay
September 21st, 2009, 01:40 AM
If you don't need to use the data until after you return from Form2, then you don't need to use an event.

Just expose the data from Form2 as a public property and get the data from the property after you return from form2.ShowDialog().

An event is only required if you need to "send" the data to Form1 while form2 is opened (providing Form2 is a modal dialog).

You may also need an event if you open Form2 as a modeless dialog.