Results 1 to 6 of 6

Thread: [RESOLVED] html in vb help

  1. #1

    Thread Starter
    Hyperactive Member jlt7's Avatar
    Join Date
    Jan 2006
    Posts
    413

    Resolved [RESOLVED] html in vb help

    How can I make a button in html that works just like a command button in visual basic.
    Ok, I have a webbrowser window with offline html in my program and I want a button or hyperlink button I guess in the html part, when its clicked will work just like if I would have clicked on a regular vb command button.
    Hope the way I worded this isn't to confusing.
    Last edited by jlt7; Apr 25th, 2006 at 08:53 PM.

  2. #2
    New Member
    Join Date
    Apr 2006
    Posts
    5

    Re: html in vb help

    Assuming Webbrowser control on vb form.

    VB Code:
    1. Private Sub OutputToBrowser()
    2.  
    3. dim Document as Object
    4.  
    5. WebBrowser1.Navigate2 "about:blank"
    6.  
    7. Do While (WebBrowser1.Busy) or (WebBrowser1.Document Is Nothing)
    8. DoEvents
    9. Loop
    10.  
    11. Set Document = WebBrowser1.Document
    12.  
    13. With Document
    14. .Open "text/html", "replace"
    15. .Write "<html><head></head><body>"
    16. .Write "<FORM name=testform1 action=http://button1"
    17. .Write " METHOD=POST>"
    18. .Write "<INPUT type=submit name=button1 value=button1>"
    19. .Write "</FORM>"
    20. .Write "<FORM name=testform2 action=http://button2"
    21. .Write " METHOD=POST>"
    22. .Write "<INPUT type=submit name=button2 value=button2>"
    23. .Write "</FORM>"
    24. .Write "</body></html>"
    25. .Close
    26. End With
    27.  
    28. Set Document = Nothing
    29.  
    30. Do While (WebBrowser1.ReadyState = READYSTATE_LOADING)
    31. DoEvents
    32. Loop
    33.  
    34. End Sub
    35.  
    36. Private Sub WebBrowser1_BeforeNavigate2(ByVal pDisp as Object, URL as Variant, Flags as Variant, ...., PostData as Variant, ....)
    37.  
    38. debug.print URL    'note URL value when html buttons are pressed
    39. ' note could probably use postdata if it could be read
    40.  
    41.  
    42. If Not pDisp.Document Is Nothing Then
    43. Cancel = True
    44. End If
    45.  
    46. End Sub

    hope this fits in with what you are doing
    Last edited by AUTO-MATE; Apr 26th, 2006 at 11:18 AM.

  3. #3

    Thread Starter
    Hyperactive Member jlt7's Avatar
    Join Date
    Jan 2006
    Posts
    413

    Re: html in vb help

    basically what I want is like a worded hyperlink on my vb webbrowser, but when clicked I don't want it to open another website I want it to open a form in vb just like a regular vb command button would do if it was clicked.
    I hope this makes sense. if this isn't possible then thats ok I was just trying something and thought it would look better that way.
    thank you for any help at all

  4. #4

    Thread Starter
    Hyperactive Member jlt7's Avatar
    Join Date
    Jan 2006
    Posts
    413

    Re: html in vb help

    also if this helps any I have my html code running in a module, this is what some of my code looks like to give you an idea of what I have.

    VB Code:
    1. txtHTML = txtHTML & " <tr>"
    2. txtHTML = txtHTML & " <td width=""140"" valign=""top"" align=""center"">"
    3. txtHTML = txtHTML & " <img src=" & .Fields("HTMLFrontCover") & " width=""140"" border=""0""><br>"

  5. #5
    New Member
    Join Date
    Apr 2006
    Posts
    5

    Re: html in vb help

    Sorry, I'll expand a little.

    You are not actually going to navigate to another website.

    You'll notice in the WebBrowser's BeforeNavigate2 subroutine that we are cancelling the navigation.

    If the browser has a document object then you are cancelling the navigation,
    checking the value in the passed in URL parameter/argument and
    using that to open your form or whatever.

    I have slightly modified the previous code to align more closely with what you seem to be doing.
    Note that the OutputToBrowser subroutine has an optional Fragment argument.
    If you want to pass in the complete HTML page string then pass false for this argument.

    The following code belongs on Form1 which has a WebBrowser control named WebBrowser. Form2 is just a blank form.

    I have attcahed the following as a complete project so you can try it.
    You can open Form2 from the hyperlink or the button.

    VB Code:
    1. Private Sub Form_Load()
    2.     Dim txtHTML As String
    3.    
    4.     txtHTML = "<a href=""/ShowForm2"">Open Form2</a>"
    5.     txtHTML = txtHTML & "<FORM name = TestForm ACTION=/ShowForm2"
    6.     txtHTML = txtHTML & " METHOD=POST>"
    7.     txtHTML = txtHTML & "<INPUT TYPE=SUBMIT NAME=ShowForm2 VALUE=Open&nbsp;Form2>"
    8.     txtHTML = txtHTML & "</FORM>"
    9.    
    10.     OutputToBrowser txtHTML, True
    11. End Sub
    12.  
    13. Private Sub OutputToBrowser(ByVal HTML As String, Optional ByVal Fragment As Boolean = True)
    14.     Dim Document As Object
    15.    
    16.     WebBrowser.Navigate2 "about:blank"
    17.     Do While (WebBrowser.Busy) Or (WebBrowser.Document Is Nothing)
    18.         DoEvents
    19.     Loop
    20.    
    21.     'write actual HTML
    22.     Debug.Print WebBrowser.LocationName
    23.    
    24.     With WebBrowser.Document
    25.         .Open "text/html", "replace"
    26.        
    27.         If Fragment Then .Write "<html><head/><body>"
    28.         .Write HTML
    29.         If Fragment Then .Write "</body></html>"
    30.  
    31.         .Close
    32.     End With
    33.    
    34.     Do While WebBrowser.ReadyState = READYSTATE_LOADING
    35.         DoEvents
    36.     Loop
    37. End Sub
    38.  
    39. Private Sub WebBrowser_BeforeNavigate2(ByVal pDisp As Object, URL As Variant, Flags As Variant, TargetFrameName As Variant, PostData As Variant, Headers As Variant, Cancel As Boolean)
    40.     Debug.Print URL
    41.    
    42.     If Not pDisp.Document Is Nothing Then
    43.         Cancel = True   'don't want to navigate to URL
    44.  
    45.         'just detect value of passed in URL
    46.         If InStr(1, URL, "ShowForm2") <> 0 Then
    47.             Form2.Show
    48.         End If
    49.     End If
    50. End Sub
    Attached Files Attached Files

  6. #6

    Thread Starter
    Hyperactive Member jlt7's Avatar
    Join Date
    Jan 2006
    Posts
    413

    Re: html in vb help

    Thank you VERY much Auto-Mate, I have been trying to figure out how to do that for months and just couldn't get it to work.
    Thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width