Link to home
Start Free TrialLog in
Avatar of DYOUNG1006
DYOUNG1006Flag for United States of America

asked on

Code Clean Up

I want to clean this code up.  I want to use the listbox and cycle through the items in the list box and add the files that way, this way i can take an unlimited number of entries.  I know in vba i could use listindex to determine where in the listbox the item was, not sure how to to it in vb, in help is appreciated!

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO

Public Class Form1

    Dim myImageCodecInfo As ImageCodecInfo
    Dim myEncoder As Encoder
    Dim myEncoderParameter As EncoderParameter
    Dim myEncoderParameters As EncoderParameters
    Dim page1 As Bitmap
    Dim page2 As Bitmap
    Dim page3 As Bitmap


    Private Sub btnGrabFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGrabFile.Click
        OpenFileDialog1.ShowDialog()
    End Sub


    Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        txtFileLoc.Text = OpenFileDialog1.FileName.ToString()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ListBox1.Items.Add(txtFileLoc.Text)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        txtFileLoc.Enabled = False
    End Sub

    Private Sub btnMultiPageTiff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiPageTiff.Click



        ' Create three Bitmap objects.
        page1 = New Bitmap("C:\Users\dyoung.AMER\Desktop\DM\Question.1\Data\Page1.tiff")
        page2 = New Bitmap("C:\Users\dyoung.AMER\Desktop\DM\Question.1\Data\Page2.tiff")
        page3 = New Bitmap("C:\Users\dyoung.AMER\Desktop\DM\Question.1\Data\Page3.tiff")

        ' Get an ImageCodecInfo object that represents the TIFF codec.
        myImageCodecInfo = GetEncoderInfo("image/tiff")

        ' Create an Encoder object based on the GUID
        ' for the SaveFlag parameter category.
        myEncoder = Encoder.SaveFlag

        ' Create an EncoderParameters object.
        ' An EncoderParameters object has an array of EncoderParameter
        ' objects. In this case, there is only one
        ' EncoderParameter object in the array.
        myEncoderParameters = New EncoderParameters(1)

        ' Save the first page (frame).
        myEncoderParameter = New EncoderParameter(myEncoder, Fix(EncoderValue.MultiFrame))
        myEncoderParameters.Param(0) = myEncoderParameter
        page1.Save("C:\Users\dyoung.AMER\Desktop\DM\Question.1\Data\multipage.tiff", myImageCodecInfo, myEncoderParameters)

        ' Save the second page (frame).
        myEncoderParameter = New EncoderParameter(myEncoder, Fix(EncoderValue.FrameDimensionPage))
        myEncoderParameters.Param(0) = myEncoderParameter
        page1.SaveAdd(page2, myEncoderParameters)

        ' Save the third page (frame).
        myEncoderParameter = New EncoderParameter(myEncoder, Fix(EncoderValue.FrameDimensionPage))
        myEncoderParameters.Param(0) = myEncoderParameter
        page1.SaveAdd(page3, myEncoderParameters)

        ' Close the multiple-frame file.
        myEncoderParameter = New EncoderParameter(myEncoder, Fix(EncoderValue.Flush))
        myEncoderParameters.Param(0) = myEncoderParameter
        page1.SaveAdd(myEncoderParameters)

    End Sub 'Main

    Private Shared Function GetEncoderInfo(ByVal mimeType As String) As ImageCodecInfo
        Dim j As Integer
        Dim encoders() As ImageCodecInfo
        encoders = ImageCodecInfo.GetImageEncoders()

        j = 0
        While j < encoders.Length
            If encoders(j).MimeType = mimeType Then
                Return encoders(j)
            End If
            j += 1
        End While
        Return Nothing

    End Function 'GetEncoderInfo

   

End Class

Open in new window

Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

You can use the same approach

Listbox1.Items(index)

and Items collection has IndexOf property

http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.objectcollection.aspx
ASKER CERTIFIED SOLUTION
Avatar of BuggyCoder
BuggyCoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DYOUNG1006

ASKER

BuddyCoder,

thx, now one step further is this true that i can use this syntax to obtain the reference number / index  / #in the list box order
Listbox1.Items(index)
to obtain the index of an item from list box use this:-
foreach(var item in listBox1.Items)
{
int index = listBox1.Items.IndexOf(item);
//do your magic here...
}

Open in new window


the code given by you in last post will give you the listitem at the index specified by you, if you want to know the index of the item use the above snippet....
Thanks !  i'm going to award points now and test later but thank you.