PC Review


Reply
Thread Tools Rate Thread

Data between forms

 
 
Ari A
Guest
Posts: n/a
 
      5th Dec 2003
I have created two forms in VisualBasic.net.
When the user hit a button on the FormB must a message
box open showing data from a textbox on FormA. Both forms
are open at same time.
I have done this by using global variant but how can I do
it by writing a line of code in FormB?
Thanks Ari.

 
Reply With Quote
 
 
 
 
Michael Lang
Guest
Posts: n/a
 
      5th Dec 2003
"Ari A" <(E-Mail Removed)> wrote in news:04f001c3bb3e
$870b5860$(E-Mail Removed):

> I have created two forms in VisualBasic.net.
> When the user hit a button on the FormB must a message
> box open showing data from a textbox on FormA. Both forms
> are open at same time.
> I have done this by using global variant but how can I do
> it by writing a line of code in FormB?


VB6 always created an implicit instance of every form you defined.

However, there is no implicit instance of a form in a .NET application.
In order for an instance of formB to interact with an instance of formA,
it must explicitly be given to that form.

The first way to do this is by making an explicit instance of each form
in a global location such as you have done

The second way is to have an internal variable in formB to a formA
instance. Then you need to tell the formB instance which formA instance
to store in it's internal variable. This could be done either by
requiring a formA instance in the formB constructor, or by having a
method taking a formA type, or a property of type formA.

An application always needs an entrance point. This is why there is a
"Main" method created in the initial form of a windows forms application.
This is what it looks like in C# (similar to VB.NET, but i don;t have
VB.NET)

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

I usually remove this from the form class and create a new class called
"Startup" or something similar and move the Main method there...

public class Startup
{
static void Main(){...}
}

In this class any variable you want to be "global" just define it as
"static" in this Startup class. Technically you could declare a static
variable anywhere in your application and it behaves like a global
variable. However, it makes it easy to manage your globals if they are
all in one place. Note: VB has a different keyword for "static", and I'm
not sure what it is. Maybe someone else can tell you that?

public class Startup
{
public static formA MyFormAInstance;
...
}

<or>

public class Startup
{
private static formA myFormA;

public static formA MyFormA
{
get{return myFormA;}
}
static void Main()
{ //instantiate globals at application start
myFormA = new formA();
}
}

Now to use this from anywhere else in your application, such as formB...

public class formB
{
public void MyMethod()
{
MessageBox.Show("Form A's textbox value = "
+ Startup.MyFormA.TextBox1.Text);
}
}

Let me know if you want an example of requiring a formA instance in the
formB constructor, or one of the other ways I mentioned.

Michael Lang
All code was typed on the fly. No warranties...

 
Reply With Quote
 
Michael Lang
Guest
Posts: n/a
 
      5th Dec 2003
Michael Lang <(E-Mail Removed)> wrote in
news:Xns9448718FD340Elang1474icqmailcom@207.46.248.16:

> Let me know if you want an example of requiring a formA instance in
> the formB constructor, or one of the other ways I mentioned.


I just saw another thread that shows the constructor option...

Group: (this one): microsoft.public.dotnet.general
Title: "Instance name of the Main Form of an application
Poster (of answer): Henke
Date: Dec 5, 2003, 1:25am
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regarding Forms and Forms representing Related Table Data magmike Microsoft Access Forms 7 30th Oct 2007 11:24 PM
Excel Data Entry Forms (Data, Worksheet) Microsoft Excel Discussion 3 28th Jun 2007 11:02 PM
SQL Data Source, refresh and access data rows in web forms Codebeh =?Utf-8?B?ZnJhbmtraXJjaG5lcg==?= Microsoft C# .NET 2 27th Sep 2006 10:20 PM
How to retrieve data through data access forms using primary key? =?Utf-8?B?Q2hhbmR1?= Microsoft Access Forms 1 29th Oct 2004 05:45 PM
Adding new records when databinding and using data relation win forms / data form wizard Developer Microsoft ADO .NET 0 16th Aug 2004 07:28 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:14 AM.