开发者

Need help using Checkbox

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.


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 


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