从数据绑定标签获取文本

时间:2015-12-16 15:46:59

标签: vb.net data-binding

我有label绑定到BindingSource。标签文本正确填充,但我无法检索该信息。换句话说,我可以看到正确的文本,但我不能在我的代码中使用它,如下所示:

Public Sub LoadImage(ByVal caseNum As String, ByVal commnum As String)
    If Me.SpGetLOMCDocumentPathTableAdapter.GetData(caseNum, commnum).Count > 0 Then
        Try
            Me.SpGetLOMCDocumentPathTableAdapter.Fill(Me.DevGISDataSet.spGetLOMCDocumentPath, caseNum, commnum)

            Dim docPath As New BindingSource
            docPath = Me.SpGetLOMCDocumentPathBindingSource

            Me.bnTIF.BindingSource = docPath

            Me.lblDocPath.DataBindings.Add(New System.Windows.Forms.Binding("Text", docPath, "DocPath", True))

            Dim currentPath As String = Nothing

            currentPath = Me.lblDocPath.Text
            MessageBox.Show(Me.lblDocPath.Text)

            Me.Show()
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message.ToString)
        End Try
    Else
        MessageBox.Show("Image not available. Please check FEMA and CAMSIS.")
        Exit Sub
    End If
End Sub

不会抛出任何错误,消息框为空。此方法适用于其他表单上的文本框。

使用绑定处理程序

Public Sub LoadImage(ByVal caseNum As String, ByVal commnum As String)
    If Me.SpGetLOMCDocumentPathTableAdapter.GetData(caseNum, commnum).Count > 0 Then
        Try
            Me.SpGetLOMCDocumentPathTableAdapter.Fill(Me.DevGISDataSet.spGetLOMCDocumentPath, caseNum, commnum)

            Dim docPath As New BindingSource
            docPath = Me.SpGetLOMCDocumentPathBindingSource

            Me.bnTIF.BindingSource = docPath
            'Me.dgDocPath.DataSource = docPath

            AddHandler docPath.BindingComplete, AddressOf Me.HandleBindingCompleted

            Me.lblDocPath.DataBindings.Add(New System.Windows.Forms.Binding("Text", docPath, "DocPath", True))
            Me.txtDocPath.DataBindings.Add(New System.Windows.Forms.Binding("Text", docPath, "DocPath", True))

            Me.Show()
        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message.ToString)
        End Try
    Else
        MessageBox.Show("Image not available. Please check FEMA and CAMSIS.")
        Exit Sub
    End If
End Sub

Private Sub HandleBindingCompleted(ByVal sender As Object, ByVal e As BindingCompleteEventArgs)
    MessageBox.Show("Test")
    If (Not e.Exception Is Nothing) Then
        Debug.WriteLine(e.ErrorText)
        MessageBox.Show(e.ErrorText)
    Else
        MessageBox.Show("No error")
    End If
End Sub

即使是“测试”消息框也无法显示。

调用此方法

Public Sub LoadLOMCImage(ByVal casenum As String, ByVal commnum As String)
    Try
        DisposeLOMCForm()

        Dim LOMCtif As New frmLOMCTiff
        LOMCtif.LoadImage(casenum, commnum)
    Catch ex As Exception
        MessageBox.Show("Error finding LOMC image: " & ex.Message.ToString)
    End Try
End Sub

我从一个单独的表单调用LoadImage Sub。同样,数据正确传递,我可以使用GetData的{​​{1}}方法正确读取数据。我可以将TableAdapter设置为BindingSource,但我遇到了同样的问题:我可以看到数据行,但无法访问索引为0的行(基本异常,索引超出范围等等。)

使用建议的代码:

DataGridView

同样,绑定成功,但我无法访问标签文本。

0 个答案:

没有答案