472,980 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Pass values b/w forms using properties

I am trying to pass a value from a texbox in Form1 to a textbox in Form2
using properties in VS2005 but it doesn't work; please help (project is
attached).

Code for Game Class:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyProject
{
class Game
{
private string strName;
public string Name
{
get
{
return strName;
}
set
{
strName = value;
}
}

private string strMfg;
public string Mfg
{
get
{
return strMfg;
}
set
{
strMfg = value;
}
}
public Game()
{ }

public Game(string strName, string Mfg)
{
this.Name = strName;
this.Mfg = strMfg;
}
}
}

Code for Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
Game clssGame = new Game();
clssGame.Name = txtName.Text;
clssGame.Mfg = txtMfg.Text;

Form2 frm = new Form2();
frm.ShowDialog();
}
}
}
Code for Form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyProject
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

private void Form2_Load(object sender, EventArgs e)
{
Game clssGame = new Game();
txtName.Text = clssGame.Name;
txtMfg.Text = clssGame.Mfg;
}
}
}
Aug 4 '08 #1
6 1662
On Mon, 04 Aug 2008 10:20:04 -0700, Ryan <Ry**@discussions.microsoft.com>
wrote:
I am trying to pass a value from a texbox in Form1 to a textbox in Form2
using properties in VS2005 but it doesn't work; please help (project is
attached).
It's hard to know exactly what your intent is. But it _looks_ like you
are trying to use the Game class as a mediator between your two form
classes. Unfortunately, you use a different instance of the Game class in
Form2.Form2_Load() than you used in Form1.button1_Click().

You either need to pass the Game instance you created in button1_Click()
to Form2 somehow, or (probably better) just pass the values to a Form2
constructor or create public properties on the Form2 class that Form1 can
use to set the values.

Pete
Aug 4 '08 #2
I've provided the code just to show you my approach which isn't working.
I need someone to help me fix my code or provide aother sample code to pass
values between forms using properties driven by a class.

"Peter Duniho" wrote:
On Mon, 04 Aug 2008 10:20:04 -0700, Ryan <Ry**@discussions.microsoft.com>
wrote:
I am trying to pass a value from a texbox in Form1 to a textbox in Form2
using properties in VS2005 but it doesn't work; please help (project is
attached).

It's hard to know exactly what your intent is. But it _looks_ like you
are trying to use the Game class as a mediator between your two form
classes. Unfortunately, you use a different instance of the Game class in
Form2.Form2_Load() than you used in Form1.button1_Click().

You either need to pass the Game instance you created in button1_Click()
to Form2 somehow, or (probably better) just pass the values to a Form2
constructor or create public properties on the Form2 class that Form1 can
use to set the values.

Pete
Aug 4 '08 #3
On Mon, 04 Aug 2008 11:01:06 -0700, Ryan <Ry**@discussions.microsoft.com>
wrote:
I've provided the code just to show you my approach which isn't working.
We can only comment on code we see.
I need someone to help me fix my code or provide aother sample code to
pass
values between forms using properties driven by a class.
What about my answer doesn't provide that? Was there something you didn't
understand?

Simply implying that an answer didn't solve your problem doesn't provide
us with any way to know why you didn't understand, or what clarification
might be needed.

Pete
Aug 4 '08 #4
Peter, he's a beginner and doesn't understand all the fancy words
you're using, like "mediator", "instance", "values", "code"...

In Form1 :

private void button1_Click(object sender, EventArgs e)
{
Game clssGame = new Game();
clssGame.Name = txtName.Text;
clssGame.Mfg = txtMfg.Text;
Form2 frm = new Form2(clssGame);
frm.ShowDialog();
}

In Form2 :
Remove Form2_Load, then :

public Form2(Game clssGame)
{
InitializeComponent();
txtName.Text = clssGame.Name;
txtMfg.Text = clssGame.Mfg;
}

And as Peter said, it's hard to guess your intent. You're passing a
reference of an object to a client class, that might not be desirable.
You'd probably be better off passing the values, but your question is
ambiguous, as you don't said why you insist on "using properties
driven by a class".

Michel

On 4 août, 20:21, "Peter Duniho" <NpOeStPe...@nnowslpianmk.comwrote:
On Mon, 04 Aug 2008 11:01:06 -0700, Ryan <R...@discussions.microsoft.com>*
wrote:
I've provided the code just to show you my approach which isn't working..

We can only comment on code we see.
I need someone to help me fix my code or provide aother sample code to *
pass
values between forms using properties driven by a class.

What about my answer doesn't provide that? *Was there something you didn't *
understand?

Simply implying that an answer didn't solve your problem doesn't provide *
us with any way to know why you didn't understand, or what clarification *
might be needed.

