Results 1 to 16 of 16

Thread: Please help with Storing values into Array

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    200

    Please help with Storing values into Array

    Hello, I have checked the Visual Basic libraries to see how VB declares and uses Arrays. I know how to declare and store values into the array from going through the samples they have in the sample libraries. But I am not sure if the values that I'm storing into the array are in the format that I want them to be in. The example which I have followed stores strings, but I want to store numerical values inside of my array.

    I have arranged a 3x5 array of checked boxes in my ChildForm, and I want to store the state of the checked boxes into an array: Here is the algorithm that I think would work but dont think it is very efficient. Maybe I can have this in a for loop or something that can check the status of the entire amount of the 3x5 array of checked boxes and store the values into the myArray all in one shot when I press the Finished button. I have included this code and it gives me errors, (tons of them), can anyone help... I have attached a file to give a visual of what I am planing to do. Can anyone help please


    Dim myArray As New ArrayList
    For X = 1 to 15
    if(CheckedBoxX.checked) then
    myArray.Add[X] = 1
    Else
    myArray.Add[X] = 0
    End if
    Next X



    =============Here is my code=====================
    Public Class ParentForm
    Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

    Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Dispose()
    End If
    End If
    MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents StatusBar1 As System.Windows.Forms.StatusBar
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents btnUDC_1 As System.Windows.Forms.Button
    Friend WithEvents btnUDC3 As System.Windows.Forms.Button
    Friend WithEvents btnUDC2 As System.Windows.Forms.Button
    Friend WithEvents btnUDC1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.StatusBar1 = New System.Windows.Forms.StatusBar
    Me.Panel1 = New System.Windows.Forms.Panel
    Me.btnUDC1 = New System.Windows.Forms.Button
    Me.btnUDC3 = New System.Windows.Forms.Button
    Me.btnUDC2 = New System.Windows.Forms.Button
    Me.Panel1.SuspendLayout()
    Me.SuspendLayout()
    '
    'StatusBar1
    '
    Me.StatusBar1.Location = New System.Drawing.Point(0, 416)
    Me.StatusBar1.Name = "StatusBar1"
    Me.StatusBar1.Size = New System.Drawing.Size(744, 22)
    Me.StatusBar1.TabIndex = 1
    Me.StatusBar1.Text = "Time:"
    '
    'Panel1
    '
    Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
    Me.Panel1.Controls.Add(Me.btnUDC1)
    Me.Panel1.Controls.Add(Me.btnUDC3)
    Me.Panel1.Controls.Add(Me.btnUDC2)
    Me.Panel1.Dock = System.Windows.Forms.DockStyle.Right
    Me.Panel1.Location = New System.Drawing.Point(648, 0)
    Me.Panel1.Name = "Panel1"
    Me.Panel1.Size = New System.Drawing.Size(96, 416)
    Me.Panel1.TabIndex = 4
    '
    'btnUDC1
    '
    Me.btnUDC1.Location = New System.Drawing.Point(8, 16)
    Me.btnUDC1.Name = "btnUDC1"
    Me.btnUDC1.TabIndex = 10
    Me.btnUDC1.Text = "UDC #1"
    '
    'btnUDC3
    '
    Me.btnUDC3.Location = New System.Drawing.Point(8, 96)
    Me.btnUDC3.Name = "btnUDC3"
    Me.btnUDC3.TabIndex = 2
    Me.btnUDC3.Text = "UDC #3"
    '
    'btnUDC2
    '
    Me.btnUDC2.Location = New System.Drawing.Point(8, 56)
    Me.btnUDC2.Name = "btnUDC2"
    Me.btnUDC2.TabIndex = 1
    Me.btnUDC2.Text = "UDC #2"
    '
    'ParentForm
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(744, 438)
    Me.Controls.Add(Me.Panel1)
    Me.Controls.Add(Me.StatusBar1)
    Me.IsMdiContainer = True
    Me.Name = "ParentForm"
    Me.Text = "Character Window"
    Me.Panel1.ResumeLayout(False)
    Me.ResumeLayout(False)

    End Sub

    #End Region

    Private Sub ParentForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
    Private mintNumForms As Integer = 0 'To Track the number of Windows open


    Private theKid As New ucdDialog 'saves the status of the ucdDialog window

    Private Sub btnUDC1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUDC1.Click
    Dim temp As Boolean 'Setting the temp variable to a boolean to check the status of the checked boxes
    Dim myArray As New ArrayList 'Declaring an Array that will store the state of the checked boxes


    mintNumForms = 1
    Call WriteStatus()
    Call myArray()
    With theKid
    .ShowDialog()
    .Text = "User Defined 1 " & CStr(Me.numForms)
    temp = theKid.CheckBox1.Checked
    End With


    End Sub
    Private Sub myArray() 'Storing each state of the CheckBox into myArray
    Dim myArray As New ArrayList
    Dim X As Integer

    For X = 1 To 15 'Checking the status of all the CheckBox
    If (CheckedBoxX.checked) Then
    myArray.Add([X] = 1)
    Else
    myArray.Add([X] = 0)
    End If
    Next X
    End Sub




    Friend ReadOnly Property numForms() As Integer
    Get
    Return mintNumForms
    End Get
    End Property
    Private Sub WriteStatus()
    'StatusBar1.Text = "Number of Child forms: " & mintNumForms.ToString
    End Sub

    End Class
    =======================================================
    Last edited by Srig007; Nov 23rd, 2005 at 02:48 PM.

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Please help with Storing values into Array

    Arrays are 0 based.

    You need it to be
    VB Code:
    1. For x = 0 to x = 14
    2.    'your code here
    3. Next

    Edit:
    Its an array list you don't give it an index. Just do this
    VB Code:
    1. myArray.Add(value)

  3. #3
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Please help with Storing values into Array

    You are on the right track
    VB Code:
    1. For each chbx as Checkbox in me.Controls
    2.    If chbx.Checked = True Then
    3.       MyArray.Add(1)
    4.    Else
    5.       MyArray.Add(0)
    6.    End If
    7. Next

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    200

    Re: Please help with Storing values into Array

    For an ArrayList, How do I know what is being stored where?.. what if the user wants to change the status of the checkBox to something different, will the for loop allow the data inside the array to change when the finished button is clicked? And how is an ArrayList different from a regular Array?


    Srig

  5. #5
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Please help with Storing values into Array

    ArrayLists are almost identical, they just take alot of the nitty-gritty work and handle it for you.

    VB Code:
    1. Private arrDialogBoxes(0) As Boolean    'Retains the checkbox information from the last UDCDialog
    2.     Private Sub btnUDC1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUDC1.Click
    3.         Dim temp As Boolean 'Setting the temp variable to a boolean to check the status of the checked boxes
    4.         Dim myArray As New ArrayList 'Declaring an Array that will store the state of the checked boxes
    5.  
    6.         mintNumForms = 1
    7.         Call WriteStatus()
    8.         With theKid
    9.             .ShowDialog()
    10.             .Text = "User Defined 1 " & CStr(Me.numForms)
    11.         End With
    12.  
    13.         ReDim arrDialogBoxes(0)
    14.         For Each cboHold As CheckBox In theKid.Controls
    15.             ReDim Preserve arrDialogBoxes(UBound(arrDialogBoxes) + 1)
    16.             arrDialogBoxes(UBound(arrDialogBoxes) - 1) = cboHold.Checked
    17.         Next cboHold
    18.         ReDim Preserve arrDialogBoxes(UBound(arrDialogBoxes) - 1)
    19.     End Sub
    I was already working on an array example before people started posting about lists, but here it is anyway.

    It also grabs all the checkboxes (no matter how many), instead of just the one.

  6. #6
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Please help with Storing values into Array

    In you case I would actully use the check change event of the checkboxes. And use a hashtable to store the values. That way you can use the index of the control as the key.

    VB Code:
    1. Private MyHash as New HashTable
    2.  
    3. Public Sub CheckBox1_CheckChanged(...)Handles ...
    4.    If MyHash.ContainsKey(me.Controls.IndexOf(sender) then
    5.         If ctype(sender, checkbox).Check = True Then
    6.             MyHash.Item(me.Controls.IndexOf(sender) = 1
    7.         Else
    8.             MyHash.Item(me.Controls.IndexOf(sender) = 0
    9.         End If
    10.    Else
    11.         If ctype(sender, checkbox).Check = True Then
    12.             MyHash.Add((me.Controls.IndexOf(sender),  1)
    13.         Else
    14.             MyHash.Add((me.Controls.IndexOf(sender), 0)
    15.         End If
    16.    End If
    17. End Sub

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    200

    Re: Please help with Storing values into Array

    And lets say I want to display the contents of the array, I would do something like this. Since I am a newbie to the VB.Net Framework, please correct me if I am wrong..

    txtShowArray.Text = Cstr(arrDialogBoxes)



    Srig

  8. #8
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: Please help with Storing values into Array

    You have to itterate through the array.
    VB Code:
    1. Dim temp as string
    2.  
    3. For cnt as integer = 0 to arrDialogBoxes.Count-1
    4.    temp &= cstr(arrDialogBoxes(cnt))
    5. Next
    6.  
    7. txtShow.Text = temp

  9. #9
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Please help with Storing values into Array

    Quote Originally Posted by Srig007
    And lets say I want to display the contents of the array, I would do something like this. Since I am a newbie to the VB.Net Framework, please correct me if I am wrong..

    txtShowArray.Text = Cstr(arrDialogBoxes)



    Srig
    It's an array, so you would have to specify which item:

    txtShowItem1.Text = arrDialogBoxes(0).ToString
    txtShowItem2.Text = arrDialogBoxes(1).ToString
    txtShowItem3.Text = arrDialogBoxes(2).ToString
    txtShowItem4.Text = arrDialogBoxes(3).ToString
    etc....

    If you wanted to dump all the items into that box (ie. Text shows: "TrueFalseFalseFalseTrue..."); you could do something like this:

    for intCnt = 0 to Ubound(arrDialogBoxes)-1
    txtShowArray.Text += arrDialogBoxes(intCnt).ToString
    next intCnt

    If you wanted to make them manageable (by that, I mean; find out which boolean value was associated with what control in the dialogbox), you would have to use a 2 dimensional array.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    200

    Re: Please help with Storing values into Array

    when I press the Finished Button on the ChildForm. it gives an exception and stops executing the code, and then highlights where the start of the error is located. The error that VB.net points to the following

    For Each cboHold As CheckBox In theKid.Controls


    i dont know why?

  11. #11
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Please help with Storing values into Array

    Quote Originally Posted by Srig007
    when I press the Finished Button on the ChildForm. it gives an exception and stops executing the code, and then highlights where the start of the error is located. The error that VB.net points to the following

    For Each cboHold As CheckBox In theKid.Controls


    i dont know why?
    Try checkboxes full name:

    System.Windows.Forms.CheckBox

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    200

    Re: Please help with Storing values into Array

    Here is what I have in the code and it still gives me errors at the same line:


    Private Sub btnUDC1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUDC1.Click
    ' Dim temp As Boolean 'Setting the temp variable to a boolean to check the status of the checked boxes
    Dim myArray As New ArrayList 'Declaring an Array that will store the state of the checked boxes

    mintNumForms = 1
    Call WriteStatus()
    ' Call myArray()
    With theKid
    .ShowDialog()
    .Text = "User Defined 1 " & CStr(Me.numForms)
    'temp = theKid.CheckBox1.Checked
    End With

    '=========================================================================='
    'Stores the Status of the Checked Boxes into an array -> arrDialogBoxes
    ReDim arrDialogBoxes(0)
    For Each cboHold As System.Windows.Forms.CheckBox In theKid.Controls
    ReDim Preserve arrDialogBoxes(UBound(arrDialogBoxes) + 1)
    arrDialogBoxes(UBound(arrDialogBoxes) - 1) = cboHold.Checked
    Next cboHold
    ReDim Preserve arrDialogBoxes(UBound(arrDialogBoxes) - 1)
    '=========================================================================='

    End Sub

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Please help with Storing values into Array

    I might guess that "theKid" does not have an exposable Controls collection.

    What is the exact error message?

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Nov 2005
    Posts
    200

    Re: Please help with Storing values into Array

    Here is the exact error message:

    An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication2.exe

    Additional information: Specified cast is not valid.

    And after it displays the error message it highlights this line:

    For Each cboHold As System.Windows.Forms.CheckBox In theKid.Controls

  15. #15
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Please help with Storing values into Array

    do you have other controls on the UDCdialogbox besides checkboxes?

  16. #16
    Addicted Member
    Join Date
    Jan 2005
    Location
    Quagmire of programming
    Posts
    165

    Re: Please help with Storing values into Array

    Quote Originally Posted by Srig007
    Here is the exact error message:

    An unhandled exception of type 'System.InvalidCastException' occurred in WindowsApplication2.exe

    Additional information: Specified cast is not valid.

    And after it displays the error message it highlights this line:

    For Each cboHold As System.Windows.Forms.CheckBox In theKid.Controls
    If 'theKid' is a form, try this:
    VB Code:
    1. For Each ctrl as Control in theKid.Controls
    2.     If (ctrl.GetType.ToString = "System.Windows.Forms.CheckBox") Then
    3.         ....
    4.     End If
    5. Next
    Kyjan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width