locked
Eval? RRS feed

  • Question

  • I know that in VB6 and some other programming languages there is a function called Eval(string), which executes the code in the string on-the-fly. In an application I am making, there are several pictureboxes with similar names (e.g. pb1, pb2, ..., pb30, pb31) - so I figured I could refer to their Image properties with something like this:

    1 For i As Integer = 1 To 40 
    2    Dim p as Bitmap = Eval("pb" & i & ".Image"
    3    '... 
    4 Next 

    However, it appears that there does not exist any such Eval function in VB .NET. I have done a lot of searching, and come across an Eval function of the ScriptControl class, in the Microsoft Script Control 1.0 COM object. This Eval function appears to be of some kind of VBScript COM object, and therefore it gives me an error when I try to reference a .NET component. More Googling gave me some classes from the System.Reflection.Emit namespace. This namespace seems to me to be a step in the right direction, however I'm not sure how exactly to implement it in this situation.

    Any help would be much appreciated.


    • Edited by Beaver1345 Friday, August 29, 2008 12:27 AM Fixed a bit of grammar
    Friday, August 29, 2008 12:26 AM

Answers

  • Hi,

    I know this may look long winded but try this code which uses one button on your FORM in addition to the PictureBoxes.

    It  appears to work, I have tried it with 2 pictureboxes

    pb1 and pb2

    their images were assigned to myImagesArray(1) and myImagesArray(2) respectively.

    Yes I know arrays start at zero, I have made this code use indexes 1 to 40 rather than 0 to 39.




    Regards,

    John
    ________________________________________________________________________




    Option Strict On

    Public Class Form1

      Private myImagesArray(40) As Image

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

        'Loop through the controls.>>
        For Each ctrl In Me.Controls
          'Is it a PictureBox.>>
          If TypeOf ctrl Is PictureBox Then
            'Used to hold the PictureBox name.>>
            Dim picBoxNumber As String
            'Get the NAME of the PictureBox.>>
            picBoxNumber = CType(ctrl, PictureBox).Name
            'Remove the 1st two characters i.e. remove "pb" >>
            picBoxNumber = picBoxNumber.Remove(0, 2)
            'Used to hold the number.>>
            Dim myNum As Integer
            'Try getting the number from the string.>>
            Integer.TryParse(picBoxNumber, myNum)
            'Add the appropriate image to the appropriate place in the array
            'so that pb1 should be assigned to myImagesArray(1) etcetera.>>
            If Not CType(ctrl, PictureBox).Image Is Nothing Then
              myImagesArray(myNum) = CType(ctrl, PictureBox).Image
            End If
          End If
        Next

      End Sub
    End Class


    .--- --- .... -.
    • Marked as answer by Xingwei Hu Monday, September 1, 2008 3:28 PM
    Friday, August 29, 2008 1:02 AM

All replies

  • Hi,

    I know this may look long winded but try this code which uses one button on your FORM in addition to the PictureBoxes.

    It  appears to work, I have tried it with 2 pictureboxes

    pb1 and pb2

    their images were assigned to myImagesArray(1) and myImagesArray(2) respectively.

    Yes I know arrays start at zero, I have made this code use indexes 1 to 40 rather than 0 to 39.




    Regards,

    John
    ________________________________________________________________________




    Option Strict On

    Public Class Form1

      Private myImagesArray(40) As Image

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

        'Loop through the controls.>>
        For Each ctrl In Me.Controls
          'Is it a PictureBox.>>
          If TypeOf ctrl Is PictureBox Then
            'Used to hold the PictureBox name.>>
            Dim picBoxNumber As String
            'Get the NAME of the PictureBox.>>
            picBoxNumber = CType(ctrl, PictureBox).Name
            'Remove the 1st two characters i.e. remove "pb" >>
            picBoxNumber = picBoxNumber.Remove(0, 2)
            'Used to hold the number.>>
            Dim myNum As Integer
            'Try getting the number from the string.>>
            Integer.TryParse(picBoxNumber, myNum)
            'Add the appropriate image to the appropriate place in the array
            'so that pb1 should be assigned to myImagesArray(1) etcetera.>>
            If Not CType(ctrl, PictureBox).Image Is Nothing Then
              myImagesArray(myNum) = CType(ctrl, PictureBox).Image
            End If
          End If
        Next

      End Sub
    End Class


    .--- --- .... -.
    • Marked as answer by Xingwei Hu Monday, September 1, 2008 3:28 PM
    Friday, August 29, 2008 1:02 AM
  • Thanks for your answer, John.

    I hadn't thought of that - it works with my problem pretty well. The only thing I foresee with that is that there may be other pictureboxes other than those I want to loop through, but I think a .IndexOf("pb") test would be enough should that happen. I'm going to try that out and see if it works right now.
    Friday, August 29, 2008 1:21 AM