将变量传递给ComboBox方法

时间:2017-10-27 16:20:47

标签: vb.net variables methods combobox

我有一个从我的第一个表单传递到第二个表单的变量。但是,当我尝试在我的ComboBox事件处理程序方法中使用此变量时,它显示为空白,我无法找出原因。

Public CellValue As String

Public Sub New(ByVal CellValue As String)
    InitializeComponent()
    MsgBox(CellValue) 
End Sub

这很好用,我可以使用Cell Value调出消息框。但是,当我尝试在同一表单上的组合框方法中访问此CellValue时,该变量为空,我无法弄清楚原因。

Public Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    MsgBox(CellValue)
End Sub

我已经尝试将" ByVal CellValue As String"作为组合框中的参数但是不起作用,我得到一个错误,它无法处理事件。

我将变量设置为公开,所以我不明白发生了什么。在正确方向上的任何帮助都会很棒,因为我一直在网上搜索几个小时。

如果有帮助,这是Form1:

Imports MySql.Data.MySqlClient
Public Class Form1
Dim MysqlConn As MySqlConnection
Dim COMMAND As MySqlCommand
Dim value As String

Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

End Sub

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString = "server=0.0.0.0;
                                  userid=username;
                                  password=******;
                                  database=password"
    Dim SDA As New MySqlDataAdapter
    Dim dbDataSet As New DataTable
    Dim bSource As New BindingSource

    Try
        MysqlConn.Open()
        Dim Query As String
        Query = "select CaseNumber, FirstName, LastName from customer"
        COMMAND = New MySqlCommand(Query, MysqlConn)
        SDA.SelectCommand = COMMAND
        SDA.Fill(dbDataSet)
        bSource.DataSource = dbDataSet
        DataGridView1.DataSource = bSource
        SDA.Update(dbDataSet)

        MysqlConn.Close()
    Catch ex As MySqlException
        MessageBox.Show(ex.Message)
    Finally
        MysqlConn.Dispose()
    End Try
End Sub

Public Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
    If e.ColumnIndex = 0 Then
        Dim CellValue As Object = DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Value

        If IsDBNull(value) Then
            MsgBox("No Case Number for this particular record.")
        Else

            Dim ThirdForm As New Form2(CellValue)
            Me.Hide()
            ThirdForm.Show()

        End If
    End If
End Sub
End Class

1 个答案:

答案 0 :(得分:0)

感谢Plutonix在评论部分,以下内容对我有用。谢谢。

 Me.CellValue = CellValue