Solved

Code Clean Up

Posted on 2012-04-07
5
299 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

691 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