在表单关闭时在表单之间传递数据 [英] Passing data between forms on form closing

查看:37
本文介绍了在表单关闭时在表单之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有文本框、按钮和公共共享属性的表单.该按钮显示另一个带有数据网格视图的表单.当在数据网格视图中单击一行时,它会获取选定的值并将其分配给原始表单的公共共享属性,同时关闭表单

I have a form with a text box, button and a public shared property. The button displays another form with a data grid view. When a row is clicked on in the data grid view it takes the value that was selected and assigns it to the public shared property of the original form, as well as closes the form

Private Sub dgvAllSku_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvAllSku.CellContentDoubleClick
    frmMain.Sku = dgvAllSku.Rows.Item(e.RowIndex).Cells(0).Value
    Me.Close()
End Sub

现在我想要发生的是,当此表单关闭时,我希望值 (frmMain.Sku) 填充原始表单上的文本框.我希望在数据网格视图表单关闭时再次触发 GotFocus 事件,我可以使用该事件为文本框分配公共属性的值,但是当表单关闭时,不会触发获得焦点事件.

Now what I want to have happen is, as this form closes I want the value (frmMain.Sku) to fill the text box on the original form. I was hoping the GotFocus event would be fired again when the data grid view form closes and I could use that event to assign the text box the value of the public property, but that got focus event isn't firing when the form closes.

Private Sub frmMain_Activated(sender As Object, e As EventArgs) Handles Me.GotFocus
    txtSku.Text = frmMain.Sku
End Sub

我如何做到这一点?

推荐答案

可能有很多方法,但我能想到的最快方法就是将 txtSku.Text 作为公共属性公开并让另一个窗口自行设置:

Probably a lot of ways, but the quickest way I can think of is just expose the txtSku.Text as a public property and let the other window just set it itself:

'Define in your main form
Public Property SkuText() As String
Get
    Return txtSku.Text
End Get
Set(ByVal value As String)
    txtSku.Text = value
End Set
End Property

然后只需从另一个窗口设置文本:

Then just set the text from your other window:

Private Sub dgvAllSku_CellContentDoubleClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvAllSku.CellContentDoubleClick
    frmMain.SkuText = dgvAllSku.Rows.Item(e.RowIndex).Cells(0).Value
    Me.Close()
End Sub  

您还可以引发另一个表单订阅的事件,或者如果您没有任何理由仍然可以同时使用这两个表单,只需使用 ShowDialog 并在主窗体将停止并等待用户选择一个单元格,然后返回并轻松地从它刚刚显示的窗体中提取选定的值.

You could also raise an event that the other form subscribes to, or if you don't have any reason to still be able to use both forms at once, just use ShowDialog and execution in the main form will stop and wait for the user to select a cell, then return and easily pull the selected value from the form it just showed.

这篇关于在表单关闭时在表单之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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