Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

using two forms

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RobcPettit@yahoo.co.uk - 11 Jul 2007 18:25 GMT
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
zacks@construction-imaging.com - 11 Jul 2007 19:04 GMT
On Jul 11, 1:25 pm, "RobcPet...@yahoo.co.uk" <RobcPet...@yahoo.co.uk>
wrote:
> 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
[quoted text clipped - 3 lines]
> 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.
RobcPettit@yahoo.co.uk - 11 Jul 2007 19:19 GMT
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
Peter Bromberg [C# MVP] - 11 Jul 2007 19:54 GMT
Rob,
Here is a short article that illustrates how to do inter-form communication
with events/delegates:

http://www.eggheadcafe.com/articles/20040229.asp

Peter

Site:  http://www.eggheadcafe.com
UnBlog:  http://petesbloggerama.blogspot.com
BlogMetaFinder(BETA):    http://www.blogmetafinder.com

> 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
zacks@construction-imaging.com - 11 Jul 2007 20:06 GMT
On Jul 11, 2:19 pm, "RobcPet...@yahoo.co.uk" <RobcPet...@yahoo.co.uk>
wrote:
> 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
zacks@construction-imaging.com - 11 Jul 2007 20:09 GMT
On Jul 11, 3:06 pm, z...@construction-imaging.com wrote:
> On Jul 11, 2:19 pm, "RobcPet...@yahoo.co.uk" <RobcPet...@yahoo.co.uk>
> wrote:
[quoted text clipped - 18 lines]
>     End Set
> End Property

I just realized I posted VB code in a C# group. Sorry. Do you need me
to translate?
Peter Bradley - 11 Jul 2007 20:11 GMT
Ysgrifennodd RobcPettit@yahoo.co.uk:
> 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
Ian Semmel - 11 Jul 2007 20:12 GMT
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

> 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
[quoted text clipped - 3 lines]
> etc, all works ok.
> regards robert
RobcPettit@yahoo.co.uk - 12 Jul 2007 20:39 GMT
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
Ian Semmel - 14 Jul 2007 22:40 GMT
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: RobcPettit@yahoo.co.uk [mailto:RobcPettit@yahoo.co.uk]
[quoted text clipped - 11 lines]
> final nodes. Any way thankyou all again.
> Regards Robert
Bruce Wood - 15 Jul 2007 20:19 GMT
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.
>
[quoted text clipped - 22 lines]
> > using a treeview in form2 and secong time around it doesnt like the
> > final nodes. Any way thankyou all again.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.