RaiseEvent not working correctly

G

Guest

I'm trying to rename some files in a directory. Pretty basic stuff -
renaming the files works fine but the problem I have is updated the text in
textbox. All I want to do is appendtext to a textbox. The problem is that
duplicate text occurs. Hopefully my code will explain better what I'm
trying to do and what i'm doing wrong :) Thanks for your help!

Public Class Form1
Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnRun.Click

If Me.txtBoxSource.Text = "" Or Me.txtBoxDest.Text = "" Then
MessageBox.Show("Please select Source and Destination!",
"Selection required!", MessageBoxButtons.OK, MessageBoxIcon.Hand)
Exit Sub
End If

If MessageBox.Show( _
"Are you sure you want to continue? Please make sure you have a
backup of the files you are about to rename.", _
"Are you sure?", MessageBoxButtons.YesNo,
MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then

' Set the file extension wildcard
If Not Me.txtBoxFileExt.Text = "" Then
objWorker.FileExtension = "*" & Me.txtBoxFileExt.Text
Else
objWorker.FileExtension = "*.*"
End If

AddHandler objWorker.Status, AddressOf WriteMessage

' Set Worker class level variables
objWorker.DestinationDirectory = Me.txtBoxDest.Text
objWorker.SourceDirectory = Me.txtBoxSource.Text
objWorker.PrependString = Me.txtboxPrepend.Text
objWorker.AppendString = Me.txtboxAppend.Text
objWorker.RenameFiles()
Else
Exit Sub
End If
End Sub


Private Sub WriteMessage(ByVal Message As String) Handles objWorker.Status
With txtBoxStatus
.AppendText(Message & vbCrLf)
End With
Application.DoEvents()
End Sub
End Class

Public Class Worker
Private m_DestDir As String
Private m_SrcDir As String
Private m_ApplyToFileExtention As Boolean
Private m_strPrepend As String
Private m_strAppend As String
Private m_RecurseSubDirs As Boolean
Private m_FileExtension As String
Private m_FileCount As Integer

Public Property DestinationDirectory() As String
Get
Return m_DestDir
End Get
Set(ByVal Value As String)
m_DestDir = Value
End Set
End Property

Public Property SourceDirectory() As String
Get
Return m_SrcDir
End Get
Set(ByVal Value As String)
m_SrcDir = Value
End Set
End Property

Public Property ApplyToFileExtention() As Boolean
Get
Return m_ApplyToFileExtention
End Get
Set(ByVal value As Boolean)
m_ApplyToFileExtention = value
End Set
End Property

Public Property RecurseSubDirs() As Boolean
Get
Return m_RecurseSubDirs
End Get
Set(ByVal value As Boolean)
m_RecurseSubDirs = value
End Set
End Property

Public Property FileExtension() As String
Get
Return m_FileExtension
End Get
Set(ByVal value As String)
m_FileExtension = value
End Set
End Property

Public Property FileCount() As Integer
Get
Return m_FileCount
End Get
Set(ByVal value As Integer)
m_FileCount = value
End Set
End Property

Public Property PrependString() As String
Get
Return m_strPrepend
End Get
Set(ByVal value As String)
m_strPrepend = value
End Set
End Property

Public Property AppendString() As String
Get
Return m_strAppend
End Get
Set(ByVal value As String)
m_strAppend = value
End Set
End Property


Public Event Status(ByVal Message As String)

Sub RenameFiles()
Try
Dim dir As New DirectoryInfo(SourceDirectory)
Dim strFile As FileInfo

If RecurseSubDirs = True Then
' Get files in SourceDirectory
For Each strFile In dir.GetFiles(FileExtension)
If Not System.IO.File.Exists(DestinationDirectory & "\"
& PrependString & Path.GetFileNameWithoutExtension(strFile.Name) &
AppendString) Then
'File.MoveTo(DestinationDirectory & "\" &
PrependString & Path.GetFileNameWithoutExtension(strFile.Name) & AppendString)
RaiseEvent Status(DestinationDirectory & "\" &
PrependString & Path.GetFileNameWithoutExtension(strFile.Name) & AppendString)
Else
RaiseEvent Status("File Already Exists - " &
DestinationDirectory & "\" & PrependString &
Path.GetFileNameWithoutExtension(strFile.Name) & AppendString)
End If
Next

' Recurse the sub directories in SourceDirectory
For Each subDirectories As String In
Directory.GetDirectories(SourceDirectory)
ListSubDirFiles(subDirectories)
Next
Else
' Do not recurse sub directories just get files in
SourceDirectory
For Each strFile In dir.GetFiles(FileExtension)
If Not System.IO.File.Exists(DestinationDirectory & "\"
& PrependString & Path.GetFileNameWithoutExtension(strFile.Name) &
AppendString) Then
strFile.MoveTo(DestinationDirectory & "\" &
PrependString & Path.GetFileNameWithoutExtension(strFile.Name) & AppendString)
RaiseEvent Status("Why not work?!?!?!")
Else
RaiseEvent Status("File Already Exists - " &
DestinationDirectory & "\" & PrependString &
Path.GetFileNameWithoutExtension(strFile.Name) & AppendString)
End If
Next
End If

Catch ex As Exception
'MessageBox.Show(ex.Message.ToString)
End Try
End Sub
End Class
 
G

Guest

You don't need to use both AddHandler and declare WriteMessage as Withevents
and including a handles clause. Just remove your line of code that calls
AddHandler objWorker.Status, AddressOf WriteMessage. This is adding a
duplicate event handler linked to the WriteMessage routine.
 
G

Guest

some answers are so simple... Thanks!

TrtnJohn said:
You don't need to use both AddHandler and declare WriteMessage as Withevents
and including a handles clause. Just remove your line of code that calls
AddHandler objWorker.Status, AddressOf WriteMessage. This is adding a
duplicate event handler linked to the WriteMessage routine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top