using two forms

R

RobcPettit

Hi, Im using two forms in the same application. Main form opens, click
menu for secend form. This is a log on form, used to get three bits of
info. Up to this point Im ok. I then want to send this info back to
form one for it to process. My question is how do I call the relevent
code on form1. If I do all the coding in one form only, ie logging on
etc, all works ok.
regards robert
 
Z

zacks

Hi, Im using two forms in the same application. Main form opens, click
menu for secend form. This is a log on form, used to get three bits of
info. Up to this point Im ok. I then want to send this info back to
form one for it to process. My question is how do I call the relevent
code on form1. If I do all the coding in one form only, ie logging on
etc, all works ok.
regards robert

You contradict yourself. You say that form2 needs to send data back to
form1. And then you ask how to call the relevant code in form1. So,
what do you want to do? Pass the data back from form2 to form1 or tell
form1 from form2 to do something?

Passing the data from form2 to form1 is simple. Just declare
properties for these values in form2 and when form2 closes, form1 will
have acces to the values in form2's properties.

Invoking a method in form1 from form2 is also pretty simple, but I
suspect you really need to just pass the data back to form1.
 
R

RobcPettit

Thanks for your reply. After re reading my post, your right I have
contradicted myself. All I need to do is pass data from forw 2 to form
1. Do I declare the values in the normal way, ie public int xxxx; etc.
Regards Robert
 
Z

zacks

Thanks for your reply. After re reading my post, your right I have
contradicted myself. All I need to do is pass data from forw 2 to form
1. Do I declare the values in the normal way, ie public int xxxx; etc.
Regards Robert

I would define them in the "standard" way to define class properties.
Declare a private variable to hold the local value. Then declare a
public (or friend) property that links to the private variable. As is:

Private _myvalue as Boolean

Public Property myvalue as Boolean
Get
Return _myvalue
End Get
Set(ByVal value as Boolean)
_myvalue = value
End Set
End Property
 
Z

zacks

I would define them in the "standard" way to define class properties.
Declare a private variable to hold the local value. Then declare a
public (or friend) property that links to the private variable. As is:

Private _myvalue as Boolean

Public Property myvalue as Boolean
Get
Return _myvalue
End Get
Set(ByVal value as Boolean)
_myvalue = value
End Set
End Property

I just realized I posted VB code in a C# group. Sorry. Do you need me
to translate?
 
P

Peter Bradley

Ysgrifennodd (e-mail address removed):
Thanks for your reply. After re reading my post, your right I have
contradicted myself. All I need to do is pass data from forw 2 to form
1. Do I declare the values in the normal way, ie public int xxxx; etc.
Regards Robert

One way of doing it would be to create a constructor for form2 that
takes an instance of a Form (or your subclass - let's say MyForm1) as a
parameter. Let's say that your second form is an instance of the
MyForm2 class.

In Form1, you create a new instance of Form2 like this:

MyForm2 myForm2 = new MyForm2(this);

You can then go on and make the new form visible and show it.

In the MyForm2 constructor, you squirrel away the reference to the first
form:

....
private MyForm1 myForm1;
....

public MyForm2(MyForm1 FirstForm)
{
myForm1 = FirstForm;
}

Let's assume that you're passing a uid and pwd back to MyForm1. To do
this, we'll assume you have two instance variables in your MyForm1
definition:

string uid;
string pwd;

Let's also assume you have a public method in your MyForm1 class called
SetUidAndPwd(string, string). Given that, and given that you've saved a
reference to your first form in your second one, you can, somewhere in
the code within your second form, do something like:

myForm1.SetUidAndPwd(uid, pwd);

Should do it. There are bound to be other ways of doing this as well.
I'm not claiming this is the best way, by any means. Perhaps a simpler
way would be to provide a method in your MyForm2 instance to return the
uid and pwd, but you would have to Hide() your form and not Close() it,
in order to call it. Of course you could close it after you've made the
call.

HTH


Peter
 
I

Ian Semmel

Look at ShowDialog for this kind of processing

Form2 doesn't send data back to Form1.
Form2 exposes public properties which allow Form1 to get the data from Form2
after the user has finished with it.

Your code in Form1 would look something like

Form2 form2 = new Form2 ();
DialogResult rc = form2.ShowDialog();
if ( rc == DialogResult.OK)
string user = form2.User;

and Form2 has a public property
public string User
{
get { return userControl.Text; }
}

etc
 
R

RobcPettit

Thankyou all for your replys. Plenty to read up on. Id got as far as
ian suggestion, which works. Now Im reading up to make sure I
understand whats happening for future ref. One issue Ive got at the
momment is when I open and use the form2, then close it, details go
back to form one. When I open it again to get details, it crashes. Im
using a treeview in form2 and secong time around it doesnt like the
final nodes. Any way thankyou all again.
Regards Robert
 
I

Ian Semmel

When you use ShowDialog, Close() doesn't destroy the form.

eg you can do

form2 = new Form2();
rc = form2.ShowDialog();
form2.Close();

myObject = form2.GetMyResult();

form2.Dispose();
 
B

Bruce Wood

Or (a bit nicer):

using (Form2 form2 = new Form2())
{
if (form2.ShowDialog() == DialogResult.OK)
{
myObject = form2.MyResult;
}
}

BTW there's no need to call Close... ShowDialog hides the form
automatically.

When you use ShowDialog, Close() doesn't destroy the form.

eg you can do

form2 = new Form2();
rc = form2.ShowDialog();
form2.Close();

myObject = form2.GetMyResult();

form2.Dispose();


-----Original Message-----
From: (e-mail address removed) [mailto:[email protected]]
Posted At: Friday, 13 July 2007 5:39 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: using two forms
Subject: Re: using two forms
Thankyou all for your replys. Plenty to read up on. Id got as far as
ian suggestion, which works. Now Im reading up to make sure I
understand whats happening for future ref. One issue Ive got at the
momment is when I open and use the form2, then close it, details go
back to form one. When I open it again to get details, it crashes. Im
using a treeview in form2 and secong time around it doesnt like the
final nodes. Any way thankyou all again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top