• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 549
  • Last Modified:

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 :(
0
RobertoFreemano
Asked:
RobertoFreemano
  • 13
  • 7
1 Solution
 
RobertoFreemanoAuthor Commented:
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

0
 
CodeCruiserCommented:
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
0
 
RobertoFreemanoAuthor Commented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
RobertoFreemanoAuthor Commented:
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

0
 
CodeCruiserCommented:
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.
0
 
RobertoFreemanoAuthor Commented:
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
0
 
RobertoFreemanoAuthor Commented:
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

0
 
CodeCruiserCommented:
Does Winter.jpg exist in the current folder?
0
 
RobertoFreemanoAuthor Commented:
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
0
 
RobertoFreemanoAuthor Commented:
Winter.jpg did exist in the current folder and it did work before i started playing with the code.
0
 
CodeCruiserCommented:
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""""/>"
0
 
RobertoFreemanoAuthor Commented:
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
0
 
RobertoFreemanoAuthor Commented:
I'm thinking the issue is with this line of code: WebBrowser1.DocumentText = RichTextBox1.Text
0
 
CodeCruiserCommented:
May be. Try saving the file to disk and then use the Navigate function of the webbrowser. You would need a url like "File://c:\winter.jpg"
0
 
RobertoFreemanoAuthor Commented:
Thanks CC > It kind of works via WebBrowser1.Navigate("file:///C:\vb_test\" & filename)...
but this engulfs the WebBrowser1 and removes everything else :(

0
 
CodeCruiserCommented:
>but this engulfs the WebBrowser1 and removes everything else
What you mean?
0
 
RobertoFreemanoAuthor Commented:
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
0
 
RobertoFreemanoAuthor Commented:
Ironically, if I open the saved HTML file... it looks as it should.
browse.png
0
 
RobertoFreemanoAuthor Commented:
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 ;)
0
 
CodeCruiserCommented:
Glad the problem is sorted.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 13
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now