vbNewbie2009
asked on
Working with Objects
I've been learning about watching for file creation; using FileSystemWatcher, and during part of the OnCreated process I'm trying to update a multi-line text box with what was read from the created text file. The issue is that the text box doesn't update.
I've attached the code that I'm using. How do I setup the objects so that I can pass the text read from the created file into the text box? This is all on a single form.
I've attached the code that I'm using. How do I setup the objects so that I can pass the text read from the created file into the text box? This is all on a single form.
Imports System.IO
Imports System.Security.Permissions
Imports System.Text.RegularExpressions
Public Class Form1
Private Shared re As New Regex("[\x00-\x1F\x7F-\xFF]+", RegexOptions.Compiled)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = "does this work"
Run()
End Sub
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
Private Shared Sub Run()
Dim watcher As New FileSystemWatcher()
watcher.Path = "C:\Programming"
watcher.NotifyFilter = NotifyFilters.FileName And Not NotifyFilters.Attributes
watcher.Filter = "*.txt"
watcher.IncludeSubdirectories = False
AddHandler watcher.Created, AddressOf OnCreated
watcher.EnableRaisingEvents = True
End Sub
Public Shared Sub OnCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
Dim ex As Exception
Dim strContents As String
Dim objReader As StreamReader
Try
System.Threading.Thread.Sleep(1000)
File.Move(e.FullPath, "C:\Programming\Archive\" & Replace(TimeOfDay, ":", "") & ".txt")
Try
objReader = New StreamReader("C:\Programming\Archive\" & Replace(TimeOfDay, ":", "") & ".txt")
strContents = objReader.ReadToEnd()
objReader.Close()
strContents = re.Replace(strContents, "")
MsgBox((strContents))
'######################################################
'########UNABLE TO SET TEXT BOX VALUE##################
'######################################################
Form1.TextBox1.Text = "yes this works"
Catch Ex2 As Exception
MsgBox(Ex2.Message)
End Try
Catch ex
Dim typeValue As String
typeValue = e.ChangeType.ToString
MsgBox("ChangeType: " & typeValue)
End Try
End Sub
End Class
ASKER
When I try using TextBox1.Text = "yes this works" ...i get a compile error referencing the following:
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class
What would happen if you remove Shared from both sub Run and sub OnCreated?
ASKER
I receive an exception:
"Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on.
"Cross-thread operation not valid: Control 'TextBox1' accessed from a thread other than the thread it was created on.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the suggestions; i'll be trying these out tomorrow.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Comments from Zhaolai and Hawkvalley1 each worked to populate the text object.
Documentation link by emoreau is an excellent resource!
Thanks to all!
Documentation link by emoreau is an excellent resource!
Thanks to all!
Me.TextBox1.Text = "yes this works"
or just
TextBox1.Text = "yes this works"