I know that control arrays are not in VB Express 2008.
Here is what I'm trying to do and how I want to go about it:
The program is to solve a Sudoku puzzle. (So, I plan on setting up 81 labels (9 rows by 9 cols)
2 arrays in the program: entryx(81) as integer (each block in the sudoku puzzle containing the correct number for that block, will contain 0 until known) poss(81,9) as boolean (each block,possible numbers in that block 1-9)
The program will be looking thru the arrays and updating the array when it determines what each block has to contain or the possibilities.
My problem is: Is there a way to update the text in say in the 2nd label in row 2 with the corresponding entryx(10) and going the other way moving the value of a label clicked on into the corresponding entryx. (label15 for example into entryx(14)). | | Marrelli Friday, October 16, 2009 7:49 PM | Solitaire, I took your solution and modified it. Your code was a solution on how to update my labels from the arrary but not the other way. Ie. For the label.click routine, I could updatethelabel but not know what array entry. Your mention of tag=1 became the solution. At design time I will give each label a tag of 1 to 81 to be used as the index to both the label array and entryx array. Here is the code with 9 labels as a test. Button1 & Button2 just used for testing displaying my entryx array and to make sure the array and the labels matched.
Thanks Jeff & Solitaire, Marrelli
Public Class Form1
Private LblArray(9) As Label
Private entryx(81) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim count As Integer
Dim lbl As Label
For Each ctl As Control In Controls
If TypeOf ctl Is Label Then
lbl = CType(ctl, Label)
count = CType(lbl.Tag, Integer)
If count > 0 Then
LblArray(count) = lbl
End If
End If
Next
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click, Label2.Click, ... <br/> Dim cnt As Integer
sender.Text = TextBox1.Text
cnt = CType(sender.tag, Integer)
entryx(cnt) = TextBox1.Text
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cnt As Integer
For cnt = 1 To 9
MsgBox(entryx(cnt))
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cnt As Integer
For cnt = 1 To 9
entryx(cnt) = CStr(cnt + 2)
LblArray(cnt).Text = entryx(cnt)
Next
End Sub
End Class
- Marked As Answer byMarrelli Saturday, October 17, 2009 4:58 PM
-
| | Marrelli Saturday, October 17, 2009 4:53 PM | this example may be of some use to you. not exactly the answer to your question but may help you look at some options to use a control array.
Dim l(4) As Label
For i As Integer = 0 To l.Length - 1
Static lft As Integer = 0
lft += 25
Dim lbl As New Label
lbl.Left = lft
lbl.Text = i.ToString
l(i) = lbl
Me.Controls.Add(lbl)
lbl.BringToFront()
Next
FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial | | Jeff - www.SRSoft.us Friday, October 16, 2009 8:30 PM | You can create a control array using the correct code. Here is an example with a group of textboxes. Apply it to the labels in your program:
Public Class Form1
Private TextArray(5) As TextBox 'or 4 starting with 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim count As Integer = 5 'or 4 to go from 0 to 4
Dim txt As TextBox
For Each ctl As Control In Controls
If TypeOf ctl Is TextBox Then
txt = CType(ctl, TextBox)
'If CType(txt.Tag, Integer) = 1 Then 'Use only if textboxes are Tagged
TextArray(count) = txt
count -= 1
'End If
End If
Next
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For x As Integer = 1 To 5 'or 0 to 4
'will also work with 0 To 5 if 6 textboxes used
TextArray(x).Text = x.ToString 'or TextArray(x).Text = (x + 1).ToString
Next
End Sub
End Class
Solitaire | | Solitaire Friday, October 16, 2009 9:09 PM | I was going to build the labels at design time.
In Jeff's reply, I'm taking it I could then reference the label as l(x).text = itemx(x).ToString?
or
In Solitaire's TextArray(x).text = itemx(x).ToString?
Another question Solitaire. When you built the array, you counted backward instead of forward.
When I design the form and add the labels left to right (9 labels), then create the second row left to right etc:
In the "For Each ctl" loop, does it do them in LIFO (last In First Out) order?
Marrelli | | Marrelli Friday, October 16, 2009 11:27 PM | You will have to try the sample for yourself. When I made it, it created the array indexes in reverse order so I had to count backwards. Leave out the count to see how it works for you.
Solitaire | | Solitaire Saturday, October 17, 2009 12:56 AM | sometimes what happens is when you add controls from the toolbox or copy and paste them either by themselves or in a group, they order of the controls are not necessarily left to right or up to down. so say if you drag 5 labels to the form each from the toolbox. then you copy and paste all 5 again, the order may be from right to left rather than left to right. i had a similar issue before where i added 12 buttons on a form from left to right and when i copied and pasted them for another row below, the order was backwards. why i don't know, i just let it be. so if you loop through your controls using for each ctl as control in me.controls you will find that the order may be different than what you see. it all depends on how and what order you add the controls. in an application i needed to check values in textboxes after the data was loaded and before allowing an update to the data i needed to check first if they made any changes. if not then there was need toperform an update with the same data. so i looped through each control and combined the data as a string. then did the same before the update and if the 2 strings did not match then there were changes. checking the data in the strings, the order it collected the data was not in the order the controls were on the form. the order was always consistent but did not match the design. so using a control array is good and may be easier sometimes to create your controls in code as well to be sure it all matches up. but you can also use tags for the controls or you can be careful about the order you add them to the form. fi you play with adding the controls in code it may save you grief later on. just depends on what works best for you. in the end that is what is important. FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial | | Jeff - www.SRSoft.us Saturday, October 17, 2009 4:51 PM | Solitaire, I took your solution and modified it. Your code was a solution on how to update my labels from the arrary but not the other way. Ie. For the label.click routine, I could updatethelabel but not know what array entry. Your mention of tag=1 became the solution. At design time I will give each label a tag of 1 to 81 to be used as the index to both the label array and entryx array. Here is the code with 9 labels as a test. Button1 & Button2 just used for testing displaying my entryx array and to make sure the array and the labels matched.
Thanks Jeff & Solitaire, Marrelli
Public Class Form1
Private LblArray(9) As Label
Private entryx(81) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim count As Integer
Dim lbl As Label
For Each ctl As Control In Controls
If TypeOf ctl Is Label Then
lbl = CType(ctl, Label)
count = CType(lbl.Tag, Integer)
If count > 0 Then
LblArray(count) = lbl
End If
End If
Next
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click, Label2.Click, ... <br/> Dim cnt As Integer
sender.Text = TextBox1.Text
cnt = CType(sender.tag, Integer)
entryx(cnt) = TextBox1.Text
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cnt As Integer
For cnt = 1 To 9
MsgBox(entryx(cnt))
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim cnt As Integer
For cnt = 1 To 9
entryx(cnt) = CStr(cnt + 2)
LblArray(cnt).Text = entryx(cnt)
Next
End Sub
End Class
- Marked As Answer byMarrelli Saturday, October 17, 2009 4:58 PM
-
| | Marrelli Saturday, October 17, 2009 4:53 PM | Jeff,
I saw your last post after I posted mine. Yes, upon testing the order was random. I made copies of labels etc. dragiing etc,
Using the tags as the index works well.
Tanks again | | Marrelli Saturday, October 17, 2009 5:02 PM | something else that may help you is if you add a handler to all your labels that way you don't have to add the handles clause for all those labels. in your for each loop add something like this
AddHandler ctl.Click, AddressOf yourclicksub
Private Sub yourclicksub(ByVal sender As Object, ByVal e As System.EventArgs)
Dim sendinglbl Aslabel = CType(CObj(sender), label)
End Sub
if you add the addhandler line in the loop of the controls you will add the yoursubclick event sub for each one. will save you some code writing.
FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial | | Jeff - www.SRSoft.us Saturday, October 17, 2009 5:14 PM | Thanks Jeff,
I added the handler to the loop and dropped the handles clause from the click sub and it worked. It will be a lot less coding especiaally when it will be 81 labels instead of 9.
Thanks,
Marrelli | | Marrelli Saturday, October 17, 2009 5:53 PM |
|