dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: check box array

  1. #1
    Join Date
    Sep 2004
    Posts
    45

    check box array

    I created a check box array, (chkPlan(), 0 to 3). Each check box has a dollar value assigned in a text file that is opened for input as 1. When I check each box the running total is displayed in a label. If I uncheck a box that value is added to the total again instead of subtracted. If I check then uncheck all four boxes the total is doubled instead of being reduced to zero. The running total variable is named sum and initialized to zero. Any suggestions greatly appreciated.

  2. #2
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Code:
    Private Sub chkPlan_Click(Index As Integer)
      Static Total As Currency
      If chkPlan(Index).Value = 1 Then 
        Total = Total + FileAmount(Index)
      Else 
        Total = Total - FileAmount(Index)
      End If
      lblTotalPlan.Caption = Total
    End Sub

  3. #3
    Join Date
    Sep 2004
    Posts
    45
    Thanks for responding Ron. The code you sent is almost identical to the code I used. The results are the same: I check a box, the running total shows, I uncheck the box, the total is doubled.
    Here's what I wrote:

    Option Explicit

    Dim price(0 To 3) As Single
    Dim sum As Single


    Private Sub chkPlan_Click(Index As Integer)
    Static sum As Currency
    If chkPlan(Index).Value = 1 Then
    sum = sum + price(Index)
    Else
    sum = sum + price(Index)
    End If

    lblAmount.Caption = Format$(sum, "currency")

    End Sub

    Private Sub Form_Load()
    Dim i As Integer
    Dim plan As String
    Dim cost As Single

    Open "C:\VB\Experiments\Check Box Example\Benefits.txt" For Input As #1
    For i = 0 To 3
    Input #1, plan, cost
    price(i) = cost
    chkPlan(i).Caption = plan + "(" + Format$(cost, "currency") + ")"
    Next i
    Close 1
    sum = 0
    End Sub

  4. #4
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    Yes, but you will notice that mine works and yours does not.
    Mine adds the price in when it is checked, and subtracts the
    price when it is not. Yours adds the price in no matter what.
    Here is Your code And Mine....
    Code:
    Yours:
      If chkPlan(Index).Value = 1 Then
        sum = sum + price(Index)
      Else
        sum = sum + price(Index)
      End If
    Mine:
      If chkPlan(Index).Value = 1 Then 
        Total = Total + FileAmount(Index)
      Else 
        Total = Total - FileAmount(Index)
      End If

  5. #5
    Join Date
    Sep 2004
    Posts
    45

    check box array

    Wow is my face red! I blame that oversight on my glasses or the weather. Every time I pride myself on my attention to detail, pride backfires on me. (Perhaps that's an example of the reason pride is among the 7 deadly sins?) Thanks very much for your help Ron. Have a great weekend.

  6. #6
    Join Date
    Jul 2007
    Posts
    4

    checkbox.

    hai

    Thanks for ur replay.
    how u declare the checkbox array?
    bcoz when i use ur coding it show the error in declaration.
    pls tell me detail codings with explanation.
    pls urgent.
    thank u.

  7. #7
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    A control array is simply a set of identical type of controls, like text boxes, labels, etc. To create an array of check boxes, give them the identical name, and set their index property to a unique value, like 1, 2, 3,... The simplest way to do this is to create the first control and then just copy and paste it. Each time you do VB will ask you if you want this to be a control array. Just Click yes and it will give the new control the same name as the original but automatically increment the Index property. If you want to add another one later just copy one and paste it again.

  8. #8
    Join Date
    Jul 2007
    Posts
    4

    Cool hiiiiiii

    Quote Originally Posted by Ron Weller
    A control array is simply a set of identical type of controls, like text boxes, labels, etc. To create an array of check boxes, give them the identical name, and set their index property to a unique value, like 1, 2, 3,... The simplest way to do this is to create the first control and then just copy and paste it. Each time you do VB will ask you if you want this to be a control array. Just Click yes and it will give the new control the same name as the original but automatically increment the Index property. If you want to add another one later just copy one and paste it again.
    hi

    I want to create in vb.net2005 through coding

  9. #9
    Join Date
    Aug 2004
    Location
    Orange, California
    Posts
    1,263
    I see. But this forum is for VB6 or older. You need to use the .NET forum. Someone there will know how to do this in .NET.

  10. #10
    Join Date
    Mar 2005
    Location
    USA
    Posts
    1

    Exclamation Else/+?

    Your ELSE statement should subtract (NOT ADD) the value of the INDEX from the total - you have both the CHECKING and UNCHECKING routines ADDING to the total. I made one small change - changing the + to a - in the ELSE statement. Hope this helps!

    Alienwebmaster
    Quote Originally Posted by wisgro
    Private Sub chkPlan_Click(Index As Integer)
    Static sum As Currency
    If chkPlan(Index).Value = 1 Then
    sum = sum + price(Index)
    Else
    sum = sum - price(Index)
    End If

    lblAmount.Caption = Format$(sum, "currency")

    End Sub

    Private Sub Form_Load()
    Dim i As Integer
    Dim plan As String
    Dim cost As Single

    Open "C:\VB\Experiments\Check Box Example\Benefits.txt" For Input As #1
    For i = 0 To 3
    Input #1, plan, cost
    price(i) = cost
    chkPlan(i).Caption = plan + "(" + Format$(cost, "currency") + ")"
    Next i
    Close 1
    sum = 0
    End Sub

Similar Threads

  1. Replies: 1
    Last Post: 04-23-2006, 10:18 AM
  2. Check combinations in an array
    By a7n9 in forum VB Classic
    Replies: 4
    Last Post: 02-10-2006, 02:27 PM
  3. Replies: 0
    Last Post: 07-14-2001, 10:55 AM
  4. Check box isssue when bound to a database column
    By Narayan in forum VB Classic
    Replies: 0
    Last Post: 04-10-2001, 10:30 AM
  5. Radio Button, Check Box
    By Eric Kline in forum XML
    Replies: 0
    Last Post: 03-31-2000, 10:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center