2017-10-12 78 views
0

See pic我對vb.net不熟悉。所以,我試圖做的是當我點擊選擇的產品,我把數量。總價格將顯示列表視圖中數量和價格的乘積值。我把情況2的代碼,因爲它是價格,但是當我運行它,它顯示0,然後當我改變它的值爲2,它不會乘。這裏是我到目前爲止的代碼:如何將文本框中的值與列表視圖中的列相乘?

Imports System.Data.SqlClient 
Imports System.IO 

Public Class Form2 
    Dim con As SqlConnection = New SqlConnection("server=.\SQL;database=try;Trusted_Connection=TRUE") 
    Dim cmd As SqlCommand 
    Dim cmd2 As SqlCommand 
    Dim rdr As SqlDataReader 

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

     con.Open() 
     con.Close() 

     list() 

    End Sub 
    Sub list() 
     con.Open() 


     cmd = New SqlCommand("SELECT * FROM ProductTable", con) 
     rdr = cmd.ExecuteReader 
     ListView1.Items.Clear() 
     If rdr.HasRows Then 
      Do While rdr.Read() 
       Dim arr As String() = New String(2) {} 
       Dim itm As ListViewItem 

       arr(0) = rdr("productID") 
       arr(1) = rdr("product") 
       arr(2) = rdr("price") 

       itm = New ListViewItem(arr) 
       ListView1.Items.Add(itm) 


      Loop 

     End If 

     con.Close() 
    End Sub 
    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged 
     With Me.ListView1 
      Dim i As Integer 
      For Each item As ListViewItem In ListView1.SelectedItems 
       i = item.Index 
      Next 

      Dim innercounter As Integer = 0 
      For Each subItem As ListViewItem.ListViewSubItem In ListView1.Items(i).SubItems 
       Dim myString As String = ListView1.Items(i).SubItems(innercounter).Text 
       Select Case innercounter 
        Case 2 
         txtqty.Text = 0.ToString() 
         txtqty.Text = CType(0, String) 
         txtqty.Text = CStr(0) 
         txtqty.Text = "0" 
         txtprice.Text = myString * txtqty.Text 

       End Select 
       innercounter += 1 
      Next 
     End With 
    End Sub 

End Class 
+0

集'選項嚴格On'文本控件包含文本。他們不是數字,除非你轉換成數字,否則不能相乘。文本控件通常還需要用戶輸入,這在表單加載中不會發生。與選擇ListView項目相同。這些全局數據庫提供者對象也會引起你的頭痛。閱讀[問]並參加[遊覽] – Plutonix

+0

您必須將'strings'轉換爲'integer' –

回答

0

你可以有一個Function乘值TextboxListView

Private Function calculate() As Integer 
    Dim price As Integer 
    Dim qty As Integer 
    Dim totalPrice As Integer 
    Try 
     price = CInt(ListView1.Items(ListView1.FocusedItem.Index).SubItems(2).Text) 
     qty = CInt(txtqty.Text) 
     totalPrice = price * qty 
    Catch ex As Exception 
     MsgBox("Invalid Inputs!") 
    End Try 
    Return totalPrice 
End Function 

轉換的StringsIntegers,這樣就可以相乘。 CInt()是將文本轉換爲int的代碼。圍繞它由Try Catch來處理無效輸入。

然後調用FunctionListView1.SelectedIndexChangedtxtqty_TextChanged

Private Sub txtqty_TextChanged(sender As Object, e As EventArgs) Handles txtqty.TextChanged 
    txtprice.Text = calculate().ToString 
End Sub 

Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged 
    txtprice.Text = calculate().ToString 
End Sub 
相關問題