Elusive Bug

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • GrandpaB

    Elusive Bug

    I have a baffling application bug. Any clues you could
    offer to help me squash this bug would be greatly
    appreciated.

    My application saves the contents of the form, as an
    object to arrayList. The form contains buttons so that I
    can navigate through all the objects and also add and
    delete objects. When the application is opened or
    closed, an XML file, using the SoapFormatter is read or
    written to the hard drive. The application works well.
    ALMOST.

    One of the controls on the form is a picture box. Double
    clicking the picture box opens the OpenFile dialog so
    that a JPEG can be displayed in the picture box. After
    invoking the OpenFile dialog I can navigate to other
    objects in the arrayList and return. The file name for
    the JPEG image has been has been saved in the arrayList
    and the picture displays as designed.

    However, when I close the application the arrayList is
    NOT written to the hard drive if I had invoked the
    OpenFile dialog.. If I manually edit the textbox that
    contains the JPEG file name, the JPEG image changes and
    the file IS written to the hard drive.

    This mystery has had me bamboozled for a couple of weeks;
    thanks in advance for any suggestions.

    GrandpaB

  • Cor Ligthert

    #2
    Re: Elusive Bug

    GrandPa,

    For me it seems if you use for a simple problem a difficult solution.

    Can you explain to us why to make it more clear why you do that or show some
    code.

    The reason for this is, that when a solution sounds difficult than helping
    is even more difficult.

    Cor


    Comment

    • GrandpaB

      #3
      Re: Elusive Bug

      Cor,

      Thanks for your response. This is actually the second
      time I posted the problem. The first post included the
      code and a verbose description of the problem. It was
      too long, too complex and I got no response. You are a
      brave soul; here is the code I think probably contains my
      bug. For brevity I've removed the code not relivant to
      the bug.

      Thanks again GrandpaB

      Imports System.IO
      Imports System.Collecti ons
      Imports System.Runtime. Serialization.F ormatters.Soap

      '************** *************** *************** *************
      Public Class Form1 '***
      '************** *************** *************** *********
      Inherits System.Windows. Forms.Form
      Public SGWArt As New classArt

      #Region " Windows Form Designer generated code "

      Private Sub Form1_Load(ByVa l sender As System.Object,
      _
      ByVal e As System.EventArg s) Handles MyBase.Load
      SGWArt.GetFile( )
      SGWArt.Indx = 0
      Art2Form(SGWArt .Art)
      StatusUpdate()
      End Sub

      Private Sub Form1_Closing(B yVal sender As Object, _
      ByVal e As
      System.Componen tModel.CancelEv entArgs) _
      Handles MyBase.Closing
      SGWArt.Art = Form2Art()
      SGWArt.SaveFile ()
      SGWArt = Nothing
      Beep()
      End Sub

      Sub StatusUpdate()
      StatusBar1.Text = "Object " + CStr(SGWArt.Ind x +
      1) + _
      " of " + CStr(SGWArt.Art Count)
      End Sub

      Sub Art2Form(ByVal MyArt As objArt)
      With MyArt
      tbTitle.Text = .Title
      tbDesc.Text = .Des
      pbPict.Text = .Pic
      End With
      DisplayJPG()
      End Sub

      Function Form2Art() As objArt
      Dim MyArt As New objArt
      With MyArt
      .Title = tbTitle.Text
      .Des = tbDesc.Text
      .Pic = pbPict.Text
      End With
      Form2Art = MyArt
      MyArt = Nothing
      End Function

      Private Sub BtnFirst_Click( ByVal sender As Object, _
      ByVal e As System.EventArg s) Handles
      BtnFirst.Click
      'Save the contents of the form as an art object
      and to
      'SGWArt.art, set the index to 0, get and display
      SGWArt
      'atthe current index
      SGWArt.Art = Form2Art()
      SGWArt.Indx = 0
      Art2Form(SGWArt .Art)
      StatusUpdate()
      End Sub

      'Note: There are additional buttons that move to the
      previous,
      'next and last index. There are also buttons to add and
      delete
      'art objects. For brevity they're not shown.

      Sub DisplayJPG()
      Dim MyImage As Image
      Dim MyWidth As Integer
      Dim MyHeight As Integer
      Dim pbArtSize As Integer
      If System.IO.File. Exists(tbDesc.T ext) Then
      MyImage = Image.FromFile( tbDesc.Text)
      pbArtSize = pbPict.Width 'Note pbArt must be
      square
      MyWidth = MyImage.Width
      MyHeight = MyImage.Height
      'Set the width & height of the thumbnail
      If MyWidth > MyHeight Then
      MyHeight = pbArtSize * MyHeight / MyWidth
      MyWidth = pbArtSize
      Else
      MyWidth = pbArtSize * MyWidth / MyHeight
      MyHeight = pbArtSize
      End If
      MyImage = MyImage.GetThum bnailImage(MyWi dth,
      MyHeight, _
      Nothing, New IntPtr)
      pbPict.Image = MyImage
      Else
      pbPict.Image = Nothing
      End If
      End Sub

      Private Sub pbPict_DoubleCl ick(ByVal sender As
      Object, _
      ByVal e As System.EventArg s) Handles
      pbPict.DoubleCl ick
      If jpgDialog.ShowD ialog = DialogResult.OK Then
      tbDesc.Text = jpgDialog.FileN ame
      DisplayJPG()
      End If
      End Sub

      End Class

      '************** *************** *************** *************
      <[Serializable]()> Public Class objArt '***
      '************** *************** *************** *********
      Public Title As String
      Public Des As String
      Public Pic As String
      End Class

      '************** *************** *************** *************
      Public Class classArt '***
      '************** *************** *************** *********
      Private Shared ArtList As New ArrayList
      Private ArtIndex As Integer
      Private ArtFileName As String = "art.xml"

      Public Sub SaveFile() 'save ArtList to ArtFile
      Dim Msg1 As String = "ArtFile was not saved!
      Reason: "
      Dim Msg2 As String = "Art"
      Dim sf As New SoapFormatter
      Dim fs As New FileStream(ArtF ileName,
      FileMode.Create )
      Try
      sf.Serialize(fs , ArtList)
      Catch ex As Exception
      MsgBox(Msg1 & ex.Message,
      MsgBoxStyle.Cri tical, Msg2)
      Finally
      fs.Close()
      End Try
      End Sub

      Public Sub GetFile() 'get ArtList from ArtFile
      Dim Msg1 As String = "Failed to open ArtList!
      Reason: "
      Dim Msg2 As String = "Art"
      If System.IO.File. Exists(ArtFileN ame) Then
      Dim fs As New FileStream(ArtF ileName,
      FileMode.Open)
      Dim sf As New SoapFormatter
      Try
      ArtList = CType(sf.Deseri alize(fs),
      ArrayList)
      Catch ex As Exception
      MsgBox(Msg1 & ex.Message,
      MsgBoxStyle.Cri tical, Msg2)
      Finally
      fs.Close()
      sf = Nothing
      fs = Nothing
      End Try
      Else
      AddArt()
      End If
      End Sub

      Public Property Art() As objArt
      Get
      'return Art object from ArtList at current
      index
      Return ArtList.Item(Ar tIndex)
      End Get
      Set(ByVal MyArt As objArt)
      'put MyArt into ArtList at current index
      ArtList.Item(Ar tIndex) = MyArt
      End Set
      End Property

      Public ReadOnly Property ArtCount() As Integer
      Get
      ArtCount = ArtList.Count
      End Get
      End Property

      Public Property Indx() As Integer
      Get
      'return the current index for ArtList
      Indx = ArtIndex
      End Get
      Set(ByVal Value As Integer)
      'set Value as ArtIndex & fix errors
      0<=Indx<ArtList .count
      If Value < 0 Then Value = 0
      If Value >= ArtList.Count Then Value =
      ArtList.Count - 1
      ArtIndex = Value
      End Set
      End Property

      End Class

      Comment

      • Cor Ligthert

        #4
        Re: Elusive Bug

        Grandpa,

        I looked at your code, however first, why do you want to use SOAP, what is
        the special reason thay you do not serialize your graph (what I did not see
        in your code) using the bytearray?

        Cor


        Comment

        • GrandpaB

          #5
          Re: Elusive Bug

          Cor,

          Thanks again for looking at the code. Being new to
          VB.Net I used the first example that I found that
          appeared to solve the problem. Because SOAP wrote an XML
          file it also allowed me to open the file in my browser
          and easily observe what was being written.

          If there is a better way to create a file, I'm willing to
          learn and to try it. However it does not seem as if SOAP
          is the cause of the bug. I am not familiar with the
          bytearray, but I will read about it and the graph term
          that you used.

          GrandpaB

          Comment

          • Cor Ligthert

            #6
            Re: Elusive Bug

            GrandpaB.

            I did a long time not something with office, however can you look at this
            "Graphic" sample I once made for images and datasets (which is directly
            XML).



            I stopped with Soap after that the first version from Microsoft WSDL was
            completly renewed, I have expiriences when Mifrosoft does that. In VSNet
            webservices everything about that is completly automaticly done.

            Do you want information about Soap than one of the webservices newsgroup is
            in my opinion a better place. I have seldom seen it in this newsgroup.

            Cor


            Comment

            • GrandpaB

              #7
              Re: Elusive Bug

              Cor,

              Again thanks for your insights. The application that I'm
              try to debug does not store the JPEG image in the data
              file. What is stored is the filename and path of the
              image.

              If I double click the picture box, pbPict, I can load a
              new filename and path into the textbox, tbDesc through
              the OpenFile dialog, jpgDialog. The selected JPEG image
              is displayed in pbPict. However, when I close the
              application the arrayList, ArtList, is not saved to the
              hard drive. If type the filename and path into the
              textbox, tbDesc, ArtList is saved to the hard drive when
              the application closes.

              I am beginning to look at your code on the google site,
              but I believe it adds the image to the output file. My
              appliction only needs the filename and path saved in the
              output file.

              Thanks, GrandpaB

              Comment

              • GrandpaB

                #8
                Re: Elusive Bug

                Cor,

                I have rewritten the application and reduced it to the
                bare essentials that create the error and still the error
                remains. I have then tried using the binaryFormatter
                instead of the SoapFormatter and I get the same error. I
                strongly suspect that this is a valid .NET 2003 bug!

                I have made a new post on this newsgroup as a possible
                bug.

                Thanks for your help, GrandpaB

                Comment

                • Cor Ligthert

                  #9
                  Re: Elusive Bug

                  GrandPa,

                  Than it is even easier when you use that.

                  Cor


                  Comment

                  • GrandpaB

                    #10
                    Elusive Bug Solved

                    Thanks to a MVP who identified the problem.

                    When my application opened the OpenFileDialog it changed
                    the default directory. Then when the application closed
                    and wrote the data file it was written to the directory
                    last used in the OpenFileDialog. The fix, suggested by
                    the MVP, was to change the OpenFileDialog
                    RestoreDirector y property from False to True. Now the
                    original default directory is restored when the dialog
                    closes.

                    Thanks, to all who offered suggestions to help solve this
                    problem.

                    GrandpaB

                    Comment

                    Working...