Monte Cristo33

Dear all,

Having recently migrated from VB6 to VB2005, I have come to a screeching halt, because control arrays are no longer available and I do not quite understand the new event handler.

I'm trying to build a 7-segment LCD display on a form. I have downloaded & installed the new drawing shapes controls form power pack 2.0. I have 7 lines on my form and want to loop through to display one line at a time.

The old VB6 code attached to a button control using a control array would look something like this:

Dim N as integer

For N = 0 to 6

Line1(N).visible = not Line1(N).visible

next N

of course, that doesn't work anymore.

What is the new 2005 equivalent if I want to iterate through a series of lines (from LineShape0 to LineShape6) at each stroke of my command button

Any help greatly appreciated as the VB online documentation did not really shed any light.

A massive thank you to whoever might be able to help.

MC



Re: Visual Basic Express Edition moving from VB6 Control arrays and using event handler in VB 2005

Dave299

For controls you normally have at least a couple of options, either use the Controls Collection or add the controls to an array or a list. With these new shapes you don't have the first option as they do not form part of the controls collection. They are part of the ShapeContainer shapes collection but that cannot be indexed. So adding them to an array or list seems to be the best option. Try something like this:

Imports Microsoft.VisualBasic.PowerPacks

Public Class Form1

Private Shapes As New List(Of Shape)

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

For Each Shape As Shape In ShapeContainer1.Shapes

If TypeOf Shape Is LineShape Then

Shapes.Add(Shape)

End If

Next

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Shapes(3).BorderColor = Color.Red

End Sub

End Class





Re: Visual Basic Express Edition moving from VB6 Control arrays and using event handler in VB 2005

Monte Cristo33

Hey Dave,

I tried the code above, however it's generating too many errors for me to handle. I can't believe what used to be so simple in the older version has now become completely cryptic!

I think I need a change of strategy: could I use preset pictures of 7-segment Digits and form numbers with those.

The question is: I have no idea whatsoever how to form a Controls collection or create a controls array with VB 2005.

Say I've got 7 pictureBoxes on my form named picDigit0, picDigit1, etc... how would I create a controls collection or a control array with these

All I want to do at that stage is have a form with one button, and each time the button is pressed, the digit being displayed is incremented: 0,1,2,3, etc...

So I guess using a loop and either a control collection or a control array would replicate what I could so easily do in the previous version.

I would be very grateful if you could shed some light on this.

Thanks

MC





Re: Visual Basic Express Edition moving from VB6 Control arrays and using event handler in VB 2005

JohnWein

This thread shows a way to make a Control Array in VB.Net. Doing it with PowerPacks Shapes is a little more complex:

Code Block

Public Class Form1

Dim LS(6) As PowerPacks.LineShape

Dim SC As New PowerPacks.ShapeContainer

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

For I As Integer = 0 To 6

LS(I) = New PowerPacks.LineShape

With LS(I)

.X1 = 10 : .Y1 = 10 * I

.X2 = 60 : .Y2 = .Y1

SC.Shapes.Add(LS(I))

AddHandler .Click, AddressOf LS_click

End With

Next

Me.Controls.Add(SC)

End Sub

Private Sub LS_click(ByVal sender As System.Object, ByVal e As System.EventArgs)

DirectCast(sender, PowerPacks.LineShape).Focus()

End Sub

End Class





Re: Visual Basic Express Edition moving from VB6 Control arrays and using event handler in VB 2005

Monte Cristo33

Dear John,

Thanks for that,

I already have my 7 lines on the form, lineShape0, lineShape1, LineShape2, etc... to form a 7-segment digit, like so:

I_I

I_I

How would I integrate my existing lines in the block of code above in order to cycle through all the lines every time the command button is clicked.

Sorry to be so demanding, I could do that with my eyes closed in VB6, but I'm really stuck here.

Many thanks again,

MC





