Solved

Code Clean Up

Posted on 2012-04-07
5
295 Views
Last Modified: 2012-04-07
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

0
Comment
Question by:DYOUNG1006
  • 2
  • 2
5 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37819641
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
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 450 total points
ID: 37820082
once you have added all the items in list box, you can use the following code to iterate over those items:-
foreach(var item in listBox1.Items)
{
//do your magic here...
}

Open in new window


here is a reference for you:-
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitem.aspx
0
 
LVL 1

Author Comment

by:DYOUNG1006
ID: 37820107
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)
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37820116
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....
0
 
LVL 1

Author Comment

by:DYOUNG1006
ID: 37820208
Thanks !  i'm going to award points now and test later but thank you.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question