Pete
Aug 5 '08 #5

Look at the code that has been changed.

>
Code for Game Class:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyProject
{
class Game
{
private string strName;
public string Name
{
get
{
return strName;
}
set
{
strName = value;
}
}

private string strMfg;
public string Mfg
{
get
{
return strMfg;
}
set
{
strMfg = value;
}
}
public Game()
{ }

public Game(string strName, string Mfg)
{
this.Name = strName;
this.Mfg = strMfg;
}
}
}

Code for Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyProject
{
public partial class Form1 : Form
{
private Game clssGame; '>>>>declare it here
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
clssGame = new Game(); '>>>>>>>set it NEW here
clssGame.Name = txtName.Text;
clssGame.Mfg = txtMfg.Text;

Form2 frm = new Form2(clssGame); ''>>>>>>>>>pass it in the
form's constructor
frm.ShowDialog();
}
}
}
Code for Form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyProject
{
public partial class Form2 : Form
{
private Game clssgame; '>>>>>>declare it here
public Form2(Object passclssGame) '>>>>>passclssGame is just an
Object in the form being declared
{
clssgame = (Game)passclssGame; '>>>>>>CAST passclssGame to
the Game class
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

private void Form2_Load(object sender, EventArgs e)
{
txtName.Text = clssGame.Name;
txtMfg.Text = clssGame.Mfg;
}
}
}
Aug 5 '08 #6
Thanks to all of you.

"Mr. Arnold" wrote:
>
Look at the code that has been changed.


Code for Game Class:

using System;
using System.Collections.Generic;
using System.Text;

namespace MyProject
{
class Game
{
private string strName;
public string Name
{
get
{
return strName;
}
set
{
strName = value;
}
}

private string strMfg;
public string Mfg
{
get
{
return strMfg;
}
set
{
strMfg = value;
}
}
public Game()
{ }

public Game(string strName, string Mfg)
{
this.Name = strName;
this.Mfg = strMfg;
}
}
}

Code for Form1:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyProject
{
public partial class Form1 : Form
{
private Game clssGame; '>>>>declare it here
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
clssGame = new Game(); '>>>>>>>set it NEW here
clssGame.Name = txtName.Text;
clssGame.Mfg = txtMfg.Text;

Form2 frm = new Form2(clssGame); ''>>>>>>>>>pass it in the
form's constructor
frm.ShowDialog();
}
}
}
Code for Form2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace MyProject
{
public partial class Form2 : Form
{
private Game clssgame; '>>>>>>declare it here
public Form2(Object passclssGame) '>>>>>passclssGame is just an
Object in the form being declared
{
clssgame = (Game)passclssGame; '>>>>>>CAST passclssGame to
the Game class
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

private void Form2_Load(object sender, EventArgs e)
{
txtName.Text = clssGame.Name;
txtMfg.Text = clssGame.Mfg;
}
}
}

Aug 6 '08 #7

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

Similar topics

12
by: Casey | last post by:
Yeah, I know this question was asked by someone elselike 2 weeks ago. But I need some additional help. I have a program I'm developing, and multiple different forms will be opened. For now though,...
2
by: Nick | last post by:
A lot of people say to use properties to pass variables between a main dialog and a subdialog like so: Form form = new Form(); if (form.ShowDialog()==DialogResult.OK) { string str =...
0
by: Daimy | last post by:
I meet the same problem below, please help me! Thanks! //written by some one I have developed a windows forms user control, which I am going to host in Internet Explorer.. I am familiar...
3
by: Hermit Dave | last post by:
Hi, Trying to pass a value to user control within a page. 1. Couldnt find a way to pass it from code behind file. 2. Trying to pass it from aspx page... using EditURL='<% "mypage.aspx?myvar=" +...
2
by: macyp | last post by:
I have to pass values from one aspx page to another. The controls I have in the first page are: a textbox, 3 drop down lists, and 2 check boxes, and a submit button. It is a search page, and the...
1
by: colleen1980 | last post by:
There is a form where it ask the two dates and then run a query then report. Private Sub Command36_Click() DoCmd.OpenQuery "qryResultsReport" DoCmd.OpenReport "rpt_TopTen", acPreview, "", ""...
14
by: =?Utf-8?B?Umljaw==?= | last post by:
I have seen examples of passing data from FormB to FormA (Parent To Child) but I need an example of passind data from FormA to FormB. My main form is FormA; I will enter some data in textboxes and...
5
by: Rob | last post by:
I have a control (Button) on a Parent form which opens a Windows form... all I want to do is pass a value from the child form back to the parent... it should be so simple... i.e....
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.