使用vb.net

时间:2019-03-12 19:20:46

标签: mysql vb.net

我想从数据库MySql中选择ItemName和ItemPrice,然后将ItemNames和ItemPrices存储到按钮中,因此当我单击按钮时,我会得到名称和价格。

这是我的代码,它当前正在工作,并且仅在数据库中的行上显示价格。我认为我需要按按钮进行排列才能使其工作。

如果有人帮助我,那将是很棒的。我是vb.net的新手,想修复此程序

Public Property BtnMenuArray As Button
Public Property BtnDoneArray As Button
Public Property GBCustemerArray As GroupBox
Public Property TBCustemerOrderArray As TextBox
Public Property TBMenuItemNameArray As TextBox
Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset    
Public Function GetItemPrice(selection) As String
    TBMenuItemNameArray = New TextBox
    Dim output As String
    con.Open("Dsn=Invintory;uid=root")
    rs.Open("Select ItemPrice From menuitem where RowID = " & selection & "", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    output = rs.GetString()
    rs.Close()
    con.Close()
    Return output
End Function
Public Function GetItemName(selection) As String
    Dim output As String
    con.Open("Dsn=Invintory;uid=root")
    'rs.Open("Select ItemName From menuitem", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    rs.Open("Select ItemName From menuitem where RowID = " & selection & "", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    output = rs.GetString()
    rs.Close()
    con.Close()
    'MsgBox("Detailes are Added")
    Return output
End Function
Public Sub RunDB()
    Dim Ittrat As Short = GetItemCount()
    TextBox1.Text = Ittrat
    Dim MyNameArray As New List(Of String)
    Dim MyItemPriceArray As New List(Of String)
    For item = 0 To Ittrat - 1
        BtnMenuArray = New Button()
        MyNameArray.Add(GetItemName(item))
        BtnMenuArray.Text = MyNameArray(item)
        TableLayoutPanel1.Controls.Add(BtnMenuArray)
        MyItemPriceArray.Add(GetItemPrice(item))
        BtnMenuArray.Tag = MyItemPriceArray(item)
    Next
    AddHandler BtnMenuArray.Click, AddressOf ClickHandle
End Sub
Public Sub ClickHandle(ByVal sender As Object, ByVal e As _
   System.EventArgs)
    MsgBox(BtnMenuArray.Tag)
End Sub

1 个答案:

答案 0 :(得分:0)

我能够弄清楚。我需要做的是将按钮的名称添加到itrat所使用的for循环中,以将按钮的名称分配为数组。这也解决了我与动态按钮事件处理程序有关的问题。现在,单击按钮后,我可以看到每个按钮的名称,文本和标签。

Dim con As New ADODB.Connection
Dim rs As New ADODB.Recordset

Public Property ItemMenuBT As Button
Public Property BtnDoneArray As Button
Public Property GBCustemerArray As GroupBox
Public Property TBCustemerOrderArray As TextBox
Public Property TBMenuItemNameArray As TextBox

Public Function GetItemCount() As Short
    TBMenuItemNameArray = New TextBox
    Dim output As Short
    con.Open("Connection String")
    rs.Open("Select Count(RowID) From TableNameHere", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    output = Convert.ToInt16(rs.GetString())
    rs.Close()
    con.Close()
    Return output
End Function
Public Function GetItemPrice(selection) As String
    TBMenuItemNameArray = New TextBox
    TextBox1.Text = selection
    Dim output As String
    con.Open("Connection String")
    rs.Open("Select ColumnName From TableName where RowID = " & selection & "", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    output = rs.GetString()
    rs.Close()
    con.Close()
    Return output
End Function
Public Function GetItemName(selection) As String
    Dim output As String
    con.Open("Connection String")
    rs.Open("Select ItemName From menuitem where RowID = " & selection & "", con, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
    output = rs.GetString()
    rs.Close()
    con.Close()
    Return output
End Function
Public Sub LOAD_ITEM_MENU()
    Dim Ittrat As Short = GetItemCount()
    Dim MyNameArray As New List(Of String)
    Dim MyItemPriceArray As New List(Of String)
    Dim buttonName As String
    Dim buttonText As String
    For item = 0 To Ittrat - 1
        ItemMenuBT = New Button()
        ItemMenuBT.Size = New Size(130, 100)
        buttonName = GetItemName(item)
        buttonText = GetItemPrice(item)
        ItemMenuBT.Name = buttonName
        ItemMenuBT.Text = buttonName
        ItemMenuBT.Tag = buttonText
        TableLayoutPanel1.Controls.Add(ItemMenuBT)
        AddHandler ItemMenuBT.Click, AddressOf Me.ClickHand
    Next
End Sub
Public Sub ClickHand(ByVal sender As Object, ByVal e As _
   System.EventArgs)
    Dim CST_ORD_SELCTEDItemName As Button = sender
    'MsgBox("Item Name: " & CST_ORD_SELCTEDItemName.Name)
    'MsgBox("Item Price: " & CST_ORD_SELCTEDItemName.Tag)
    Dim valueNew As Double
    valueNew = CST_ORD_SELCTEDItemName.Tag
    TBCustemerOrderArray.Text = Val(TBCustemerOrderArray.Text) + valueNew
End Sub