【问题标题】:VB.NET add or subtract an integer value to a SQL Server value into a label when selecting a combobox itemVB.NET 在选择组合框项时将整数值添加或减去 SQL Server 值到标签中
【发布时间】: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

Here is the SQL table look

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


【解决方案1】:

由于只有一个Handles 子句,因此不需要以下行。 sender 只能是Handles 中的ComboBox

Dim cb = DirectCast(sender, ComboBox)

如果在PopulateCB方法中设置combo的ValueMember,可以节省一长行代码,使代码更具可读性。

Dim val = DirectCast(cb.SelectedItem, DataRowView).Row.Field(Of Integer)("cout_unité")

收件人:

Dim val = CInt(ComboBoxC1L1.SelectedValue)

我们需要 CInt,因为 SelectedValue 是一个对象。

在设置DisplayMemberValueMember 之前不要分配DataSource

您检查了两次ComboBoxC1L1.SelectedIndex >= 0。 只需在第一个 If 中包含单位类型即可。

如果已经选择了正确的值,用户可能不必触发SelectedIndexChanged 事件。也许单击按钮会更好。

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.DisplayMember = "nom_unité"
    ComboBoxC1L1.ValueMember = "cout_unité"
    ComboBoxC1L1.DataSource = dt
End Sub

Private Sub btnCalculateValue_Click(sender As Object, e As EventArgs) Handles btnCalculateValue.Click
    If ComboBoxC1L1.SelectedIndex >= 0 Then
        Dim val = CInt(ComboBoxC1L1.SelectedValue)
        If ComboBoxQC1L1.Text = "ordinaire" Then
            LabelPointsC1L1.Text = val.ToString
        ElseIf ComboBoxQC1L1.Text = "médiocre" Then
            LabelPointsC1L1.Text = (val - 2).ToString
        ElseIf ComboBoxQC1L1.Text = "élite" Then
            LabelPointsC1L1.Text = (val + 2).ToString
        End If
        Dim val2 = DirectCast(ComboBoxC1L1.SelectedItem, DataRowView).Row.Field(Of String)("type_unité")
        LabelUnitType.Text = val2
    End If
    Dim totalC1L1 As Integer
    totalC1L1 = CInt(TextBoxC1L1.Text) * CInt(LabelPointsC1L1.Text)
    LabelTotalC1L1.Text = totalC1L1.ToString
End Sub

【讨论】:

  • 非常感谢 Mary 的指导性意见!我会测试的!干杯!
  • 好的,我已经测试了代码,我遇到了 2 个问题。第一个是 ComboBoxC1L1.ValueMember = "cout_unité" 和 Dim val = CInt(ComboBoxC1L1.SelectedValue)。在 ComboBox C1L1 中,没有“cout_unité”,只有“nom_unité”。 “cout_unité”应该出现在 LabelPointsC1L1 中。我已将 Dim val = CInt(ComboBoxC1L1.SelectedValue) 行替换为 Dim val = CInt(LabelPointsC1L1.Text) 并且它正在工作。但是现在,我面临的第二个问题(我之前遇到过这个问题)是:每次点击“计算”时,它都会完成整个过程。这个想法是从表值中只计算一次 +2/-2。
  • 示例:对于“5”值,最大值和最小值应为 3 和 7。每次单击“计算”时不应计算。我不知道是否清楚:D
猜你喜欢
  • 1970-01-01
  • 2019-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多