Solved

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

Posted on 2011-10-01
20
532 Views
Last Modified: 2012-05-12
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
Comment
Question by:RobertoFreemano
  • 13
  • 7
20 Comments
 

Author Comment

by:RobertoFreemano
ID: 36899598
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36900422
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
 

Author Comment

by:RobertoFreemano
ID: 36900522
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
 

Author Comment

by:RobertoFreemano
ID: 36900529
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36900540
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
 

Author Comment

by:RobertoFreemano
ID: 36900562
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
 

Author Comment

by:RobertoFreemano
ID: 36900571
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36900577
Does Winter.jpg exist in the current folder?
0
 

Author Comment

by:RobertoFreemano
ID: 36900622
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
 

Author Comment

by:RobertoFreemano
ID: 36900682
Winter.jpg did exist in the current folder and it did work before i started playing with the code.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36902423
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
 

Author Comment

by:RobertoFreemano
ID: 36905257
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
 

Author Comment

by:RobertoFreemano
ID: 36905325
I'm thinking the issue is with this line of code: WebBrowser1.DocumentText = RichTextBox1.Text
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 36905972
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
 

Author Comment

by:RobertoFreemano
ID: 36906228
Thanks CC > It kind of works via WebBrowser1.Navigate("file:///C:\vb_test\" & filename)...
but this engulfs the WebBrowser1 and removes everything else :(

0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36906241
>but this engulfs the WebBrowser1 and removes everything else
What you mean?
0
 

Author Comment

by:RobertoFreemano
ID: 36906441
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
 

Author Comment

by:RobertoFreemano
ID: 36906443
Ironically, if I open the saved HTML file... it looks as it should.
browse.png
0
 

Author Closing Comment

by:RobertoFreemano
ID: 36906472
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
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36906504
Glad the problem is sorted.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now