从C#中的另一个表单中获取值 [英] get a value from another form in the C#

查看:64
本文介绍了从C#中的另一个表单中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
a在我的项目中有两个表格c#

i想要获得一个表单形式1并发送到表单2

我是怎么做到的?

示例表单2在表单中显示了此值的信息

如何定义我的数据类型或对象?

mybe我要发送要发送到表单2的表。

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 .

推荐答案

查看similat问题:如何从C#中的另一个表单更改控件属性 [ ^ ]。在那里你会找到一个解决方案;)
Have a look at similat question: How to Change controls properties from another form in C#[^]. There you'll find a solution ;)


表单基本上是可以放置变量的类。所以你可以在你的目的地表格上做类似下面的事情



假设你有这个课你想从你的来源传递到目的地



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

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

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.

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写道:

我如何定义我的数据类型或对象?

how i do define my data type or object ?



不确定你的意思,但如果你的意思是将你的对象从一个表单传递到另一个表单,是的它是可能的,并且过程与上面的过程相同。



编辑:我添加了一个将从源传递到目标的Product类。希望你觉得它很有用。


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.

I have added a Product class that will be passed from source to destination. Hope you find it useful.


From1:



Button_click(对象发送者,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)//构造函数..

{

txtprod.text = product;

txtprodID.text = productID;

}



希望它有所帮助......
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....


这篇关于从C#中的另一个表单中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