?
Solved

Code Clean Up

Posted on 2012-04-07
5
Medium Priority
?
312 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 1800 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

862 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