Problems pulling info from Form1 to Form2

W

wanyok

I thought I had it figured out but I'm still getting errors :( I would
appreciate any help I could get.

I have two forms: Form1 and frmVerify. Once the info gets submitted on
Form1 its text fields are transfered to some label fields to frmVerify.
Sounds easy but I keep getting "System.Windows.Forms.Groupbox' does not
contain a definition for 'verifyForm'
Here is that part of the code:

private void btnSubmit_Click(object sender, EventArgs e)
{
frmVerify verifyForm = new frmVerify(); //initialazing
Form2 (frmVerify)
if (condition)
MessageBox.Show(...)
else
{
verifyForm.lblFirmName.Text = this.txtFirm.Text;
verifyForm.lblProgramType.Text = this.grpProgram.
verifyForm.lblInvoiceNumber.Text = this.txtInvoiceNumber.Text;
verifyForm.ShowDialog();
}
Any suggestions ?
Thank you.
 
G

Guest

Hi,

At what point in your process and where exactly in your code block is the
exception being thrown. Please post the code if possible.

Thanks
 
S

Simon Tamman

You wont be able to immediately assign values from 1 forms control to
another unless the controls are public on the verify form.
However I would not recommend making these controls public as that ruins
encapsulation.

Either, create public properties to access the controls on the other form or
create an intermediate object to carry the data across.

E.G.

class Form1:Form
{
private void btnSubmit_Click(object sender, EventArgs e)
{
FormVerify verify = new FormVerify();
VerficationData data = new VerficationData();
data.Program = grpProgram;
data.FirmName = txtFirm.Text;
data.InvoiceNumber = txtInvoiceNumber.Text;
verify.Initialize(data);
verify.ShowDialog();
}
}

class FormVerify:Form
{
public void Initialize(VerificationData data)
{
lblFirmName.Text = data.FirmName;
lblProgramType.Text = data.Program.
lblInvoiceNumber.Text = data.InvoiceNumber;
}
}

class VerficationData
{
string program;
string firmName;
string invoiceNumber;

public string Program
{
get{return program;}
set{program = value;}
}

public string FirmName
{
get{return firmName;}
set{firmName = value;}
}

public string InvoiceNumber
{
get{return invoiceNumber;}
set{invoiceNumber = value;}
}
}

HTH

Simon Tamman
 
V

Vanyok

The error is highlighting the first part of this line:

verifyForm.lblInvoiceNumber.Text = this.txtInvoiceNumber.Text;

And I do have Modifiers for all labels on form2 set to public.. hmmm
 
V

Vanyok

And sorry for my Newbiness but could you tell me a bit moer about this
method you mnetions? :

"FormVerify verify = new FormVerify();
VerficationData data = new VerficationData(); "

What is the purpose of this ?
 
V

Vanyok

SOrry for my newbiness but could you tell me a bit more about:

VerficationData data = new VerficationData();
data.Program = grpProgram;
verify.Initialize(data);

I want to understand what it does. Thank you :)
 
V

Vanyok

Actual code as you requested: ( the exception is being thrown on
verifyForm.lblInvoiceNumber.Text = this.txtInvoiceNumber.Text line)

private void btnSubmit_Click(object sender, EventArgs e)
{
frmVerify verifyForm = new frmVerify(); //initialazing
Form2 (frmVerify)


//Making sure client name isnt' blank
if (txtFirm.Text == "")
{
MessageBox.Show("Please Enter the firm name.", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{

verifyForm.lblFirmName.Text = this.txtFirm.Text;
verifyForm.lblProgramType.Text = this.grpProgram.
verifyForm.lblInvoiceNumber.Text =
this.txtInvoiceNumber.Text;




verifyForm.ShowDialog();


} //eNd of Else
 
G

Guest

As Simmon indicate you init all the data that you want to load on frmVerfiy
with into an instance of VerficationData, then pass the latter into the
constructor of frmVerfiy, from which you pefrom the init as desired.
 
S

Simon Tamman

I'm not sure if I can give any more information as I did provide a whole
bunch of code to demonstrate the workings.

As MMA has specified later in the thread, my advice is to just package up
the values from form1 into a "container class" (VerficationData) and have
the VerifyForm read those values in via a method called Initialize().
This is a "cleaner" because it seperates the data from the controls, with
this approach you could also use databinding as well.

I would assume that if you implemented this object then your exception would
probably disappear as it's possibly something to do with making the controls
on the second form public or it's some wierd designer oddity. Either that or
it's something to do with the rest of your code that isn't posted because
the code you provided shouldn't really cause that exception to exist.

HTH

Simon
 
V

Vanyok

Thank you so much for your replies guys! I found what's wrong.
Seriously, I"m an idiot lol ...
This: verifyForm.lblProgramType.Text = this.grpProgram.
....Was trying to assign groupbox to text box... duuuhhh

I will know about assigning data to "container class" for the next
time.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top