简体   繁体   English

我如何从C#中同一项目下的不同Windows窗体访问不同的组件?

[英]How can i access the different component from different windows form which are under same project in c#?

For example in form 4 i have a data grid view and when i select a data from data grid view and click on update.. form 5 pops up with various empty text fields. 例如,在表单4中,我有一个数据网格视图,当我从数据网格视图中选择一个数据并单击更新时。表单5弹出带有各种空文本字段。 Now i wanna make sure those text fields are filled up with my data that i selected in data grid view. 现在,我要确保这些文本字段填充了我在数据网格视图中选择的数据。 How can i make that happen? 我怎样才能做到这一点?

say i selected 4th row in my data grid view which is 说我在数据网格视图中选择了第四行

id = 4
name = name
address = address

on form 5 i have 3 text box ie so when form load how can i make those text box auto-filled with the data i selected in data grid view in form 4. 在表格5上,我有3个文本框,即在加载表格时如何使那些文本框自动填充在表格4的数据网格视图中选择的数据。

Probably the best way is to write a custom constructor for your form, and pass the data to it. 最好的方法可能是为表单编写一个自定义构造函数,然后将数据传递给它。 Something like this: 像这样:

 public DisplayForm(int id, string name, string address)
{
    InitializeComponent();
    //Take the data that you passed to the constructor, and use it to update the text boxes:
    txtID.Text = id.ToString();
    txtName.Text = name;
    txtAddress.Text = address;
}

You then call it using the constructor that you just wrote: 然后,使用刚刚编写的构造函数调用它:

DisplayForm display = new DisplayForm(4, name, address);

You could even create one that takes a DataGridViewRow as an argument: 您甚至可以创建一个以DataGridViewRow作为参数的对象:

 public DisplayForm(DataGridViewRow row)
{
    InitializeComponent();
    //Take the data that you passed to the constructor, and use it to update the text boxes:
    txtID.Text = row.Cells[0].Value.ToString();
    txtName.Text = row.Cells[1].Value.ToString();
    txtAddress.Text = row.Cells[2].Value.ToString();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从不同的表单访问按钮按下? C# - How can I access a Button Press from a different Form? C# 如何检查不同picBox中的图片是否相同? C#Windows窗体 - How to check if pictures in different picBoxes are the same? c# windows form [C#] [XNA 3.1]如何在一个Windows窗体中托管两个不同的XNA窗口? - [C#][XNA 3.1] How can I host two different XNA windows inside one Windows Form? 相同的C#Windows Form应用程序从不同的打印机获得不同的打印尺寸 - Different print dimensions from different printers by same C# Windows Form Application C# 从另一个表单打开一个表单,该表单位于不同的项目中 - C# Open a form from another form, which is located in a different project 如何在 Unity C# 的多个不同脚本中访问具有相同名称的 function - How can I access a function with the same name held on multiple different scripts in Unity C# 在同一解决方案中访问来自其他项目的表单的控件 - To access controls of a form from a different project in same solution vsto:使用C#在相同的解决方案中从Windows窗体应用程序迁移不同的excel工作簿 - vsto: Caling different excel workbooks from windows form application in same solutions using c# 如何在从数据库收集数据时通过登录页面在C#中打开不同的表单页面 - How can i open different form pages in C# by login page while collecting data from database 我可以从同一解决方案中的不同项目访问另一个项目的嵌入式资源吗? - Can I access another project's Embedded Resource from a different project in the same solution?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM