需要使用复选框的帮助

发布于 2024-11-19 08:47:15 字数 277 浏览 2 评论 0原文

我对我的基本项目有几个问题,

如果我有一个复选框,当它被选中时,它将进入一个显示价格的文本框,当我取消选中它时,我的价格仍然显示在该文本框中,我怎样才能当我取消选中该框时使其消失?

Dim total As Double
If rb_s1.Checked = True Then
    total += 650.0

txt_1.Text = total

这就是我的代码。

我有很多组合框,当我选中/取消选中它们时,如何使它们全部加起来。

i have a couple of questions about my basic project

if i have a check box and when its checked, it is going to a text box which is displaying the price, when i uncheck it my price still says in that text box, how can i make it dissapear as i uncheck the box?

Dim total As Double
If rb_s1.Checked = True Then
    total += 650.0

txt_1.Text = total

thats my code.

and i have many combo boxes, how can i make them all add up as i check/uncheck them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

也只是曾经 2024-11-26 08:47:15

我会将此功能添加到 CheckBox_Changed 事件处理程序中。这样您就可以判断它是未选中还是已选中,并从价格中添加或减去该值。

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked Then
        total += 650.00
    Else
        total -= 650.00
    End If

    TextBox1.Text = total.ToString()
End Sub 

I would add this functionality into the CheckBox_Changed event handler. This way you can tell if it is unchecked or checked and add or subtract the value from price.

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If CheckBox1.Checked Then
        total += 650.00
    Else
        total -= 650.00
    End If

    TextBox1.Text = total.ToString()
End Sub 
要走就滚别墨迹 2024-11-26 08:47:15

您必须使用复选框的 Checked_Changed 事件。

SHARED void CheckBox1_CheckedChanged(object sender, EventArgs e)
     IF ChkBx.Checked = true then
      textBox1.text = "1500"
     else
     textBox1.text = "" 
     END IF

END SUB

You have to use Checked_Changed event of checkbox.

SHARED void CheckBox1_CheckedChanged(object sender, EventArgs e)
     IF ChkBx.Checked = true then
      textBox1.text = "1500"
     else
     textBox1.text = "" 
     END IF

END SUB
唔猫 2024-11-26 08:47:15

要在复选框状态更改时更改显示的文本,您需要处理 CheckedChanged 事件。在 Visual Studio 中,当窗体/控件处于 Desginer 模式时,您可以选择复选框控件,然后在“属性”窗口中选择“事件”选项卡(带有小闪电图标的选项卡),然后双击要存根的 CheckChanged 事件在事件处理程序方法中并将事件附加到处理程序。

ETA:我重读了这篇文章,我不确定我是否清楚。当我提到在事件处理程序中存根并将事件附加到处理程序时,我的意思是在设计器中双击事件的路线将为您完成此操作。

顺便说一句,听起来您希望文本仅是已检查项目的总和,因此从体系结构的角度来看,我建议创建一个方法来确定总和,并让所有复选框检查事件调用该方法而不是试图让事件处理程序方法本身直接做太多事情(也许您已经很清楚了)。

所以你可能会这样做:

Public Class Form1

    Private Sub DisplayTotal()
        Dim total As Decimal = 0

        If (CheckBox1.Checked) Then
            total += Decimal.Parse(txtItem1.Text)
        End If

        'Add other items

        txtTotal.Text = total

        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
        DisplayTotal()
    End Sub

    Private Sub CheckBox2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
        DisplayTotal()
    End Sub

End Class

To get your displayed text to change when the state of your checkbox changes, you'll need to handle the CheckedChanged event. In Visual Studio while in Desginer mode for your form/control, you can select the check box control, and then in the Properties window, select the Events tab (the one with the little lightingbolt icon), and double click the CheckChanged event to stub in an event handler method AND attach the event to the handler.

ETA: I re-reading this, I'm not sure how clear I was. When I mentioned stubbing in the event handler and attaching the event to the handler, I meant that going the route of double-clicking the event in the designer will do this for you.

As an aside, it sounds like you want the text to be a sum of only the checked items, so from an architechtueral sense, I would recommend creating a single method to determine the sum, and have all check-box check events invoke that method rather than trying to make the event handler method itself do too much directly (maybe that was already clear to you).

So you might do something like this:

Public Class Form1

    Private Sub DisplayTotal()
        Dim total As Decimal = 0

        If (CheckBox1.Checked) Then
            total += Decimal.Parse(txtItem1.Text)
        End If

        'Add other items

        txtTotal.Text = total

        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
        DisplayTotal()
    End Sub

    Private Sub CheckBox2_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBox1.CheckedChanged
        DisplayTotal()
    End Sub

End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文