【发布时间】:2022-01-02 21:29:36
【问题描述】:
在@Andrew Morton 回答了我之前的问题之后,我还有一个问题:)
这是我的全部代码(暂时不长):
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Sql
Public Class Form1
Sub PopulateCB()
Dim connection As String = "Data Source=.\SQLEXPRESS;Initial Catalog=OST;Integrated Security=True"
Dim sql = "SELECT * FROM liste_unités"
Dim dt As New DataTable
Using conn As New SqlConnection(connection),
da As New SqlDataAdapter(sql, conn)
da.Fill(dt)
End Using
ComboBoxC1L1.DataSource = dt
ComboBoxC1L1.DisplayMember = "nom_unité"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PopulateCB()
End Sub
Private Sub ComboBoxC1L1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxC1L1.SelectedIndexChanged
Dim cb = DirectCast(sender, ComboBox)
If cb.SelectedIndex >= 0 Then
Dim val = DirectCast(cb.SelectedItem, DataRowView).Row.Field(Of Integer)("cout_unité")
If ComboBoxQC1L1.Text = "ordinaire" Then
LabelPointsC1L1.Text = val
ElseIf ComboBoxQC1L1.Text = "médiocre" Then
LabelPointsC1L1.Text = val - 2
ElseIf ComboBoxQC1L1.Text = "élite" Then
LabelPointsC1L1.Text = val + 2
End If
If cb.SelectedIndex >= 0 Then
Dim val2 = DirectCast(cb.SelectedItem, DataRowView).Row.Field(Of String)("type_unité")
LabelUnitType.Text = val2
End If
End If
Try
Dim totalC1L1 As Integer
totalC1L1 = CInt(TextBoxC1L1.Text) * CInt(LabelPointsC1L1.Text)
LabelTotalC1L1.Text = totalC1L1
Catch ex As Exception
End Try
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBoxQC1L1.Text = "ordinaire"
End Sub
Private Sub TextBoxC1L1_TextChanged(sender As Object, e As EventArgs) Handles TextBoxC1L1.TextChanged
Try
Dim totalC1L1 As Integer
totalC1L1 = CInt(TextBoxC1L1.Text) * CInt(LabelPointsC1L1.Text)
LabelTotalC1L1.Text = totalC1L1
Catch ex As exception
End Try
End Sub
End Class
Here is the program interface when the Button has been clicked
Red Arrow ComboBox 文本是一个 DropDownStyle 框,有 3 种可能的文本选择:
ordinaire,
élite,
médiocre
我想要做什么:更改红色箭头组合框文本时,cout_unité 标签也应更改为“médiocre”组合框文本的“cout_unité -2”,或“élite”的“cout_unité +2” " ComboBox 文本或保持 = 到 "cout_unité" 如果所选文本是 "ordinaire"。
并且它应该只从表中的原始“cout_unité”值计算一次(如果在“ordinaire”上单击 10 次,它不应该将 10 * 2 减去“cout_unité”值,只有 1 * 2)
我可以在 ComboBoxC1L1 中执行此操作(请参阅代码),但我无法使用此红色箭头组合框重现它(可能是因为此组合框中的数据类型是“字符串”,我不知道)。
非常感谢:)
【问题讨论】:
-
“可能是因为这个组合框中的数据类型是“字符串”,我不知道” - 设置断点并找出?
-
现在和永远开启 Option Strict。项目属性 -> 编译选项卡。也适用于未来的项目工具 -> 选项 -> 项目和解决方案 -> VB 默认值更正它指出的任何错误。
-
@Mary:好的。谢谢你。 Dale K:即使我指出了这一点,我也无法纠正它。这就是为什么我要求一些专业知识。
-
不要写空的 Catch 块。他们吞下错误。
标签: sql-server vb.net if-statement combobox label