将方法的返回值设置为公共字符串
我试图在表单之间传递一个值 - 在本例中,是 DataGridView 中突出显示的单元格的结果。
在我的主窗体中,我使用公共字符串方法获取值:
public string GetCaseID()
{
int i;
i = dgCases.SelectedCells[0].RowIndex;
string caseid = dgCases.Rows[i].Cells[1].Value.ToString();
string version = dgCases.Rows[i].Cells[2].Value.ToString();
return version + "_c" + caseid;
}
//Form2 is launched
private void btnEvLvlUserSelect_Click(object sender, EventArgs e)
{
Form2 form2= new Form2();
form2.ShowDialog();
}
由于 GetCaseID() 被声明为公共字符串,所以我应该能够从 Form2 中调用它,对吧?
在 Form2 中,我只有这个:
private void button1_Click(object sender, EventArgs e)
{
//Take selected case information
fmHome fmhome = new fmHome();
textBox1.Text = fmhome.GetCaseID();
}
我知道跨表单通信是有效的:如果我用普通的旧字符串替换 GetCaseID(),它会按预期在 Form2 中显示。
这是否与必须将 dgCases 声明为公开有关?
谢谢。
I'm trying to pass a value between forms - in this case, the result of a highlighted cell in a DataGridView.
In my main form, I get the value using a public string method:
public string GetCaseID()
{
int i;
i = dgCases.SelectedCells[0].RowIndex;
string caseid = dgCases.Rows[i].Cells[1].Value.ToString();
string version = dgCases.Rows[i].Cells[2].Value.ToString();
return version + "_c" + caseid;
}
//Form2 is launched
private void btnEvLvlUserSelect_Click(object sender, EventArgs e)
{
Form2 form2= new Form2();
form2.ShowDialog();
}
Since GetCaseID() is declared as a public string, I should be able to call it from my Form2, right?
In Form2, I just have this:
private void button1_Click(object sender, EventArgs e)
{
//Take selected case information
fmHome fmhome = new fmHome();
textBox1.Text = fmhome.GetCaseID();
}
I know the cross-form communication works: If I replace GetCaseID() with a plain old string, it displays as expected in Form2.
Could it be something to do with having to declare the dgCases as public as well?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在
button1_Click
中,您将创建fmHome
类的新实例。这是一个与创建它的fmHome
实例不同的实例,因此它在dgCases
中没有选定的行。在此实例上调用GetCaseID()
将不会返回您期望的结果。您的
button1_Click
处理程序需要有一种方法可以在打开它的表单上调用GetCaseID()
。一个非常基本的方法是在Form2
上添加这样的属性:然后,当您打开
Form2
实例时,执行以下操作:所以在您的
button1_Click< /code> 处理程序,您可以访问该实例而不是创建一个新实例:
希望这会有所帮助!
In
button1_Click
, you're creating a new instance of thefmHome
class. This is a different instance from the instance offmHome
that created it, so it doesn't have a selected row indgCases
. CallingGetCaseID()
on this instance will not return what you're expecting.Your
button1_Click
handler needs to have a way to callGetCaseID()
on the form that opened it. A very basic way would be to add a property like this onForm2
:Then, when you open your instance of
Form2
, do this:So in your
button1_Click
handler, you can access that instance instead of creating a new one:Hope this helps!
您在button1_Click 中创建fmHome 的新实例,因此它不包含原始数据网格。
您可以将数据网格的实例作为参数传递给 Form2 构造函数。
注意:混合视图和数据的方式将导致代码无法维护,这不是一个好的做法......但我假设您正在处理遗留代码?
You create a new instance of fmHome in button1_Click, so it does not contain your original datagrid.
You could pass the instance of the datagrid as a parameter to the Form2 constructor.
Note: the way you mix view and data will lead to unmaintainable code and is not a good practise... but I assume you're dealing with legacy code?
像您一样创建新实例不会获得任何有意义的价值。
相反,请确保
Form2
可以获取对原始fmHome
实例的引用。最常见的模式是添加一个fmHome
类型的(私有)Form2
成员变量,并将其设置为Form2
构造函数的一部分,要求创建者将其传入(在您的情况下,在btnEvLvlUserSelect_Click
中)。然后您可以在Form2.button1_Click()
方法中使用该成员,而不是创建一个新的 (看不见的)空形式。Creating a new instance as you are doing won't get any meaningful value.
Instead, make sure
Form2
can get a reference to the originalfmHome
instance. The commonest pattern is to add a (private)Form2
member variable of typefmHome
, and set it as part of theForm2
constructor(s), requiring the creator to pass it in (in your case, inbtnEvLvlUserSelect_Click
. Then you can use that member in yourForm2.button1_Click()
method instead of creating a new (invisible) empty form.您还可以执行以下操作:
然后在 Form2 中:
您还可以创建一个类,其中包含您的申请所需的所有信息,然后在需要的地方填写它,并在另一个表单上从中获取数据无需连接它们两个。但我不知道这是否是最好的解决方案。
What you can also do is the following:
Then in Form2:
What you can also do is make a class which holds all the information that is required for your application and then fill it up where needed and get the data from it on the other form without having to connect both of them. But I don't know if this is the best solution.