Solved

Code Clean Up

Posted on 2012-04-07
5
287 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks !  i'm going to award points now and test later but thank you.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB.NET Server side Javascript 8 51
Paging GridView 7 32
Round a string to two digits 12 23
Make a border less form movable 2 11
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now