473,378 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

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
0 1884

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
4
by: Andreas Huber | last post by:
Hi there Is there a reason why .NET framework base classes implementing IDisposable (like e.g. System.ComponentModel.Component) do not prevent multiple calls to the protected virtual void...
4
by: Sunit Joshi | last post by:
Hello All I have an abstract class C1 with this: public abstract class C1 { protected bool m_Dirty; protected override void Dispose(bool disposing) { if(m_Dirty) WriteOuput();
6
by: Cody Powell | last post by:
Greetings all, I'm wondering if there's a rule of thumb related to objects that implement IDisposable, but whose Dispose() methods are actually marked as protected. If you dig around System.IO,...
16
by: Jeremy | last post by:
I was looking through some sample code that Microsoft provided for data access and I noticed that when they call the Dispose() method on the Connection object they cast it to IDisposable first. ...
1
by: Just Me | last post by:
I've been disposing whenever I create a bitmap or graphics object. Recently I developed a bug which turned out to be as follows: bmp=new Bitmap(... picBox.Image = bmp .. .. bmp.dispose...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
13
by: Grafix | last post by:
All - As we all know, Dispose method is reserved in C++ 8 and the expected syntax is to use ~MyClass(). In 1.1, we used to have following structure for Dispose class MyClass : IDisposable {...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.