By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,928 Members | 1,173 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,928 IT Pros & Developers. It's quick & easy.

Want to Dispose the Image Object in Derived class (MDIChildForm)

P: n/a
I want to dispose the image object in my child form, unless I won't dispose this

object i can't delete the image file in my folder.
I get this error in MDIparent form

"An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll"
"Additional information: Invalid parameter used."

Here is my code:-

Public Class frmViewImage
Inherits System.Windows.Forms.Form

Dim myOleAdapter As OleDbDataAdapter
Dim mydataset As DataSet = New DataSet()
Dim mydatatable As DataTable = New DataTable()
Dim mycommander As OleDbCommandBuilder

Private savedImageName As String
Private curImage As Image = Nothing

Dim iRecordCount As Integer
Dim iCurrentPosition As Integer
Dim WithEvents oBinding As Binding

Dim flag As Boolean = True
Dim tmpStr As String

Dim myCommand As OleDbCommandBuilder
#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If

End If
MyBase.Dispose(disposing)

'MsgBox(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
Friend WithEvents cmbUserName As System.Windows.Forms.ComboBox
Friend WithEvents cmdNext As System.Windows.Forms.Button
Friend WithEvents cmdFirst As System.Windows.Forms.Button
Friend WithEvents cmdPrev As System.Windows.Forms.Button
Friend WithEvents CmdLast As System.Windows.Forms.Button
Friend WithEvents txtImageCmnts As System.Windows.Forms.TextBox
Friend WithEvents CmdExit As System.Windows.Forms.Button
Friend WithEvents lblImageName As System.Windows.Forms.Label
Friend WithEvents CmbCategory As System.Windows.Forms.ComboBox
Friend WithEvents CmdExecute As System.Windows.Forms.Button
Friend WithEvents CmdDelete As System.Windows.Forms.Button
Friend WithEvents Button1 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.cmbUserName = New System.Windows.Forms.ComboBox()
Me.CmbCategory = New System.Windows.Forms.ComboBox()
Me.cmdNext = New System.Windows.Forms.Button()
Me.cmdFirst = New System.Windows.Forms.Button()
Me.cmdPrev = New System.Windows.Forms.Button()
Me.CmdLast = New System.Windows.Forms.Button()
Me.txtImageCmnts = New System.Windows.Forms.TextBox()
Me.CmdExit = New System.Windows.Forms.Button()
Me.lblImageName = New System.Windows.Forms.Label()
Me.CmdExecute = New System.Windows.Forms.Button()
Me.CmdDelete = New System.Windows.Forms.Button()
Me.Button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Location = New System.Drawing.Point(24, 56)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(536, 432)
Me.PictureBox1.TabIndex = 0
Me.PictureBox1.TabStop = False
'
'cmbUserName
'
Me.cmbUserName.Location = New System.Drawing.Point(64, 24)
Me.cmbUserName.Name = "cmbUserName"
Me.cmbUserName.Size = New System.Drawing.Size(121, 21)
Me.cmbUserName.TabIndex = 1
'
'CmbCategory
'
Me.CmbCategory.Location = New System.Drawing.Point(200, 24)
Me.CmbCategory.Name = "CmbCategory"
Me.CmbCategory.Size = New System.Drawing.Size(121, 21)
Me.CmbCategory.TabIndex = 2
'
'cmdNext
'
Me.cmdNext.Location = New System.Drawing.Point(214, 504)
Me.cmdNext.Name = "cmdNext"
Me.cmdNext.TabIndex = 3
Me.cmdNext.TabStop = False
Me.cmdNext.Text = "Next"
'
'cmdFirst
'
Me.cmdFirst.Location = New System.Drawing.Point(48, 504)
Me.cmdFirst.Name = "cmdFirst"
Me.cmdFirst.TabIndex = 4
Me.cmdFirst.TabStop = False
Me.cmdFirst.Text = "First"
'
'cmdPrev
'
Me.cmdPrev.Location = New System.Drawing.Point(131, 504)
Me.cmdPrev.Name = "cmdPrev"
Me.cmdPrev.TabIndex = 5
Me.cmdPrev.TabStop = False
Me.cmdPrev.Text = "Previous"
'
'CmdLast
'
Me.CmdLast.Location = New System.Drawing.Point(297, 504)
Me.CmdLast.Name = "CmdLast"
Me.CmdLast.TabIndex = 6
Me.CmdLast.TabStop = False
Me.CmdLast.Text = "Last"
'
'txtImageCmnts
'
Me.txtImageCmnts.Location = New System.Drawing.Point(568, 128)
Me.txtImageCmnts.Multiline = True
Me.txtImageCmnts.Name = "txtImageCmnts"
Me.txtImageCmnts.Size = New System.Drawing.Size(224, 352)
Me.txtImageCmnts.TabIndex = 7
Me.txtImageCmnts.Text = ""
'
'CmdExit
'
Me.CmdExit.Location = New System.Drawing.Point(448, 504)
Me.CmdExit.Name = "CmdExit"
Me.CmdExit.TabIndex = 8
Me.CmdExit.Text = "Exit"
'
'lblImageName
'
Me.lblImageName.Location = New System.Drawing.Point(608, 56)
Me.lblImageName.Name = "lblImageName"
Me.lblImageName.Size = New System.Drawing.Size(160, 23)
Me.lblImageName.TabIndex = 9
Me.lblImageName.Text = "Img"
'
'CmdExecute
'
Me.CmdExecute.Location = New System.Drawing.Point(384, 24)
Me.CmdExecute.Name = "CmdExecute"
Me.CmdExecute.TabIndex = 10
Me.CmdExecute.Text = "Execute"
'
'CmdDelete
'
Me.CmdDelete.Location = New System.Drawing.Point(376, 512)
Me.CmdDelete.Name = "CmdDelete"
Me.CmdDelete.TabIndex = 11
Me.CmdDelete.Text = "Delete"
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(8, 512)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 12
Me.Button1.Text = "Button1"
'
'frmViewImage
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(804, 550)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.

