Results 1 to 8 of 8

Thread: Can Anyone help in this set of codes?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Singapore
    Posts
    55

    Resolved Can Anyone help in this set of codes?

    Hi there.

    I have done up a set of codes to view photos/ pictures. The program is working well but, there are some errors. I suspect that it is the logic problem.
    Can anyone help to solve it?

    Here's some parts of the codes:

    Public FileIndex As Integer
    'Public FileIndexx As Integer
    Public ImageFiles() As FileInfo

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim DirObj As New DirectoryInfo("C:\Documents and Settings\project\My Documents\My Pictures")
    ImageFiles = DirObj.GetFiles("*.jpg")

    'If ImageFiles.Length = 0 Then
    'Me.Close()
    'Else
    If ImageFiles.Length = 1 Then
    'Button1.Text = "Done"
    'Else
    btnNext.Text = "Next"
    End If

    FileIndex = 0
    Dim MyImage As Image
    Try
    MyImage = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)

    PicBox().Image = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)

    Catch Ex As Exception
    End Try
    Me.Text = ImageFiles(FileIndex).FullName
    'End If
    End Sub

    Private Sub open_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles open.Click
    Dim open As OpenFileDialog = New OpenFileDialog

    open.Title = "Select the image to open."
    open.Filter = ("Supported Files|*.gif;*.jpg;*.bmp")
    open.ShowDialog()

    Dim theImage As New Bitmap(open.FileName)
    PicBox.Image = theImage

    End Sub

    Private Sub rotate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rotate.Click
    PicBox.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
    PicBox.Refresh()
    End Sub

    Private Sub exitleft_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitleft.Click
    Close()
    End Sub

    Private Sub btnPre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPre.Click
    FileIndex = FileIndex - 1
    If FileIndex = ImageFiles.Length Then
    Me.Close()
    Else
    If FileIndex = (ImageFiles.Length + 1) Then
    btnPre.Text = "Done"
    End If
    Dim MyImage As Image
    Try
    MyImage = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)

    PicBox().Image = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)

    Catch Ex As Exception
    End Try
    'Me.Text = ImageFiles(FileIndex).FullName
    End If
    End Sub

    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    FileIndex = FileIndex + 1
    If FileIndex = ImageFiles.Length Then
    Me.Close()
    Else
    If FileIndex = (ImageFiles.Length - 1) Then
    'btnNext.Text = "Done"
    btnNext.Text = "Next"
    End If
    Dim MyImage As Image
    Try
    MyImage = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)

    PicBox().Image = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)

    Catch Ex As Exception
    End Try
    'Me.Text = ImageFiles(FileIndex).FullName
    End If
    End Sub


    Thanks so much!
    Last edited by longdexin; Jul 12th, 2005 at 01:07 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can Anyone help in this set of codes?

    That's a fair bit of code. You can't expect us to just read it all and know what the issues are. I'd suggest you put it in [Highlight=VB] tags to make it more readable and then tell us what the issues are and where they occur.

  3. #3
    Frenzied Member dinosaur_uk's Avatar
    Join Date
    Sep 2004
    Location
    Jurassic Park
    Posts
    1,098

    Re: Can Anyone help in this set of codes?

    Here you go

    VB Code:
    1. Public FileIndex As Integer
    2.     'Public FileIndexx As Integer
    3.     Public ImageFiles() As FileInfo
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         Dim DirObj As New DirectoryInfo("C:\Documents and Settings\project\My Documents\My Pictures")
    7.         ImageFiles = DirObj.GetFiles("*.jpg")
    8.  
    9.         'If ImageFiles.Length = 0 Then
    10.         'Me.Close()
    11.         'Else
    12.         If ImageFiles.Length = 1 Then
    13.             'Button1.Text = "Done"
    14.             'Else
    15.             btnNext.Text = "Next"
    16.         End If
    17.  
    18.         FileIndex = 0
    19.         Dim MyImage As Image
    20.         Try
    21.             MyImage = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)
    22.  
    23.             PicBox().Image = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)
    24.  
    25.         Catch Ex As Exception
    26.         End Try
    27.         Me.Text = ImageFiles(FileIndex).FullName
    28.         'End If
    29.     End Sub
    30.  
    31.     Private Sub open_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles open.Click
    32.         Dim open As OpenFileDialog = New OpenFileDialog
    33.  
    34.         open.Title = "Select the image to open."
    35.         open.Filter = ("Supported Files|*.gif;*.jpg;*.bmp")
    36.         open.ShowDialog()
    37.  
    38.         Dim theImage As New Bitmap(open.FileName)
    39.         PicBox.Image = theImage
    40.  
    41.     End Sub
    42.  
    43.     Private Sub rotate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles rotate.Click
    44.         PicBox.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
    45.         PicBox.Refresh()
    46.     End Sub
    47.  
    48.     Private Sub exitleft_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitleft.Click
    49.         Close()
    50.     End Sub
    51.  
    52.     Private Sub btnPre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPre.Click
    53.         FileIndex = FileIndex - 1
    54.         If FileIndex = ImageFiles.Length Then
    55.             Me.Close()
    56.         Else
    57.             If FileIndex = (ImageFiles.Length + 1) Then
    58.                 btnPre.Text = "Done"
    59.             End If
    60.             Dim MyImage As Image
    61.             Try
    62.                 MyImage = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)
    63.  
    64.                 PicBox().Image = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)
    65.  
    66.             Catch Ex As Exception
    67.             End Try
    68.             'Me.Text = ImageFiles(FileIndex).FullName
    69.         End If
    70.     End Sub
    71.  
    72.     Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    73.         FileIndex = FileIndex + 1
    74.         If FileIndex = ImageFiles.Length Then
    75.             Me.Close()
    76.         Else
    77.             If FileIndex = (ImageFiles.Length - 1) Then
    78.                 'btnNext.Text = "Done"
    79.                 btnNext.Text = "Next"
    80.             End If
    81.             Dim MyImage As Image
    82.             Try
    83.                 MyImage = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)
    84.  
    85.                 PicBox().Image = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)
    86.  
    87.             Catch Ex As Exception
    88.             End Try
    89.             'Me.Text = ImageFiles(FileIndex).FullName
    90.         End If
    91.     End Sub

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can Anyone help in this set of codes?

    ...or not.

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Singapore
    Posts
    55

    Re: Can Anyone help in this set of codes?

    Sorry for posting that junk of codes the other time. Anyway, juz to give you a little background of the project.

    Basically, this project is to write a program that can be able to view pictures. This program will be constantly connected to the server and when server is sending information (which is the pictures), the program shld be able to show the pictures. There will be a 'Next' and 'Previous' Button and navigate the pictures.

    The codes below is able to show next and previous pictures within the folder. However, there's some logic error. I'm not so sure of wad went wrong, so decided to post the set of codes to this forum to see if anyone could help.

    Sorry for the trouble and thanks for helpin out.

    Here's the Next button codes:
    VB Code:
    1. Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
    2.         FileIndex = FileIndex + 1
    3.         If FileIndex = ImageFiles.Length Then
    4.             Me.Close()
    5.         Else
    6.             If FileIndex = (ImageFiles.Length - 1) Then
    7.                 'btnNext.Text = "Done"
    8.                 btnNext.Text = "Next"
    9.             End If
    10.             Dim MyImage As Image
    11.             Try
    12.                 MyImage = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)
    13.  
    14.                 PicBox().Image = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)
    15.  
    16.             Catch Ex As Exception
    17.             End Try
    18.             Me.Text = ImageFiles(FileIndex).FullName
    19.         End If
    20.     End Sub

    Here's the Previous button codes:

    VB Code:
    1. Private Sub btnPre_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPre.Click
    2.         FileIndex = FileIndex - 1
    3.         If FileIndex = ImageFiles.Length Then
    4.             Me.Close()
    5.         Else
    6.             If FileIndex = (ImageFiles.Length + 1) Then
    7.                 btnPre.Text = "Done"
    8.             End If
    9.             Dim MyImage As Image
    10.             Try
    11.                 MyImage = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)
    12.  
    13.                 PicBox().Image = Image.FromFile(ImageFiles(FileIndex).FullName, RichTextBoxStreamType.PlainText)
    14.  
    15.             Catch Ex As Exception
    16.             End Try
    17.             Me.Text = ImageFiles(FileIndex).FullName '<---- The error occur here
    18.         End If
    19.     End Sub

    I would oso like to ask on how to implement a loop method. If there are 5 picture files, when i click on the next button till the 5th picture, it shld be able to continue to loop back to the 1st picture and so on.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can Anyone help in this set of codes?

    As to the loop you mention, for the Next button use:
    VB Code:
    1. FileIndex += 1
    2.  
    3. If FileIndex = ImageFiles.Length Then
    4.     FileIndex = 0
    5. End If
    and for the Previous button use:
    VB Code:
    1. FileIndex -= 1
    2.  
    3. If FileIndex = -1 Then
    4.     FileIndex = ImageFile.Length - 1
    5. End If
    As for the error you are getting, I'd say it's because you are testing for "FileIndex = ImageFiles.Length" and "FileIndex = (ImageFiles.Length + 1)" when the Previous button is clicked instead of "FileIndex = -1" and "FileIndex = 0". You are decrementing the FileIndex variable to the lower bound of the array, remember.

  7. #7

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Singapore
    Posts
    55

    Re: Can Anyone help in this set of codes?

    jmcilhinney,

    thanks alot man. i trying the code and it works but still editing some of the codes.

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2005
    Location
    Singapore
    Posts
    55

    Re: Can Anyone help in this set of codes?

    once again thanks alot to all u guys... the loop is working[resloved]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width