Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Minus Value When Checkbox de-selected

Status
Not open for further replies.

smchoudhury

Programmer
Feb 18, 2009
10
GB
Hi there,
At present i add 50 to any textbox value when a checkbox is selected. What i want is if someone selects a checkbox then wishes to de-select it; the value which was added earlier to be minused from the value inside the textbox.

Present scenario
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

If CheckBox1.Checked Then
TextBox1.Text = TextBox1.Text + (ComboBox1.SelectedItem * 0.5)
End If

End Sub

What i want:
if a user de-selects that cehcekbox the calculation should work out:
TextBox1.Text = TextBox1.Text - (ComboBox1.SelectedItem * 0.5).

I'm not sure how to specify the de-select part.
thanks,
Mahmud
 
If you're looking to subtract a value based on the current combobox selection, it's a simple matter of subtracting in the If statement's Else clause:
Code:
If CheckBox1.Checked Then
    TextBox1.Text = TextBox1.Text + (ComboBox1.SelectedItem * 0.5)
Else
    TextBox1.Text = TextBox1.Text - (ComboBox1.SelectedItem * 0.5)
End If

If however you want to subtract the value that was added even if the user has changed the combobox in the meantime, you can save the amount that was added in CheckBox1's Tag property, then retrieve that value when the checkbox is unchecked:
Code:
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    Dim ComboBox1Value As Double
    Dim TextBox1Value As Double

    If (Not Double.TryParse(ComboBox1.SelectedItem * 0.5, ComboBox1Value)) Then Exit Sub
    If (Not Double.TryParse(TextBox1.Text, TextBox1Value)) Then Exit Sub

    If CheckBox1.Checked Then
        ComboBox1.Tag = ComboBox1Value
        TextBox1Value += ComboBox1Value
    Else
        ComboBox1Value = Double.Parse(ComboBox1.Tag)
        TextBox1Value -= ComboBox1Value
    End If

    TextBox1.Text = TextBox1Value.ToString

End Sub
 
With the above code, i keep on getting this eeror for some reasons:
Argument not specified for parameter 'provider' of 'Public Shared Overloads Function TryParse(s As String, style As System.Globalization.NumberStyles, provider As System.IFormatProvider, ByRef result As Double) As Boolean'.

its to do with TryParse method - any idea how i could solve the problem.

M
 
The TryParse method was just some rudimentary error checking I added that has nothing to do with solving your original question.

Upgrade to .NET 2.0 or type in the statement by hand to see if the TryParse method is available and if so, what overloads it supports. Otherwise, figure out another way to convert your combo and text boxes to Doubles.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top