ok i am making a simple HTML editor and i added a small feature that allows the user to click a preview button which opens up another form window that has a webpage object in it.

the problem is, how do i get the html text to show converted into the new webpage object on the second form window

please help, im making great advances because of youguys!!!!

regards!

Recommended Answers

All 5 Replies

Save your HTML editor code to a file and put a webbrowser control on the preview form to open that file.

Here's a "Preview"-button skeleton:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  '
  Dim PreviewFileHTML As String
  Dim PreviewFileName As String

  PreviewFileName = "C:\temp.html"
  PreviewFileHTML = ""
  PreviewFileHTML = PreviewFileHTML & "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"" > """
  PreviewFileHTML = PreviewFileHTML & "<html>"
  PreviewFileHTML = PreviewFileHTML & "<head>"
  PreviewFileHTML = PreviewFileHTML & "</head>"
  PreviewFileHTML = PreviewFileHTML & "<body>"
  PreviewFileHTML = PreviewFileHTML & "<!-- Here goes the HTML from your editor -->"
  PreviewFileHTML = PreviewFileHTML & "Hello <b>HTML</b>!"
  PreviewFileHTML = PreviewFileHTML & "<!-- Here ends the HTML from your editor -->"
  PreviewFileHTML = PreviewFileHTML & "</body>"
  PreviewFileHTML = PreviewFileHTML & "</html>"
  ' Save the HTML preview
  My.Computer.FileSystem.WriteAllText(PreviewFileName, PreviewFileHTML, False)
  Form2.ShowDialog()

End Sub

Form2 is your preview form. Add a webbrowser control and a button control to close the form:

Public Class Form2

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Close()
  End Sub

  Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' Open the HTML preview  
    WebBrowser1.Navigate("C:\temp.html")
  End Sub

End Class

Of course you need some error handling etc.

ok this is getting somewhere, good.
but when i tried to run the debugger it highlighted this:

My.Computer.FileSystem.WriteAllText(PreviewFileName, PreviewFileHTML, False)

i dont know what to do now.
thanks for the help so far, regards!

Like I said, you have to catch exceptions etc.

You can change that line to: My.Computer.FileSystem.WriteAllText("C:\temp.html", PreviewFileHTML, False) . Just make sure you point to same file in both forms.

You may not have permission to write C-drives root (this happens in Vista) or that file exists and you can't write to it.

Let's modify the code a bit:

PreviewFileName = My.Computer.FileSystem.GetTempFileName & ".html" ' Create a temp file
' Here goes the "build preview file code"
Try
  My.Computer.FileSystem.WriteAllText(PreviewFileName, PreviewFileHTML, False)
Catch ex As Exception
   ' Handle exception. Make sure to check why you can't write to file (ex.Message)
End Try
If My.Computer.FileSystem.FileExists(PreviewFileName) Then
  Form2.PreviewFileName = PreviewFileName ' Now, pass the temp file's name to preview form
  Form2.ShowDialog()
  My.Computer.FileSystem.DeleteFile(PreviewFileName) ' Delete temp file
End If

and the "preview" form:

Public Class Form2

  Public PreviewFileName As String ' Get the name of the file

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Close()
  End Sub

  Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Try
      WebBrowser1.Navigate(PreviewFileName)
    Catch ex As Exception
      ' Handle exception
    End Try
  End Sub

End Class

If the debugger still highlights the same row, check ex.Message and let me know what the error is.

Private Sub PreviewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PreviewToolStripMenuItem.Click
        Dim PreviewFileName As String
        Dim PreviewFileHTML As String
        PreviewFileName = My.Computer.FileSystem.GetTempFileName & ".html" ' Create a temp file' Here goes the "build preview file code"Try  My.Computer.FileSystem.WriteAllText(PreviewFileName, PreviewFileHTML, False)Catch ex As Exception   ' Handle exception. Make sure to check why you can't write to file (ex.Message)End TryIf My.Computer.FileSystem.FileExists(PreviewFileName) Then  Form2.PreviewFileName = PreviewFileName ' Now, pass the temp file's name to preview form  Form2.ShowDialog()  My.Computer.FileSystem.DeleteFile(PreviewFileName) ' Delete temp fileEnd IfPreviewFileName = My.Computer.FileSystem.GetTempFileName & ".html" ' Create a temp file
        ' Here goes the "build preview file code"
        Try
            My.Computer.FileSystem.WriteAllText(PreviewFileName, [U]PreviewFileHTML[/U], False)
        Catch ex As Exception
            ' Handle exception. Make sure to check why you can't write to file (ex.Message)
        End Try
        If My.Computer.FileSystem.FileExists(PreviewFileName) Then
            form2.PreviewFileName = PreviewFileName ' Now, pass the temp file's name to preview form
            form2.ShowDialog()
            My.Computer.FileSystem.DeleteFile(PreviewFileName) ' Delete temp file
        End If
    End Sub
End Class

ok it opens without errors but it underlind previewfilehtml. and also it didnt display what whas in the textbox. hmmm
thanks for the help thogh, i think well get it

Put a break point where the underlined variable is and check what it contains. It should have at least (without line breaks):
<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN"" >
<html>
<head>
</head>
<body>
<!-- Here goes the HTML from your editor -->
Hello <b>HTML</b>!
<!-- Here ends the HTML from your editor -->
</body>
</html>
Make sure you get <html> and <body> tags right. From the original code: PreviewFileHTML = PreviewFileHTML & "Hello <b>HTML</b>!" should be replaced PreviewFileHTML = PreviewFileHTML & TextBox1.Text to get the text from your editor box.

Did you get any exception from the My.Computer.FileSystem.WriteAllText(PreviewFileName, PreviewFileHTML, False) ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.