creating images from code on a form in VB5

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nevj
    New Member
    • Oct 2012
    • 2

    creating images from code on a form in VB5

    I am trying to place a user defined number of imageboxs inside a picture box. I have tried creating an array of images:

    Code:
    dim shapes() as image
    and I can then redimension this array but if I try to load a picture:

    Code:
    for i = 0 to #
    shapes(i).picture = loadpicture("...")
    next
    it gives an error message : "Object variable or With block variable not set"

    Can someone help me out?
    Last edited by zmbd; Oct 20 '12, 04:26 PM. Reason: please format posted code using the <CODE/> button.
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    It appears that you posted some generic code, such as "#" in the for loop and the (...) etc...

    Please post the actual code.

    Comment

    • PsychoCoder
      Recognized Expert Contributor
      • Jul 2010
      • 465

      #3
      Moved to proper forum :)

      Comment

      • Nevj
        New Member
        • Oct 2012
        • 2

        #4
        Hello,
        Thanks for your reply. The whole code is the following:

        Code:
        Private Sub ok_Click()
        Dim test As Integer
           
        If IsNumeric(Text1.Text) = True Then
            If Text1.Text > 0 Then
                test = Text1.Text
                If Text1.Text - test = 0 Then
                    ReDim shapes.(Text1.Text)
                    For i = 0 To Text1.Text
                    
                    shapes(i).Picture = LoadPicture("C:\Users\Neville Junkin\Desktop\School\Ananum\paint\Sans titre.bmp")
        
                    Next
                    Form1.Picture1.DrawWidth = 3
                    Form1.Picture1.ForeColor = vbRed
                    For i = 0 To 831
                        X1 = (Form1.Picture1.Width / 2) + (50 * node(i, 0))
                        Y1 = (Form1.Picture1.Height / 2) + (50 * node(i, 1))
                        Form1.Picture1.PSet (X1, Y1)
                    Next
                    Points.Hide
                Else
                    MsgBox "Nombre des points doit être entier.", vbOKOnly, Error
                End If
            Else
            MsgBox "Nombre des points doit être positive.", vbOKOnly, Error
            End If
        Else
        MsgBox "Nombre des points doit être un integer.", vbOKOnly, Error
        End If
        End Sub
        With shapes defined in a module as follows :

        Code:
        Public shapes() as Image
        The code works until after I redimension shapes in line 8, when I try to add a picture to the image I recieve the error message "Object variable or With block variable not set". I have tried:
        Code:
        Set shapes(i).Picture = LoadPicture("C:\Users\Neville Junkin\Desktop\School\Ananum\paint\Sans titre.bmp")
        with "Set" and I recieve the same error.
        I have also tried:

        Code:
        set Shapes(i) as New image
        But the the option New Image is not available nor is picturebox which I tried after changing the variable to that type of object.
        The value of the object is Nothing and I beleive that might be the problem but I can't seem to change it. Any help would be much appreciated.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          Well....

          - I don't see where you have declared any object in the first code block.
          - I don't see where the array was declared to start with so I'm surprised the redim works without error; however, VB is a little different from VBA which I work with most of the time.
          - Line8 looks odd to me; usually such would be written as
          ReDim Shapes(text.tex t) or even
          ReDim Preserve Shapes(text.tex t) so as to save anything already stored in the array; however, without the rest of your project I may have missed something... :)

          The second code block you've posted has no code at all when expanded, you can go back into your post and edit it so as to post the code that might be helpful. Also, is this part of a class module?
          Last edited by zmbd; Oct 21 '12, 12:26 PM.

          Comment

          • Killer42
            Recognized Expert Expert
            • Oct 2006
            • 8429

            #6
            Not sure whether it's your problem, as I've only had a quick glance at the code so far. But ReDim erases all existing entries in the array unless you add the keyword Preserve.

            Oops, I see you already mentioned that zmbd.

            However, I don't get what you mean about ReDim Preserve Shapes(text.text). This statement is used to change the size of the array, not the contents, so the text.text doesn't seem to have any relevance.

            Comment

            • PsychoCoder
              Recognized Expert Contributor
              • Jul 2010
              • 465

              #7
              Your line

              Code:
              ReDim shapes.(Text1.Text)
              Is not only invalid but not using Preserve everything in the array is wiped, which would lead to the error you're receiving. So change it to

              Code:
              ReDim Preserve Shapes (Text1.Text)
              And it should work

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                @killer42
                Look at lines 4, 5, and 6.
                These give me the impression that "text.text" is the value entered into the form control named "text" as referenced by the "text" property. Really very unfourtunate to use a property name as a control name. Does that help with my suggestion to use ReDim Preserve Shapes(text.tex t) in that, as stated in OP, Nevj is trying to create an array to hold the information.

                Comment

                • Rodney Roe
                  New Member
                  • Oct 2010
                  • 61

                  #9
                  If your trying to create several controls and put them into an array do something like this;

                  add this code into your userform
                  Code:
                  dim myImage as new image
                  redim myImage(someNumber)
                  for i = 0 to someNumber
                    set myImage(i) = Me.Controls.Add("forms.image.1", "image" & i, True)
                    ... add more code here
                  next
                  I did this in VBA and i'm pretty sure it'll work in vb5 but not possitive.

                  Comment

                  • Killer42
                    Recognized Expert Expert
                    • Oct 2006
                    • 8429

                    #10
                    Originally posted by zmbd
                    @killer42
                    Look at lines 4, 5, and 6...
                    Sorry, I should have read in more detail before replying.

                    Comment

                    • zmbd
                      Recognized Expert Moderator Expert
                      • Mar 2012
                      • 5501

                      #11
                      @Killer:
                      No prob. Took me three reads to figure out what was going on... four lines of code to make sure the "text" control value is numeric and greater than zero... and then... we don't use the type declared variable in the rest of the code.

                      Comment

                      Working...