Re: Visual Basic Express Edition moving from VB6 Control arrays and using event handler in VB 2005

JohnWein

Then do it in VB6 and let VB2005 upgrade your code.





Re: Visual Basic Express Edition moving from VB6 Control arrays and using event handler in VB 2005

Monte Cristo33

Can I do that Do I open the old VB file with VB2005 or copy and paste the old code





Re: Visual Basic Express Edition moving from VB6 Control arrays and using event handler in VB 2005

JohnWein

I think in the Express edition you have to open the old VB file. In the Standard and higher editions you can copy and select upgrade on the Tools menu to paste the upgraded code. The easiest way is to select Open on the File menu, navigate to the VB6 folder and click on the file displayed in the dialog. But what you want to do is really easy to do in VB.Net. There is very little really new or changed in VB. The change is the .Net framework which lets you do so much more with much less effort.





Re: Visual Basic Express Edition moving from VB6 Control arrays and using event handler in VB 2005

Monte Cristo33

Hi John,

I tried importing the code, however it gets stuck because VB 2005 does not recognise the original control array and therefore cannot loop through it.

you suggest that it would be easy to do in VB 2005. For the most part I agree with you, I have found upgrading to 2005 a blessing so far, until I hit this problem.

Is there a way I could simply cycle through every segment of my 7-segment digit at each press of a button without creating an array

Could I use something like:

Private Sub cmdShowHide_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdShowHide.Click
For N As Integer = 0 To 6
Me.Controls("LineShape" & N).Visible = Not Me.Controls("LineShape" & N).Visible
Next N
End Sub

Is it going in the right direction

Again, my 7 lines are already on the form and are named LineShape0, LineShape1, lineShape2, etc..

Your input is greatly appreciated.

Thanks

MC





Re: Visual Basic Express Edition moving from VB6 Control arrays and using event handler in VB 2005

Dave299

Monte Cristo33 wrote:

Could I use something like:

Private Sub cmdShowHide_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdShowHide.Click
For N As Integer = 0 To 6
Me.Controls("LineShape" & N).Visible = Not Me.Controls("LineShape" & N).Visible
Next N
End Sub

Well if you tried it you would know the answer is no. As stated above LineShapes do not form part of the form's controls collection so you cannot access them in this way.

As you already have the line shapes on your form, which is what I had assumed, there is no reason why the code I posted above should not work. You gave no indication of what errors you had other than there were a lot and you couldn't be bothered to let me know what they were.

I have just added the following to my code

For Each L As LineShape In Shapes

L.Visible = Not L.Visible

Next

and it works fine.





Re: Visual Basic Express Edition moving from VB6 Control arrays and using event handler in VB 2005

Tall Dude

I downloaded someone's code for a LCD (or LED) the other

day and was playing around with it.

Original code (modified some by me,) is not that hot,

but at least it does show a working LCD display when

the 'pump fuel' button is held down.

May not be the best way to do this, but code is at:

http://www.driveway.com/jgkde76440

A good simple example (my code) of how to make dynamic controls with

event handlers is at:

http://www.driveway.com/mejyy79231






Re: Visual Basic Express Edition moving from VB6 Control arrays and using event handler in VB 2005

Monte Cristo33

Hi Dave,

Thanks for looking into this. Sorry I did not report all the error messages I was getting. I'll try it again and report on it.

When I tried your code, it did display horizontal lines, so I guess I need to replace the original loop included in your code with the second one you just posted in order to handle only those lines present on the form. Is this correct

MC





Re: Visual Basic Express Edition moving from VB6 Control arrays and using event handler in VB 2005

Monte Cristo33

Hi TallDude,

I had a look at your solution, and it's exactly what I'm after! I need to print the code, study it and make sure I understand it fully. I hope you won't mind my coming back to you with a few questions if there are a few bits and bobs I don't get.

Hope that's ok with you.

It wouldn't be the first time you save my bacon.

Thanks a lot

MC