vb.net 如何在 VB 2012 中使用带有表单和代码的 Tag 属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22292641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:00:56  来源:igfitidea点击:

How do I use the Tag property with forms and code in VB 2012?

vb.nettag-property

提问by Aegelis

I am writing a program using a database for customers and technicians. The main form (CustomerIncidents) has a toolstripbutton that opens a different form to (SearchByState) where the user inputs a state code and looks for any incidents.

我正在为客户和技术人员使用数据库编写程序。主窗体 (CustomerIncidents) 有一个工具条按钮,可打开一个与 (SearchByState) 不同的窗体,用户在其中输入状态代码并查找任何事件。

If the user clicks into one of the datagrid cells I want that customers information to be stored in the TAG so that when the form is closed using the OK button that it will show back up in the main form (CustomerIncidents).

如果用户单击其中一个数据网格单元格,我希望将客户信息存储在 TAG 中,以便在使用“确定”按钮关闭表单时,它将显示在主表单 (CustomerIncidents) 中。

Edited 03/11/14 12:21pm

已编辑 03/11/14 下午 12:21

The problem is in the Main Form. When I click the OK button in the Second Form it tries to convert the DialogResult Button to a String. I can't figure out how to fix it.

问题出在主窗体中。当我单击“第二个表单”中的“确定”按钮时,它会尝试将 DialogResult 按钮转换为字符串。我不知道如何修复它。

Customer Form (Main Form) Opens to Secondary Form

客户表格(主表格)打开二级表格

Private Sub btnOpenState_Click(ByVal sender As System.Object, 
ByVal e As System.EventArgs) Handles btnOpenState.Click
        Dim frmSearchState As New FindCustomer
 ----->>Dim selectedButton As DialogResult = frmSearchState.ShowDialog()
        If selectedButton = Windows.Forms.DialogResult.OK Then
            CustomerIDToolStripTextBox.Text = frmSearchState.Tag.ToString
        End If'

Search By State Form (Secondary Form) Or "Child Form"

按州表格(二级表格)或“子表格”搜索

Private Sub btnOk_Click(message As String, ByVal e As DataGridViewCellEventArgs) Handles btnOk.Click

    message = CustomersDataGridView.Rows(e.RowIndex).Cells(e.ColumnIndex).Value.ToString

    Me.Tag = message
    Me.DialogResult = DialogResult.OK
End Sub

采纳答案by LarsTech

The click event for a button does not have a DataGridViewCellEventArgs parameter, and will throw an exception when you try to use it.

按钮的单击事件没有 DataGridViewCellEventArgs 参数,当您尝试使用它时会抛出异常。

You don't need to use the Tag property since you can just create your own property.

您不需要使用 Tag 属性,因为您可以创建自己的属性。

In your child form, create a property called GridValue:

在您的子窗体中,创建一个名为 GridValue 的属性:

Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
  If dgv.CurrentCell Is Nothing OrElse dgv.CurrentCell.Value Is Nothing Then
    MessageBox.Show("A cell needs to be selected.")
  Else
    Me.DialogResult = DialogResult.OK
  End If
End Sub

Public ReadOnly Property GridValue As String
  Get
    Return dgv.CurrentCell.Value.ToString
  End Get
End Property

In your parent form, you can now access your information:

在您的父表单中,您现在可以访问您的信息:

Using frmSearchState As New FindCustomer
  If frmSearchState.ShowDialog(Me) = DialogResult.Ok Then
    CustomerIDToolStripTextBox.Text = frmSearchState.GridValue
  End If
End Using

回答by dotNET

My personal approach for doing this kind of stuff is to create a public property in the child form, having the same type as the DATA you want to take back to your main form. So instead of storing DataGridView's reference in Tagproperty, you should really be storing the actual value that was there in the cell that the user clicked on.

我个人做这种事情的方法是在子窗体中创建一个公共属性,与要带回主窗体的 DATA 具有相同的类型。因此,不是将DataGridView的引用存储在Tag属性中,您应该真正存储用户单击的单元格中的实际值。

For example, if your DGV cell has a string value in it, you could do something like:

例如,如果您的 DGV 单元格中有一个字符串值,您可以执行以下操作:

Public Readonly Property StateName As String
    Get
        If YourDGV.SelectedCell IsNot Nothing Then
            Return YourDGV.SelectedCell.Value
        Else
            Return ""
        End If
    End Get
End Property

(I have written that code by hand, so there may be some syntax problems, but you should be able to get the idea.)

(我手工编写了该代码,因此可能存在一些语法问题,但您应该能够理解。)

You can now use ShowDialog()in the main form to bring up this child form and upon OK or Cancel, you could check the value of StateNameproperty of your child form to get this value. The thing to remember here is that closing a form doesn't dispose off all its constituent controls and properties and therefore you can access them even after the form has finished ShowDialog()call.

您现在可以ShowDialog()在主窗体中使用来调出此子窗体,在确定或取消时,您可以检查StateName子窗体的属性值以获取此值。这里要记住的是,关闭表单不会释放其所有组成控件和属性,因此即使在表单完成ShowDialog()调用之后您也可以访问它们。