Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi a have two form in the my project c#
i want get a value form form 1 and send to form 2
how i do it?
example form 2 shown a info from this value in the form
how i do define my data type or object ?
mybe i want to send a table to send to form 2 .
Posted
Comments
pradiprenushe 13-Aug-13 1:41am    
use static variable to send data
H.Brydon 13-Aug-13 18:01pm    
No, that is not a realistic way to do that.
[no name] 13-Aug-13 5:14am    
You would do this the exact same way you have already been told. http://www.codeproject.com/Questions/438840/how-i-do-get-a-value-of-variable-in-form2

Have a look at similat question: How to Change controls properties from another form in C#[^]. There you'll find a solution ;)
 
Share this answer
 
Forms are basically classes where you can put variables to. So you can do something like the one below on your destination form

Suppose you have this class that you want to pass from your source to destination

C#
public class Product
    {
        public Product() { }
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public double Price { get; set; }
    }

On your destination form, declare the class that will contain the passed object
C#
public partial class Form2 : Form
{
    public string val; //added variable
    public Product product; //added Product class
    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        //Do something on val and product
    }
}

And then on your source form, you put values on the variables and on the object like below.
C#
Form2 frm2 = new Form2();
var product = new Product();
product.ProductID = 12345;
product.ProductName = "";
product.Price = 12.0;
frm2.val = "Test";
frm2.product = product;
frm2.Show();


OP wrote:
how i do define my data type or object ?

Not sure what you mean by that, but if you mean passing your objects from one form to another, yes it is possible and the process is the same with the one above.

Edit: I have added a Product class that will be passed from source to destination. Hope you find it useful.
 
Share this answer
 
v2
Comments
sadegh_rusta 13-Aug-13 1:58am    
i want know
how i do passing objects from one form to another ?
walterhevedeich 13-Aug-13 2:12am    
I have edited my answer. Hope you will find it useful.
From1:

Button_click(object sender,Evenargs e)
{
string product="AAAA";
int productID=001;

Form2 obj=new Form2(product,productID);
obj.show();
this.hide();
}

Form2:

public Form2(string product,int productID)//Constructor..
{
txtprod.text=product;
txtprodID.text=productID;
}

Hope it helps....
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900