471,350 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 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 1593
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by Casey | last post: by
3 posts views Thread by Hermit Dave | last post: by
14 posts views Thread by =?Utf-8?B?Umljaw==?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

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.