473,326 Members | 2,168 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,326 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 1680
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....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.