Link to home
Start Free TrialLog in
Avatar of RobertoFreemano
RobertoFreemanoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

winform to save HTML file ....VB.NET 2010 xp

Hi Experts,

I'm a VB hobbiest, so my skills are very limited :(... I want to create a winForm editor app.
On it will be:
1. picturebox
2. Textbox
3. Label
4. Listbox
5. Button

If Button is clicked...saves a HTML file to c:\folder & c:\folder\img
I want to be able to change the picture or text at runtime then save it... if possible, what ever is in the listbox will save in a table on the HTML doc.

Thanks,
Roberto
I've tried a few examples off the web but struggling :(
Avatar of RobertoFreemano
RobertoFreemano
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Ok, so what I've achieved so far is a Winform with:
Form1> RichTextBox/ PictureBox & Button...  
Form2>WebBrowser1.

if I type into Form1> RichTextBox = message on Form2 appears (HOORAY)
what I'm trying to do next is, click PictureBox; OpenFileDialog1 to a stored image dir... once loaded; it'll update in Form2 [THIS BIT I'M STUCK & CURRENTLY RESEARCHING]

Any help would be appreciated :)

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Form2.Show()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim iWsave As System.IO.StreamWriter
            iWsave = File.AppendText("C:\vb_test\test" & ".html")
            iWsave.WriteLine(RichTextBox1.Text)
            iWsave.Close()
        Catch ex As Exception
            MsgBox("Error code 10!")
        End Try
    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        Form2.WebBrowser1.DocumentText = RichTextBox1.Text
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        PictureBox1.Image = System.Drawing.Image.FromFile("C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg")

        REM Form2.WebBrowser1.Document.ExecCommand("insertimage", False, "C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Winter.jpg")
    End Sub

End Class

Open in new window

Avatar of Nasir Razzaq
You need to insert a img tag in the html. Set the src attribute of the img tag to the path of the image file

http://www.w3schools.com/tags/tag_img.asp
Hi Experts,

You see my problem... I cannot view the image in WebBrowser1, nor does it display in native HTML from the file :(

Can some one please advise what I'm doing wrong please?

Thanks,
Roberto
shot1.tif
Hi CC,

Thanks for your help... Sorry, I have made some amendments before I saw your comment... here is my code as it is now.
Imports System.IO


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RichTextBox1.Text = "<html>" & vbCrLf _
            & "<head>" & vbCrLf & "<meta>" & vbCrLf & "<title>Title of your web page</title>" & vbCrLf _
        & "</head>" & vbCrLf & "<body>HTML web page contents" & vbCrLf & "<br>" & vbCrLf

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Dim iWsave As System.IO.StreamWriter
            iWsave = File.CreateText("C:\vb_test\test" & ".html")
            iWsave.WriteLine(RichTextBox1.Text)
            iWsave.Close()
        Catch ex As Exception
            MsgBox("Error code 10!")
        End Try

    End Sub

    Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
        WebBrowser1.DocumentText = RichTextBox1.Text
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

        ' Browser for File
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
        End If

        ' get the filename 
        Label1.Text = OpenFileDialog1.FileName
        Dim filename As String = IO.Path.GetFileName(OpenFileDialog1.FileName)
        Label1.Text = filename

        ' Display the image in the Browser View
        RichTextBox1.Text &= """<img src= filename width=500 height=300 border=0 alt=""Yo""""/>"""

        ' copy selected imagine to root directory
        Try
            File.Copy(OpenFileDialog1.FileName, "C:\vb_test\" & filename)
        Catch ex As Exception
            MessageBox.Show("File Already Exists")
        End Try



        WebBrowser1.Document.ExecCommand("insertimage", False, "C:\vb_test\" & filename)
    End Sub

    Private Function stFilePathAndName() As String
        Throw New NotImplementedException
    End Function

End Class

Open in new window

You are appending the img at the end of the existing html. Are you adding the </body></html> at any time before saving the file? Show us the html from the browser View Source command or the html file itself.
I noticed "" in the RTB and wondered if this was the issue?
So I've changed the code to:
RichTextBox1.Text &= "<img src= filename width=500 height=300 border=0 alt=""Yo""""/>"

but the issue still occurs :(
shot2.png
This is what is was before i started playing with it.

You're right, I moved the <img src=.... to another location.

Do you think this might be the problem?
<html>
<head>
<meta>
<title>Title of your web page</title>
</head>
<body>HTML web page contents
<br>
<img src="Winter.jpg" width="41" height="41" border="0" alt="Yo" />
</body>
</html>

Open in new window

Does Winter.jpg exist in the current folder?
ignore Winter.jpg...

I've modified the code to point to <img src= filename...>

Maybe my methods are all twisted. This is what I'm trying to do...

1. Click PictureBox1
2. OpenFileDialog opens
3. Select the file > displayed in PictureBox1
4. Label1 gets the filename + ext.
5. adds the line into RichTextBox1
6. copies the selected image to same dir as HTML file.
7. displays the image in WebBrowser1
8. Button1 saves the HTML file
Winter.jpg did exist in the current folder and it did work before i started playing with the code.
The problem you have with FileName is that you are adding it within the string

Change

RichTextBox1.Text &= "<img src= filename width=500 height=300 border=0 alt=""Yo""""/>"

to

RichTextBox1.Text &= "<img src=" & chr(34) & filename & chr(34) & " width=500 height=300 border=0 alt=""Yo""""/>"
Hi CC,

Thanks for your suggestion. I've attached 2 images; you code works with window2 from native browsing... which is ACE - Thanks a lot!!!!!

Window1 is still a mystery to me :(

Any ideas?
window1.png
window2.png
I'm thinking the issue is with this line of code: WebBrowser1.DocumentText = RichTextBox1.Text
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks CC > It kind of works via WebBrowser1.Navigate("file:///C:\vb_test\" & filename)...
but this engulfs the WebBrowser1 and removes everything else :(

>but this engulfs the WebBrowser1 and removes everything else
What you mean?
Sorry CC, I'm trying to explain it the best I can... please see images in seq on Bef/Mid/Aft

1. "Before" shows Text in WebBrowser1 control
2. "Mid" shows OpenFileBrowser once i've clicked on Picturebox1
3. "Aft" show the image displayed in wb1 control window... no text :(

I guess what I'm trying to accomplish is BrwoserView will show the user what to expect!
' Display the image in the Browser Vie
        RichTextBox1.Text &= "<html>" & vbCrLf _
             & "<head>" & vbCrLf & "<meta>" & vbCrLf & "<title>Title of your web page</title>" & vbCrLf _
         & "</head>" & vbCrLf & "<body>HTML web page contents" & vbCrLf & "<br>" & vbCrLf _
         & "<img src=" & Chr(34) & filename & Chr(34) & "  border=0 alt=""Yo""""/>" & vbCrLf _
         & "</html>"
        WebBrowser1.Navigate("file:///C:\vb_test\" & filename)

Open in new window

before.png
mid.png
aft.png
Ironically, if I open the saved HTML file... it looks as it should.
browse.png
Thanks CC,

Sorry, my brain was burning out :(

I simply (lucky guess) inserted your idea as follows:

"<img src=""file:///C:\vb_test\" & filename & Chr(34) & filename & Chr(34) & "  border=0 alt=""Yo""""/>" & vbCrLf

This did the trick ;)
Glad the problem is sorted.