CmdDelete, Me.CmdExecute, Me.lblImageName, Me.CmdExit, Me.txtImageCmnts, Me.CmdLast,

Me.cmdPrev, Me.cmdFirst, Me.cmdNext, Me.CmbCategory, Me.cmbUserName, Me.PictureBox1})
Me.Name = "frmViewImage"
Me.Text = "frmViewImage"
Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub CmdExit_Click(ByVal sender As System.Object, ByVal e As System.

EventArgs) Handles CmdExit.Click
Close()
End Sub

Private Sub frmViewImage_Load(ByVal sender As System.Object, ByVal e As System.

EventArgs) Handles MyBase.Load
If conn.State <> ConnectionState.Open Then
conn.Open()
End If

Try
myOleAdapter = New OleDbDataAdapter("select UserId,UserName from UserInfo

", conn)
myOleAdapter.Fill(mydataset, "UserInfo")
mydatatable = mydataset.Tables("UserInfo")

cmbUserName.DataSource = mydatatable
cmbUserName.DisplayMember = "UserName"
cmbUserName.ValueMember = "UserId"
conn.Close()
Catch
Console.WriteLine("Error Opening {0}", conn.DataSource)
End Try

Try
myOleAdapter = New OleDbDataAdapter("select CategoryName from Category",

conn)
myOleAdapter.Fill(mydataset, "category")
mydatatable = mydataset.Tables("Category")

CmbCategory.DataSource = mydatatable
CmbCategory.DisplayMember = "CategoryName"
conn.Close()
Catch ex As Exception
Console.Write("Error Opening {0}", conn.DataSource)
End Try

Try
myOleAdapter = New OleDbDataAdapter("select * from Users", conn)
myOleAdapter.Fill(mydataset, "Users")
mydatatable = mydataset.Tables("Users")

display()
callblob()
conn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
Console.WriteLine("Error opening {0}", conn.DataSource)
End Try

myCommand = New OleDbCommandBuilder(myOleAdapter)

End Sub

Private Sub cmdFirst_Click(ByVal sender As System.Object, ByVal e As System.

EventArgs) Handles cmdFirst.Click
iCurrentPosition = 0
showData()
End Sub

Private Sub CmdLast_Click(ByVal sender As System.Object, ByVal e As System.

EventArgs) Handles CmdLast.Click
iCurrentPosition = mydataset.Tables("Users").Rows.Count - 1
showData()
End Sub

Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.

EventArgs) Handles cmdNext.Click
If (iCurrentPosition < mydataset.Tables("Users").Rows.Count - 1) Then
iCurrentPosition = iCurrentPosition + 1
flag = False
showData()
cmdPrev.Enabled = True
Else
'cmdNext.Enabled = False
Try
MessageBox.Show("Last Record")
Catch efrm As Exception
MessageBox.Show(efrm.ToString)
End Try
End If

