|
-
Nov 23rd, 2005, 01:29 PM
#1
Thread Starter
Addicted Member
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.
-
Nov 23rd, 2005, 01:38 PM
#2
Frenzied Member
Re: Please help with Storing values into Array
Arrays are 0 based.
You need it to be
VB Code:
For x = 0 to x = 14
'your code here
Next
Edit:
Its an array list you don't give it an index. Just do this
-
Nov 23rd, 2005, 01:43 PM
#3
Frenzied Member
Re: Please help with Storing values into Array
You are on the right track
VB Code:
For each chbx as Checkbox in me.Controls
If chbx.Checked = True Then
MyArray.Add(1)
Else
MyArray.Add(0)
End If
Next
-
Nov 23rd, 2005, 01:45 PM
#4
Thread Starter
Addicted Member
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
-
Nov 23rd, 2005, 01:50 PM
#5
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:
Private arrDialogBoxes(0) As Boolean 'Retains the checkbox information from the last UDCDialog
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()
With theKid
.ShowDialog()
.Text = "User Defined 1 " & CStr(Me.numForms)
End With
ReDim arrDialogBoxes(0)
For Each cboHold As 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
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.
-
Nov 23rd, 2005, 01:56 PM
#6
Frenzied Member
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:
Private MyHash as New HashTable
Public Sub CheckBox1_CheckChanged(...)Handles ...
If MyHash.ContainsKey(me.Controls.IndexOf(sender) then
If ctype(sender, checkbox).Check = True Then
MyHash.Item(me.Controls.IndexOf(sender) = 1
Else
MyHash.Item(me.Controls.IndexOf(sender) = 0
End If
Else
If ctype(sender, checkbox).Check = True Then
MyHash.Add((me.Controls.IndexOf(sender), 1)
Else
MyHash.Add((me.Controls.IndexOf(sender), 0)
End If
End If
End Sub
-
Nov 23rd, 2005, 02:12 PM
#7
Thread Starter
Addicted Member
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
-
Nov 23rd, 2005, 02:14 PM
#8
Frenzied Member
Re: Please help with Storing values into Array
You have to itterate through the array.
VB Code:
Dim temp as string
For cnt as integer = 0 to arrDialogBoxes.Count-1
temp &= cstr(arrDialogBoxes(cnt))
Next
txtShow.Text = temp
-
Nov 23rd, 2005, 02:18 PM
#9
Re: Please help with Storing values into Array
 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.
-
Nov 23rd, 2005, 02:33 PM
#10
Thread Starter
Addicted Member
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?
-
Nov 23rd, 2005, 02:35 PM
#11
Re: Please help with Storing values into Array
 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
-
Nov 23rd, 2005, 02:39 PM
#12
Thread Starter
Addicted Member
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
-
Nov 23rd, 2005, 02:51 PM
#13
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?
-
Nov 23rd, 2005, 03:00 PM
#14
Thread Starter
Addicted Member
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
-
Nov 23rd, 2005, 03:17 PM
#15
Re: Please help with Storing values into Array
do you have other controls on the UDCdialogbox besides checkboxes?
-
Nov 23rd, 2005, 03:50 PM
#16
Addicted Member
Re: Please help with Storing values into Array
 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:
For Each ctrl as Control in theKid.Controls
If (ctrl.GetType.ToString = "System.Windows.Forms.CheckBox") Then
....
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|