8 Replies - 4794 Views - Last Post: 21 December 2010 - 04:50 PM Rate Topic: -----

#1 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Get Values from Form1 To Form2

Posted 21 December 2010 - 04:38 AM

i want to get the values from form1 to another form using C#.
in my form1 some values are name,address,contact and one datagridview ,if the button clicked the same values in the form1 should be saved in form2 .
here is my form2 code.

 cInvoice oInvoice=null;
 private frmWalkInCustomer oWalkInCustomer = new frmWalkInCustomer () ;
        
        private void btnSave_Click(object sender, EventArgs e)
        {
            oInvoice = new cInvoice();

            oInvoice.ChairNo = oWalkInCustomer.cboChairNo.Text;
            oInvoice.Date = oWalkInCustomer.dtpDate.Value.Date;
            oInvoice.Name = oWalkInCustomer.txtName.Text.Trim();
            oInvoice.Address = oWalkInCustomer.txtAddress.Text.Trim();
            oInvoice.City = oWalkInCustomer.cboCity.Text.Trim();
            oInvoice.State = oWalkInCustomer.cboState.Text.Trim();
            oInvoice.MobileNo = oWalkInCustomer.txtCell.Text.Trim();
            oInvoice.Amount = Convert.ToDecimal(txtAmount.Text.Trim());
            oInvoice.TotDis = Convert.ToDecimal(lblDiscountRate.Text);
            oInvoice.Total = Convert.ToDecimal(txtTotal.Text);
            oInvoice.TotalAmount = Convert.ToDecimal(txtTotAmt.Text);
            oInvoice.PayMode = cboPaymentMode.Text.Trim();

SO on..........................
...........................
}



Is This A Good Question/Topic? 0
  • +

Replies To: Get Values from Form1 To Form2

#2 DivideByZero  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 238
  • View blog
  • Posts: 551
  • Joined: 02-December 10

Re: Get Values from Form1 To Form2

Posted 21 December 2010 - 05:34 AM

I haven't done this in a while but try writing this in form 2.

form1.name_of_variable_you_want;

This post has been edited by DivideByZero: 21 December 2010 - 05:35 AM

Was This Post Helpful? 0
  • +
  • -

#3 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Get Values from Form1 To Form2

Posted 21 December 2010 - 05:55 AM

You could expose a property on Form2.cs that accepts whatever object you want, then in Form1 when you invoke that Form2, give it that object that's already filled with information.

Then in Form2's constructor you can use that object to prefill whatever you'd like.

Without more information this is the best I can do.
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Get Values from Form1 To Form2

Posted 21 December 2010 - 06:23 AM

Check out this tutorial by on passing values between forms.

http://www.dreaminco...orms-in-c%23/#/
Was This Post Helpful? 1
  • +
  • -

#5 D.Mulroy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 81
  • View blog
  • Posts: 430
  • Joined: 30-June 10

Re: Get Values from Form1 To Form2

Posted 21 December 2010 - 06:36 AM

As Sergio metnioned using properties would do the trick.

Form 1
int a;
Form newFrm = new Form();
newFrm._inA = _OutA
int _outA
{
   get { return a; }
}
newFrm.Show();
This.Dispose(false);



Form 2
int b;
int _inA
{
   Set { b = value };
}



int b on form 2 is now equals int a from form 1. If i'm wrong here (somewhat new to properties) somebody please correct me :]
Was This Post Helpful? 0
  • +
  • -

#6 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1252
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Get Values from Form1 To Form2

Posted 21 December 2010 - 06:41 AM

I'd change the code to be more readable (even I had trouble reading it). This is using the constructor but you could change it to use a property no sweat. I'm assuming int a is a REQUIRED variable, that's why I used the constructor.

int a = 10;
Form2 theOtherForm = new Form2(a);
theOtherForm.Show();


//This code is the constructor for Form2:
int otherA;

public Form2(int a)
{
    otherA = a;
}

Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Get Values from Form1 To Form2

Posted 21 December 2010 - 10:11 AM

Does nobody search or even glance through the previous questions on here?
We must get this same question 3 times a week.

In this one from yesterday, the OP is told they need to raise an event in one form, and subscribe to it in the other form.
Sure there are ways of exposing the GUI elements or properties and directly changing them, but that tightly binds the forms together and subverts the idea of black-box coding where different classes aren't supposed to know that much about each other.

Please see this thread, rather than make everyone explain it all over again.
http://www.dreaminco...-another-class/
Was This Post Helpful? 2
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4455
  • View blog
  • Posts: 7,760
  • Joined: 08-June 10

Re: Get Values from Form1 To Form2

Posted 21 December 2010 - 04:03 PM

I wrote a tutorial to deal specifically with this question:
http://www.dreaminco...ny-other-forms/
Was This Post Helpful? 3
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5469
  • View blog
  • Posts: 11,749
  • Joined: 02-June 10

Re: Get Values from Form1 To Form2

Posted 21 December 2010 - 04:50 PM

View PostinsertAlias, on 21 December 2010 - 03:03 PM, said:

I wrote a tutorial to deal specifically with this question:
http://www.dreaminco...ny-other-forms/


Nice tutorial. I'm glad you did it! The link to is is now part of my standard reply links for the newbies.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1