Solved

Webbrowser enable/disable goback and goforward

Posted on 2004-10-08
6
998 Views
Last Modified: 2008-01-09
I've got a webbrowser control in my application and now how too navigate and goback a page and goforward a page.

Now I want too disable the go back button in my program if there is no page to go back too and i want too disable the forward button in my program if there is no page too go forward too. How can I check if there are pages too go back or forward too ?
0
Comment
Question by:oostwijk
  • 3
  • 3
6 Comments
 
LVL 32

Expert Comment

by:Erick37
ID: 12258315
Private Const CSC_NAVIGATEFORWARD As Long = 1
Private Const CSC_NAVIGATEBACK As Long = 2
From MS:

"The WebBrowser control supports a CommandStateChange event, which is fired whenever the Forward or Back buttons need to be enabled or disabled. "

http://support.microsoft.com/default.aspx?scid=kb;EN-US;163282

' A Visual Basic application can also implement this
' functionality in this manner:  

Private Const CSC_NAVIGATEFORWARD As Long = 1
Private Const CSC_NAVIGATEBACK As Long = 2

Private Sub WebBrowser_CommandStateChange(ByVal Command As Long,ByVal Enable As Boolean)

       Select Case Command

           Case CSC_NAVIGATEBACK
               YourBackButtonName.Enabled = Enable

           Case CSC_NAVIGATEFORWARD
              YourForwardButtonName.Enabled = Enable

       End Select

End Sub
0
 

Author Comment

by:oostwijk
ID: 12261331
hmm, this is difficult for me too understand.. im sort of a VB newbie...

I understand that it checks if the back or forward button needs too be enabled.

Where in the code can I disable the buttons if the back or forward function isnt available ?
0
 
LVL 32

Expert Comment

by:Erick37
ID: 12261996
The webbrowser control fires the CommandStateChange event when the enabled state of the command buttons you are using to control navigation need to be changed.

For example if youur webbrowser control's name is WebBrowser1 then the event would be:

Private Sub WebBrowser1_CommandStateChange(ByVal Command As Long,ByVal Enable As Boolean)
           Select Case Command

           Case CSC_NAVIGATEBACK
               YourBackButtonName.Enabled = Enable '<<< This enables or disables the button.  
                                                                                 Replace "YourBackButtonName" with the name of your button

           Case CSC_NAVIGATEFORWARD
              YourForwardButtonName.Enabled = Enable

       End Select
End Sub

And if the button you use to navigate forward is called cmdNavFwd, then to navigate forward in the click event:

Private Sub cmdNavFwd_Click()
    Webbrowser1.GoForward
End Sub
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:oostwijk
ID: 12265161
ok, here is what I got, I've placed an webbrowser control on the form along with 2 commandbuttons named back and forward. And this is the code I have:

=========================================

Private Const CSC_NAVIGATEFORWARD As Long = 1
Private Const CSC_NAVIGATEBACK As Long = 2

Private Sub WebBrowser_CommandStateChange(ByVal Command As Long, ByVal Enable As Boolean)

       Select Case Command

           Case CSC_NAVIGATEBACK
               back.Enabled = Enable

           Case CSC_NAVIGATEFORWARD
               forward.Enabled = Enable

       End Select

End Sub

Private Sub back_Click()
WebBrowser1.GoBack
End Sub

Private Sub Form_Load()
WebBrowser1.Navigate "http://www.cnn.com"
End Sub

Private Sub forward_Click()
WebBrowser1.GoForward
End Sub

============================================

If you try this code you can see nothing at all happens with the commandbuttons, they are not enabled or disabled. What am I doing wrong ?
0
 
LVL 32

Accepted Solution

by:
Erick37 earned 50 total points
ID: 12265885
Private Sub WebBrowser1_CommandStateChange(ByVal Command As Long, ByVal Enable As Boolean)
----------------------------^ Don't forget the 1, Webbrowser1 is the name of your control.

Try correcting this and see if it works for you.

In the code edit window there is a dropdown box upper left which lists objects on your form.  Select Webbrowser1, and then the dropdown box upper right will list all the available events for the control.

Hope it helps!
0
 

Author Comment

by:oostwijk
ID: 12266325
That was it, thank you very much
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

816 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

10 Experts available now in Live!

Get 1:1 Help Now