|
-
Aug 17th, 2012, 05:59 AM
#1
Thread Starter
Lively Member
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
-
Aug 17th, 2012, 06:16 AM
#2
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.
-
Aug 17th, 2012, 07:08 AM
#3
Thread Starter
Lively Member
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
-
Aug 17th, 2012, 09:22 AM
#4
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?
-
Aug 17th, 2012, 11:02 AM
#5
Thread Starter
Lively Member
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.
-
Aug 17th, 2012, 11:02 AM
#6
Thread Starter
Lively Member
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.
-
Aug 17th, 2012, 12:10 PM
#7
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:
Private Sub Form1_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown Dim filePath = Path.Combine(Application.StartupPath, "HTMLPage1.htm") Dim fileContents = File.ReadAllText(filePath) RichTextBox1.Text = fileContents WebBrowser1.DocumentText = fileContents End Sub
-
Aug 17th, 2012, 01:10 PM
#8
Thread Starter
Lively Member
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.
-
Aug 17th, 2012, 08:34 PM
#9
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?
-
Aug 18th, 2012, 05:52 AM
#10
Thread Starter
Lively Member
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
-
Aug 18th, 2012, 07:07 AM
#11
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.
-
Aug 18th, 2012, 07:54 AM
#12
Thread Starter
Lively Member
Re: 2 Form controls reading from the same file
 Originally Posted by jmcilhinney
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|