如何将下拉菜单链接到其他类

时间:2014-04-16 23:58:44

标签: sql vb.net drop-down-menu

我有一个问题,我必须将下拉菜单链接到其他类,该类是sqlcommand

所以我的代码有点像这样

Form1.vb的

Public Class Input
   Dim CallSQL As New SQL Private Sub Button1_Click(ByVal sender As 
             System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ListBox1.Visible = True
    If ListBox1.SelectedValue = "A" Then
      -------"I DONT KNOW HOW TO COMMAND HERE "--
    End If
    If ListBox1.SelectedValue = "B" Then
      -------"I DONT KNOW HOW TO COMMAND HERE "---------
      End If
    End Sub
End Class

我需要匹配此处的下拉列表

SQL.vb

Public Class SQL 

Private Sub GetRoadDataFromDatabase(station)
    ExecuteSqlCommand("exec '" & station & "', 'A'", "TableA")
End Sub

Public Sub GettrainDataFromDatabase(station) 
    ExecuteSqlCommand("exec  '" & station & "', 'B'", "TableB")
End Sub

所以当用户选择A时,执行道路的数据,并选择B执行列车的数据。请帮助我。这假设是一项简单的任务。但是因为我在vb.bet的新功能我无法弄清楚

1 个答案:

答案 0 :(得分:0)

如果用户选择选项' A'您希望执行GetRoadDataFromDatabase如果用户选择GetTrainDataFromDatabase,则B。只需在SQL语句之外创建IF/ELSE类的实例,并在IF/ELSE中调用相关方法:

Public Class Input
   Dim CallSQL As New SQL Private Sub Button1_Click(ByVal sender As 
             System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ListBox1.Visible = True
    Dim sql AS SQL =new SQL() 'Please check this if it is valid VB.Net statement
    If ListBox1.SelectedValue = "A" Then
        'Supposing you have got station already
        sql.GetRoadDataFromDatabase(station)
    End If
    If ListBox1.SelectedValue = "B" Then
        'Supposing you have got station already
        sql.GettrainDataFromDatabase(station)
    End If
    End Sub
End Class

编辑: - 不公开方法

正如您所说,您不希望公开这两个函数,一个选项是编写一个网关函数来接受参数,然后调用所需的私有方法。

Public Class SQL 

    Private Sub GetRoadDataFromDatabase(station)
        ExecuteSqlCommand("exec '" & station & "', 'A'", "TableA")
    End Sub

    'THOUGH YOUR THIS FUNCTION IS PUBLIC IN QUESTION
    Private Sub GettrainDataFromDatabase(station) 
        ExecuteSqlCommand("exec  '" & station & "', 'B'", "TableB")
    End Sub 

    Public Sub CallDBFunction(station as string, code as string)
        If code == "A" Then
            GetRoadDataFromDatabase(station)
        Else
            GetTrainDataFromDatabse(station)
        End If
    End Sub
End Class