Results 1 to 11 of 11

Thread: 2 Form controls reading from the same file

  1. #1
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    2 Form controls reading from the same file

    Hi guys,

    Is there any problem having 2 Form controls reading from the same file?
    I have a webbrowser and a textbox both reading from the same file but only the textBox displays the file contents.
    I tried commenting the textbox statement and now the webbrowser was reading the file.


    Code:
    pjDir = Form1.FBDialog.SelectedPath
                If My.Computer.FileSystem.FileExists(pjDir & "/index.html") Then
                    Form1.RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(pjDir & "/index.html")
                    Form1.WebBrowser1.DocumentText = My.Computer.FileSystem.ReadAllText(pjDir & "/index.html")
                End If
    Any help is welcome,
    Mike

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,230

    Re: 2 Form controls reading from the same file

    The controls aren't reading from the file. The ReadAllText method opens the file, reads the contents into a String, closes the file and then returns the String. The controls don't know anything about the file. The fact that you're reading the same file twice when you only need one String is very inefficient. You should be just calling ReadAllText once and assigning the result to a String variable and then assigning that to the appropriate properties of the controls. On a similar but slightly less inefficient note, you shouldn't be building the same file path three times. Give that a go and see what happens and, if there's still an issue then we'll deal with it then.

  3. #3
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: 2 Form controls reading from the same file

    you are right, thanks for the tip, however i changed my code but I still have exactly the same results.

    Code:
    pjDir = Form1.FBDialog.SelectedPath
                Dim dirMainFile As String = pjDir & "/index.html"
    
                If My.Computer.FileSystem.FileExists(dirMainFile) Then
                    Dim getfile As String = My.Computer.FileSystem.ReadAllText(dirMainFile)
                    Form1.RichTextBox1.Text = getfile
                    Form1.WebBrowser1.DocumentText = getfile
                End If

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,230

    Re: 2 Form controls reading from the same file

    Where exactly is that code located? Also, what happens if you load the WebBrowser before the RTB?

  5. #5
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: 2 Form controls reading from the same file

    Thanks for your quick response.
    The code is located in a module where I keep all my sub procedures. Inside the form1.vb I call the procedure through a Click event.

    If I put the Wbrowser statement before the RTB, it's the same result, only the RTB loads the file contents.
    But if I remove the RTB statement, the browser works.

  6. #6
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: 2 Form controls reading from the same file

    Thanks for your quick response.
    The code is located in a module where I keep all my sub procedures. Inside the form1.vb I call the procedure through a Click event.

    If I put the Wbrowser statement before the RTB, it's the same result, only the RTB loads the file contents.
    But if I remove the RTB statement, the browser works.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,230

    Re: 2 Form controls reading from the same file

    I really have no explanation for that. I will say one thing though, there really is no reason for that code to not be in the form itself. The code is specific to that form because it's referring directly to that form, so it should be in that form. Organising your code is good but you're over-organising for no gain. I would just try moving that code into the form itself and see if that makes any difference. I'm not sure that it will but I think that it's worth a try. I just tried this and it worked for me:
    vb.net Code:
    1. Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown
    2.     Dim filePath = Path.Combine(Application.StartupPath, "HTMLPage1.htm")
    3.     Dim fileContents = File.ReadAllText(filePath)
    4.  
    5.     RichTextBox1.Text = fileContents
    6.     WebBrowser1.DocumentText = fileContents
    7. End Sub

  8. #8
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: 2 Form controls reading from the same file

    I already tried that. The behaviour is still the same.
    there where a couple of times where they both would load ,
    something like 1/20 of the time, but the problem is still there.

    I'm using 2010 express not it would have anything to do with it, but i'm beginning to wonder.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,230

    Re: 2 Form controls reading from the same file

    That is peculiar. I can't think of a reason that that would happen. If you try the same thing in another project, does it happen the same way?

  10. #10
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: 2 Form controls reading from the same file

    I Just found out what the problem was. I have a TextChanged Event which is:
    Code:
    'WebBrowser1.DocumentText = RichTextBox1.Text
    I removed that, and everything loads fine. Somehow they are conflicting. I dont know why. Any guess?

    Thanks,
    Mike

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 05
    Location
    Sydney, Australia
    Posts
    81,230

    Re: 2 Form controls reading from the same file

    If you have a TextChanged event handler that loads the Text of the RTB as the DocumentText of the WB then what do you need the second line of your original code for at all? All you need to do is set the Text of the RTB and the TextChanged event will be raised. The code already in the event handler will take care of the rest.

  12. #12
    Lively Member
    Join Date
    Jul 12
    Posts
    111

    Re: 2 Form controls reading from the same file

    Quote Originally Posted by jmcilhinney View Post
    If you have a TextChanged event handler that loads the Text of the RTB as the DocumentText of the WB then what do you need the second line of your original code for at all? All you need to do is set the Text of the RTB and the TextChanged event will be raised. The code already in the event handler will take care of the rest.
    The thing is that the other function enables the user to open a project, the script looks for an existing index.html page
    and then loads them automaticaly in the RTB and the WBrowser.

    But I found a way around. I created a boolean var and set it to false. then added a btn to enable the textChanged event which sets the var to true.
    The user now has to enable the the Textchange, it's not exactly what I wanted but should suffice for now.
    Thanks,
    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •