There are active x controls involved so I don't think you can just paste
this in to VS. For what it is worth, here is all the code to this form.
I'm open to criticism on any of it:
Public Class frmZones
#Region "Class Variables"
Friend ProcessID As Integer
Private drvZone As DataRowView
Private drZone As dsZones.zonesRow
Private TemplateFile As String
Private dsIndexFields As DataSet
Private bmZones As BindingManagerBase
' Button text for adding & editing barcode zones
Const SaveButtonText As String = "Save Zone"
Const NewButtonText As String = "New Zone"
Const EditButtonText As String = "Edit Zone"
Const CancelButtonText As String = "Cancel"
#End Region
#Region "Form Events"
Public Sub New(ByVal ProcessID As Integer)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.ProcessID = ProcessID
End Sub
Private Sub frmZones_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Set list box to owner drawn type
Me.lstZones.DrawMode = DrawMode.OwnerDrawFixed
AddHandler Me.lstZones.DrawItem, AddressOf Me.lstZones_DrawItem
' Fill Zones Table Adapter
Try
frmMain.DbConnect()
Me.ZonesTableAdapter.Connection = frmMain.Cnn
Me.ZonesTableAdapter.Fill(Me.DsZones.zones, ProcessID)
Me.ZonesBindingSource.DataSource = Me.DsZones
Me.bmZones = Me.BindingContext(DsZones, "zones")
Catch ex As Exception
MsgBox(ex.Message)
End Try
' Form settings
Me.Text = Application.ProductName & ": " & Me.Text
Me.WindowState = FormWindowState.Maximized
' Initialize Scanner
Me.IkScan1.ScanInitialize(Handle.ToInt32, 1, 0, "1.0",
"Microfilm Service Corp", "Barcode Ripper", "Barcode Ripper")
' Set display rectangle colors
With Me.IkDisp1
.RectColor1 = Color.Red
.RectColor2 = Color.LightBlue
End With
' Load KF fields
dsIndexFields = frmMain.GetIndexFields(ProcessID)
If Not Me.dsIndexFields Is Nothing Then
Dim dr As DataRow
dr = Me.dsIndexFields.Tables("ProjectDef").NewRow()
dr.Item("fieldnum") = 0
dr.Item("displayname") = "(No Associated Field)"
Me.dsIndexFields.Tables("ProjectDef").Rows.InsertAt(dr, 0)
With Me.cboKfField
.DataSource = Me.dsIndexFields.Tables("ProjectDef")
.ValueMember = "fieldnum"
.DisplayMember = "displayname"
End With
Else
Dim kfc As New Collection
kfc.Add("N/A", 0)
Me.cboKfField.DataBindings.Clear()
Me.cboKfField.DataSource = kfc
Me.cboKfField.SelectedItem = 0
End If
Me.lstZones_SelectedIndexChanged(sender, e)
EnableZoneFields(False)
' Tool tips
ToolTip1.SetToolTip(Me.btnSaveTemplate, "This will save the
currenlty loaded image for this process")
' Load template
TemplateFile = IO.Path.Combine(Application.StartupPath,
"Templates\" & Me.ProcessID & ".tif")
If IO.File.Exists(TemplateFile) Then
Me.LoadImage(TemplateFile)
End If
Me.ZonesTableAdapter.Update(Me.DsZones)
End Sub
Private Sub frmZones_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Me.IkScan1.ScanTerminate()
frmMain.DbDisconnect()
End Sub
#End Region
#Region "Owner-drawn List Box"
Private Sub lstZones_DrawItem(ByVal sender As Object, ByVal e As
DrawItemEventArgs)
Dim Color As Brush = Brushes.White
' The following method should generally be called before drawing.
' It is actually superfluous here, since the subsequent drawing
' will completely cover the area of interest.
e.DrawBackground()
'The system provides the context
'into which the owner custom-draws the required graphics.
'The context into which to draw is e.graphics.
'The index of the item to be painted is e.Index.
'The painting should be done into the area described by e.Bounds.
If e.Index >= 0 Then
Try
If e.Index < DsZones.Tables(0).Rows.Count AndAlso
DsZones.Tables(0).Rows(e.Index).RowState <> DataRowState.Deleted Then
If e.BackColor.ToString.Contains("Highlight") Then
If Not
IsDBNull(DsZones.Tables(0).Rows(e.Index).Item("split_zone")) AndAlso
DsZones.Tables(0).Rows(e.Index).Item("split_zone") Then
Color = Brushes.DarkRed
Else
Color = Brushes.Black
End If
Else
If Not
IsDBNull(DsZones.Tables(0).Rows(e.Index).Item("split_zone")) AndAlso
DsZones.Tables(0).Rows(e.Index).Item("split_zone") Then
Color = Brushes.Yellow
Else
Color = Brushes.White
End If
End If
e.Graphics.DrawString(DsZones.Tables(0).Rows(e.Index).Item("name").ToString,
lstZones.Font, Color, e.Bounds.X, e.Bounds.Y)
End If
Catch ex As Exception
MsgBox(ex.Message & vbNewLine & e.Index.ToString,
MsgBoxStyle.Information)
End Try
End If
e.DrawFocusRectangle()
End Sub
#End Region
#Region "Template Buttons"
Private Sub btnSaveTemplate_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSaveTemplate.Click
If Me.IkDisp1.ImgHandle Then
If Not
IO.Directory.Exists(IO.Path.GetDirectoryName(TemplateFile)) Then
Try
IO.Directory.CreateDirectory(IO.Path.GetDirectoryName(TemplateFile))
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
Exit Sub
End Try
End If
Me.IkFile1.ImgHandle = Me.IkDisp1.ImgHandle
Me.IkFile1.LoadFile(IMGKIT6Lib.LoadFileConstants.ikLoad)
Me.IkFile1.FileName = TemplateFile
Me.IkFile1.SaveFile(IMGKIT6Lib.SaveFileConstants.ikSaveTIFFNoncompressed)
End If
End Sub
Private Sub btnDeleteTemplate_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnDeleteTemplate.Click
Dim DeleteTemplate As New dlgDeleteTemplate
If DeleteTemplate.ShowDialog = Windows.Forms.DialogResult.OK Then
' Delete the template image
If IO.File.Exists(TemplateFile) Then
Try
IO.File.Delete(TemplateFile)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
Exit Sub
End Try
End If
Me.IkDisp1.Display(IMGKIT6Lib.DispModeConstants.ikClear)
Me.IkDisp1.Refresh()
If DeleteTemplate.DeleteZones Then
While Me.ZonesBindingSource.Count > 0
Me.ZonesBindingSource.RemoveCurrent()
Me.ZonesTableAdapter.Update(Me.DsZones)
End While
End If
End If
End Sub
Private Sub btnReloadTemplate_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnReloadTemplate.Click
If IO.File.Exists(TemplateFile) Then
Me.LoadImage(TemplateFile)
Else
MsgBox("A saved template image does not exist for this
process.", MsgBoxStyle.Information)
End If
End Sub
#End Region
#Region "Image Buttons & Scanning"
Private Sub btnLoadImage_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnLoadImage.Click
Me.IkFile1.FilePath =
My.Computer.FileSystem.SpecialDirectories.MyDocuments
If Me.IkFile1.OpenFileDlg() Then
Me.LoadImage(Me.IkFile1.FileName)
End If
End Sub
Private Sub btnScanImage_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnScanImage.Click
Dim Ret As Boolean
Dim Index(10) As Integer
Try
Me.IkScan1.UiMode =
IMGKIT6Lib.ScanUIModeConstants.ikScanUICLOSE
Ret = IkScan1.ScanExec(Index(0))
IkScan1.ScanTerminate()
Catch ex As Exception
MsgBox("Error scanning document: " & ex.Message,
MsgBoxStyle.Critical)
End Try
End Sub
Private Sub btnSelectScanner_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnSelectScanner.Click
Me.IkScan1.ScanSelect()
End Sub
Private Sub btnScanSettings_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnScanSettings.Click
End Sub
Private Sub IkScan1_AfterScan(ByVal sender As Object, ByVal e As
AxIMGKIT6Lib._IIkScanEvents_AfterScanEvent) Handles IkScan1.AfterScan
Dim Filename As String
Me.IkFile1.ImgHandle = e.dibHandle
Me.IkCommon1.ImgHandle = IkFile1.ImgHandle
Me.IkCommon1.GetImageType()
Filename = "scan.tif"
IkFile1.FileName = IO.Path.Combine(IO.Path.GetTempPath, Filename)
If Not
IkFile1.SaveFile(IMGKIT6Lib.SaveFileConstants.ikSaveTIFFNoncompressed) Then
MsgBox("Could not save scanned file...")
End If
Me.IkFile1.LoadFile(IMGKIT6Lib.LoadFileConstants.ikLoad)
Me.IkCommon1.ImgHandle = Me.IkFile1.ImgHandle
Me.IkCommon1.GetImageType()
With Me.IkDisp1
.ImgHandle = Me.IkFile1.ImgHandle
.Display(IMGKIT6Lib.DispModeConstants.ikScale)
.ScrollBar = True
.ScrollDir = IMGKIT6Lib.ScrollDirConstants.ikBottomRight
.ScrollDrag = False
.RectDraw = True
End With
End Sub
#End Region
#Region "Image Display"
Private Sub IkDisp1_MouseUpImage(ByVal sender As Object, ByVal e As
AxIMGKIT6Lib._IIkDispEvents_MouseUpImageEvent) Handles IkDisp1.MouseUpImage
If Me.txtZoneName.Enabled Then
With Me.IkDisp1
Me.txtTop.Text = .RectTop
Me.txtLeft.Text = .RectLeft
Me.txtBottom.Text = .RectBottom
Me.txtRight.Text = .RectRight
End With
' Give each text box focus or else it won't save changes
With Me
.txtTop.Focus()
.txtLeft.Focus()
.txtBottom.Focus()
.txtRight.Focus()
End With
Else
Try
With Me.IkDisp1
.RectTop = Me.txtTop.Text
.RectLeft = Me.txtLeft.Text
.RectBottom = Me.txtBottom.Text
.RectRight = Me.txtRight.Text
End With
Catch ex As Exception
End Try
End If
End Sub
Private Function LoadImage(ByVal FileName As String) As Boolean
Try
Me.IkFile1.FileName = FileName
Me.IkFile1.LoadFile(IMGKIT6Lib.LoadFileConstants.ikLoad)
Me.IkCommon1.ImgHandle = Me.IkFile1.ImgHandle
Me.IkCommon1.GetImageType()
With Me.IkDisp1
.ImgHandle = Me.IkFile1.ImgHandle
.Display(IMGKIT6Lib.DispModeConstants.ikScale)
.ScrollBar = True
.ScrollDir = IMGKIT6Lib.ScrollDirConstants.ikBottomRight
.ScrollDrag = False
.RectDraw = True
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
Return False
End Try
' Set the first box
IkDisp1_MouseUpImage(Nothing, Nothing)
Return True
End Function
#End Region
#Region "Coordinate Changes"
Private Sub txtTop_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles txtTop.TextChanged
Me.ChangeCoordinate(Me.IkDisp1.RectTop, Me.txtTop)
End Sub
Private Sub txtLeft_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtLeft.TextChanged
Me.ChangeCoordinate(Me.IkDisp1.RectLeft, Me.txtLeft)
End Sub
Private Sub txtBottom_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtBottom.TextChanged
Me.ChangeCoordinate(Me.IkDisp1.RectBottom, Me.txtBottom)
End Sub
Private Sub txtRight_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles txtRight.TextChanged
Me.ChangeCoordinate(Me.IkDisp1.RectRight, Me.txtRight)
End Sub
Private Sub ChangeCoordinate(ByRef IkCoord As Integer, ByRef
TextBox As Windows.Forms.TextBox)
Try
IkCoord = TextBox.Text
Catch ex As Exception
'MsgBox(ex.Message)
End Try
End Sub
#End Region
#Region "Zones Buttons & Fields"
Private Sub btnNewSave_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnNewSave.Click
If Me.btnNewSave.Text = frmZones.SaveButtonText Then
If Not IsNumeric(Me.txtTop.Text) OrElse Not
IsNumeric(Me.txtLeft.Text) OrElse Not IsNumeric(Me.txtBottom.Text)
OrElse Not IsNumeric(Me.txtRight.Text) Then
MsgBox("Coordinates are missing or invalid. Please
select highlight the barcode zone, or enter numeric coordinates
manually.", MsgBoxStyle.Information)
Exit Sub
ElseIf txtZoneName.Text.Length = 0 Then
MsgBox("You must give this zone a name before saving
it.", MsgBoxStyle.Information)
Me.txtZoneName.Focus()
Exit Sub
Else
Try
If Not drvZone Is Nothing Then
Me.DsZones.zones.Rows.Add(drvZone.Row)
Me.ZonesBindingSource.CancelEdit()
Else
Me.ZonesBindingSource.EndEdit()
End If
Me.ZonesTableAdapter.Update(Me.DsZones.zones)
Me.DsZones.AcceptChanges()
' Debug purposes
' Me.ZonesTableAdapter.Fill(DsZones.zones, ProcessID)
Catch ex As Exception
MsgBox("Error Saving Changes: " & ex.Message,
MsgBoxStyle.Critical)
End Try
End If
Me.EnableZoneFields(False)
Else
Try
Me.drvZone = Me.ZonesBindingSource.AddNew()
drvZone.Item("processes_id") = ProcessID
drvZone.Item("split_zone") = 0
drvZone.Item("kf_field_id") = 0
Me.EnableZoneFields()
Catch ex As Exception
MsgBox("Error adding new zone: " & ex.Message,
MsgBoxStyle.Exclamation)
End Try
End If
End Sub
Private Sub btnEditCancel_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnEditCancel.Click
If Me.btnEditCancel.Text = frmZones.EditButtonText Then
Me.EnableZoneFields()
Else
Me.EnableZoneFields(False)
Me.ZonesBindingSource.CancelEdit()
End If
Me.drvZone = Nothing
End Sub
Private Sub lstZones_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
lstZones.SelectedIndexChanged
If Me.lstZones.SelectedIndex >= 0 And Me.lstZones.SelectedIndex
< bmZones.Count Then
bmZones.Position = Me.lstZones.SelectedIndex
End If
'If Me.lstZones.SelectedIndex >= 0 Then
' Try
' Me.txtTop.Text =
CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("top").ToString
' Me.txtLeft.Text =
CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("left").ToString
' Me.txtBottom.Text =
CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("bottom").ToString
' Me.txtRight.Text =
CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("right").ToString
' Me.txtZoneName.Text =
CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("name").ToString
' If
IsDBNull(CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("kf_field_id")) Then
' Me.cboKfField.Text = "N/A"
' Else
' Me.cboKfField.SelectedValue =
CType(Me.ZonesBindingSource.Item(Me.lstZones.SelectedIndex),
DataRowView).Item("kf_field_id")
' End If
' Catch ex As Exception
' MsgBox("Error updating fields: " & ex.Message,
MsgBoxStyle.Exclamation)
' End Try
'End If
End Sub
Private Sub EnableZoneFields(Optional ByVal Enable As Boolean = True)
Try
Me.btnEditCancel.Enabled = True
Me.lstZones.Enabled = Not Enable
Me.txtTop.Enabled = Enable
Me.txtLeft.Enabled = Enable
Me.txtBottom.Enabled = Enable
Me.txtRight.Enabled = Enable
Me.txtZoneName.Enabled = Enable
If Me.dsIndexFields Is Nothing Then
Me.cboKfField.Enabled = False
Else
Me.cboKfField.Enabled = Enable
End If
If Enable Then
Me.btnNewSave.Text = frmZones.SaveButtonText
Me.btnEditCancel.Text = frmZones.CancelButtonText
Else
Me.btnNewSave.Text = frmZones.NewButtonText
Me.btnEditCancel.Text = frmZones.EditButtonText
' If an existing zone is not selected then we can't edit it
If Me.lstZones.Items.Count = 0 Then
Me.btnEditCancel.Enabled = False
End If
End If
Catch ex As Exception
MsgBox("Error enabling fields: " & ex.Message,
MsgBoxStyle.Exclamation)
End Try
End Sub
#End Region
#Region "Right-click Menu Actions"
Private Sub Delete_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Delete.Click
If MsgBox("Are you sure you want to delete the selected zone?",
MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
Try
Me.ZonesBindingSource.RemoveCurrent()
Me.ZonesTableAdapter.Update(Me.DsZones)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
End Try
End If
End Sub
Private Sub TestZoneToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
TestZoneToolStripMenuItem.Click
Dim Barcode As SoftekBarcode
Dim nBarCode As Short
Dim strBarcode As String
Barcode = New SoftekBarcode()
frmMain.SetBCPrefs(Me.ProcessID, Barcode)
' Set area to scan
Barcode.SetScanRect(Me.txtLeft.Text, Me.txtTop.Text,
Me.txtRight.Text, Me.txtBottom.Text, 0)
nBarCode = Barcode.ScanBarCode(Me.IkFile1.FileName)
' Or read the barcode from an image in a PictureBox
' bm = New Bitmap(Me.PictureBox1.Image)
' nBarCode = barcode.ScanBarCodeFromBitmap(bm.GetHbitmap())
If (nBarCode < 0) Then
MsgBox("Error reading barcode")
ElseIf (nBarCode = 0) Then
MsgBox("No barcode found on this image")
Else
strBarcode = Barcode.GetBarString(1)
MsgBox("Barcode Found: " & strBarcode)
End If
End Sub
Private Sub
SplitMultipageTIFsWithThisZoneToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
SplitMultipageTIFsWithThisZoneToolStripMenuItem.Click
For i As Integer = 0 To DsZones.Tables(0).Rows.Count - 1
Me.drvZone = Me.ZonesBindingSource.Item(i)
If i = Me.lstZones.SelectedIndex Then
drvZone.Item("split_zone") = 1
Else
drvZone.Item("split_zone") = 0
End If
Next
Try
Me.ZonesBindingSource.EndEdit()
Me.ZonesTableAdapter.Update(Me.DsZones)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical)
End Try
Me.lstZones.Refresh()
End Sub
#End Region
End Class