Aegelis
Aegelis

Reputation: 43

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

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.

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).

Edited 03/11/14 12:21pm

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.

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

Upvotes: 3

Views: 12838

Answers (2)

LarsTech
LarsTech

Reputation: 81620

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

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

In your child form, create a property called 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

Upvotes: 2

dotNET
dotNET

Reputation: 35400

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 Tag property, you should really be storing the actual value that was there in the cell that the user clicked on.

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

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 StateName property 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.

Upvotes: 0

Related Questions