Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 17th, 2008, 08:17 AM
Authorized User
 
Join Date: Feb 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Passing variables from 1 form to another.

hi Friends,
I want to know how to pass variables from one form to another.

Thank You in advance.
Any help is welcomed

JAININ
__________________
JAININ
 
Old February 17th, 2008, 06:15 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

The best way is to define a public property on one form:

public string TheProperty
{
  get { return _myProperty; }
}

Then you can call it from the other form, provided you have a reference to the other form:

MyForm form = (MyForm)formReference;
Console.WriteLine(form.TheProperty);

If this doesn't help then you'll have to be more specific in your requirements.


/- Sam Judson : Wrox Technical Editor -/
 
Old February 18th, 2008, 04:32 AM
Authorized User
 
Join Date: Feb 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think I got it. Thank u for your help.

JAININ
 
Old February 18th, 2008, 07:45 AM
Authorized User
 
Join Date: Feb 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I m not getting it,
Can you help me?
Actually i have an Mdi form and from it different form opens,
So i want that only 1 instance of any form is open at a given time.For that I m using a boolean variable in Mdi form. Now i want to access that boolean variable in another form which is a child form of Mdi form.
Can you solve my problem?

Thank You

JAININ
 
Old February 18th, 2008, 08:00 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Inside your MdiParent you will have a property, something like this:

public bool IsFormOpen
{
  get { return isFormOpenVariable; }
}

In your child you will do the following:

MyMdiParent parent = (MyMdiParent)this.MdiParent;
if( !parent.IsFormOpen )
{
  // whatever
}


/- Sam Judson : Wrox Technical Editor -/
 
Old February 18th, 2008, 08:29 AM
Authorized User
 
Join Date: Feb 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I m still not getting it,
I want to change the value of the boolean variable in the childform and return that changed value to the MdiParent form when the child form closes,
but i get the following error.

Error 1 Property or indexer 'WindowsApplication1.MDIopeningpage.frmload' cannot be assigned to -- it is read only

Thank You.

JAININ
 
Old February 18th, 2008, 08:38 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Right, you didn't say you wanted to 'change' the value, just 'access' it, so what I wrote above is a read only property.

public bool MyProperty
{
  get
  {
    return true;
  }
  set
  {
    // do something with 'value' here.
    Console.WriteLine(value);
  }
}

/- Sam Judson : Wrox Technical Editor -/
 
Old February 18th, 2008, 09:02 AM
Authorized User
 
Join Date: Feb 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I m still not getting it.

I want to change the value of the boolean variable of the Mdi form on the child form and return the changed value of the boolean variable back to the Mdi form.

Thank You for helping me
 
Old February 18th, 2008, 09:39 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I'm not sure what you are having trouble with. My previous example above shows you how to write a property setter, so you can write the the property - you then store the 'value' in whatever internal variable you want to.

/- Sam Judson : Wrox Technical Editor -/
 
Old February 18th, 2008, 06:38 PM
Authorized User
 
Join Date: Feb 2008
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The trouble is:: I have a MDI parent form and a child form for Suppplier details. Ok. Now when i first open the supplier form it opens normally and when I try yo open it again at that time it says that the form is already open.
For this i have used a boolean variable whose value is get to true once the form is opened. and that boolean variable is declared in Mdi Parent form. So now i want that when i close the Supplier form the boolean variable value should again be false.

But when I close the supplier form and try to open it again at that time also I get the message that your form is already opened. i.e.the value of the boolean variable is true only.

I want to get rid of this problem

Thank you in advance.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing Variables CMOS Classic ASP Basics 2 January 22nd, 2006 07:38 PM
Passing variables karlirvin Beginning PHP 3 December 9th, 2005 04:37 PM
Passing variables acko ASP.NET 1.x and 2.0 Application Design 1 December 23rd, 2003 09:40 AM
Passing variables jesseleon Access VBA 4 October 24th, 2003 01:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.