Solved

Working with Objects

Posted on 2010-09-12
9
274 Views
Last Modified: 2012-05-10
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.
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

Open in new window

0
Comment
Question by:vbNewbie2009
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33659313
It is:

Me.TextBox1.Text = "yes this works"

or just

TextBox1.Text = "yes this works"

0
 
LVL 1

Author Comment

by:vbNewbie2009
ID: 33659329
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
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33659366
What would happen if you remove Shared from both sub Run and sub OnCreated?

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 1

Author Comment

by:vbNewbie2009
ID: 33659376
I receive an exception:
"Cross-thread operation not valid:  Control 'TextBox1' accessed from a thread other than the thread it was created on.
0
 
LVL 9

Accepted Solution

by:
Hawkvalley1 earned 200 total points
ID: 33659466
This is because your watcher is on a diff thread and you need a delegate to talk to the textbox.

Private Delegate Sub UpdateTextDelegate(ByVal txt As String)

Private Sub UpdateText(ByVal txt As String)
    textbox1.Text = txt
End Sub

Public Sub OnCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)
    textbox1.Invoke(New UpdateTextDelegate(AddressOf UpdateText), e.Name)
End Sub
0
 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 200 total points
ID: 33659490
After you created FileSystemWatcher object, add the following line,

 watcher.SynchronizingObject = Me

0
 
LVL 1

Author Comment

by:vbNewbie2009
ID: 33659595
Thanks for the suggestions; i'll be trying these out tomorrow.
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 100 total points
ID: 33660688
0
 
LVL 1

Author Closing Comment

by:vbNewbie2009
ID: 33668676
Comments from Zhaolai and Hawkvalley1 each worked to populate the text object.  

Documentation link by emoreau is an excellent resource!

Thanks to all!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Expression Evaluater 3 46
How to duplicate form in Visual Studio 2015 2 98
Visual Studio hangs on running project 6 62
Get sourcecode path 14 48
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question