473,385 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Data between forms

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.

Nov 22 '05 #1
2 1148
"Ari A" <an*******@discussions.microsoft.com> wrote in news:04f001c3bb3e
$8****************@phx.gbl:
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...

Nov 22 '05 #2
Michael Lang <ml@nospam.com> wrote in
news:Xn********************************@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
Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bill Stock | last post by:
The few times in the past that I've loaded unbound data, I've tended to cheat and use temp tables (not really unbound) or use code for small datasets. I'm currently involved in a project that...
3
by: juky | last post by:
Hi all, I have 2 applications one in VB.net and the other in VC6. I need to pass data between them. How can I do it? what's the best way to implement such communication ? Any comment will be...
6
by: Tejpal Garhwal | last post by:
I have datagrid filled with some data rows. At the run time i want know how many total rows are there in the data grid ? Any idea ? Any Suggestions ? Thanks in advance Tej
1
by: Angus Lepper | last post by:
I'm writing a stock ticker for a stock market simulation, and can load the data into the xmlreader in the first place, but can't figure out how to refresh/update the data in it. Any ideas? Code:...
3
by: Uma sakshi | last post by:
Hi I have one VB.NET application,in that application i have one datagrid control.The datagrid control contains somedata.I want to copy the data in a particular cell and paste it into my C#.NET...
0
by: Eric Sabine | last post by:
OK, I'm trying to further my understanding of threading. The code below I wrote as kind of a primer to myself and maybe a template that I could use in the future. What I tried to do was pass data...
10
by: Doug Bell | last post by:
Hi, I have an application that has a "Data Access Class" and "User Interface Class". It is for receiving Purchase Order data from one system and pushing processed transactions to another...
1
by: mfunkmann | last post by:
Hi, I recently got an error and I don't know how to fix it: Error 1 'System.Data.DataColumn' does not contain a definition for 'Windows' C:\c#\CsharpPRO\Form1.Designer.cs 304 77 CsharpPRO I...
19
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect...
6
by: Wesley Peace | last post by:
I hate to cross post, but I've gotten no answer yet on a problem I'm having with visual studio 2008. I've created a series of forms with controls to access a Access database tables. The...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.