End Sub

Private Sub cmdPrev_Click(ByVal sender As System.Object, ByVal e As System.

EventArgs) Handles cmdPrev.Click
If (iCurrentPosition > 0) Then
iCurrentPosition = iCurrentPosition - 1
showData()
cmdNext.Enabled = True
Else
cmdPrev.Enabled = False
MessageBox.Show("First Record")

End If
End Sub

Private Sub CmdExecute_Click(ByVal sender As System.Object, ByVal e As System.

EventArgs) Handles CmdExecute.Click
Dim sql As String
txtImageCmnts.DataBindings.Clear()
lblImageName.DataBindings.Clear()

mydataset = New DataSet()
mydatatable = New DataTable()
sql = "select * from Users where CategoryName='" & CmbCategory.Text & "'"

If conn.State <> ConnectionState.Open Then
conn.Open()
End If

Try
myOleAdapter = New OleDbDataAdapter(sql, conn)
myOleAdapter.Fill(mydataset, "Users")
mydatatable = mydataset.Tables("Users")

display()
callblob()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub

Private Sub CmdDelete_Click(ByVal sender As System.Object, ByVal e As System.

EventArgs) Handles CmdDelete.Click

Dim dr As DataRow
dr = mydataset.Tables("Users").Rows(iCurrentPosition)

Try
dr.Delete()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

Try
myOleAdapter.Update(mydataset, "Users")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

mydataset.Tables("Users").AcceptChanges()
iCurrentPosition = mydataset.Tables("Users").Rows.Count - 1

showData()
curImage.Dispose()
DeleteAll()

End Sub

Private Sub display()
txtImageCmnts.DataBindings.Add(New Binding("Text", mydataset.Tables("Users"),

"cmnts"))
lblImageName.DataBindings.Add(New Binding("Text", mydataset.Tables("Users"),

"ImageName"))
savedImageName = lblImageName.Text
iCurrentPosition = 0
End Sub

Private Sub callblob()

' Display image
curImage = Image.FromFile(savedImageName)
PictureBox1.Image = curImage
PictureBox1.Invalidate()
End Sub

Private Sub showData()
lblImageName.Text = mydataset.Tables("Users").Rows(iCurrentPosition)("

ImageName").ToString()
txtImageCmnts.Text = mydataset.Tables("Users").Rows(iCurrentPosition)(" Cmnts

").ToString()
savedImageName = lblImageName.Text
callblob()
End Sub

Private Sub DeleteAll()
curImage.Dispose()
If flag = False Then
FileSystem.Kill(savedImageName)
flag = True
End If
End Sub
End Class

I hope anybody can solve this to me
And thanks in advance
--------------------------------
From: Deepak C.G

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>cKpdlMsZwE+FadsJSJ6hcA==</Id>
Nov 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.