none
WebBrowser SelectAll/Text size

    Question

  • How can I (i) select all the content of a webbrowser control and (ii) change its text size in code? In VB6 I would have used the ExecWB method, but this seems to have vanished in .net.
    Friday, December 09, 2005 4:29 PM

Answers

  • to set the textsize for all Internet Explorer Browsers



    That doesn't surprize me. Sorry your not handling my three lines of code. Going from your statement and example I'll give you this. At first I assumed Webbrowser1 as drawn on your form. Maybe you'll eyeball it this way. Where Webbrowser1 exists on a form with three generically named radio buttons. Try C & P with a form with those four elements and see if it doesn't still work.


        Private Enum iZooms As Long
            Small = 0
            Medium = 2
            Large = 4
        End Enum
        Private Sub Select_Zoom(ByVal Zoom As iZooms, Optional ByVal SelectText As Boolean = False)
            Dim MyWeb As Object
            MyWeb = Me.WebBrowser1.ActiveXInstance
            If SelectText = True Then MyWeb.ExecWB(17, 2)
            If Zoom = iZooms.Small Then MyWeb.execwb(19, 2, 0)
            If Zoom = iZooms.Medium Then MyWeb.execwb(19, 2, 2)
            If Zoom = iZooms.Large Then MyWeb.execwb(19, 2, 4)
        End Sub

        Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
            Select_Zoom(iZooms.Small)
        End Sub

        Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
            Me.Select_Zoom(iZooms.Medium)
        End Sub

        Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
            Me.Select_Zoom(iZooms.Large)
        End Sub

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Me.Select_Zoom(iZooms.Medium, True)
        End Sub

     


    Randy

    Tuesday, December 13, 2005 6:03 AM

All replies

  • I try the following, it does not work...


            For Each item As Windows.Forms.Control In Me.WebBrowser1.Controls
                item.Select()
                Dim f As New Drawing.Font(item.Font.FontFamily, item.Font.Size * 5, item.Font.Style, item.Font.Unit)
                item.Font = f
                item.Refresh()
            Next

     
    Friday, December 09, 2005 8:15 PM
    Moderator
  • No, because most web pages don't contain any controls. But thanks for trying.
    Friday, December 09, 2005 8:56 PM
  • Hey Ya go dude,

    Three lines of code to select all.


            Dim MyWeb As Object
            MyWeb = Me.WebBrowser1.ActiveXInstance
            MyWeb.ExecWB(17, 2)

     


    Now can you show me some VB 6 code that changes the size? I'm assuming it has something to do with the zoom?

    Randy

    Saturday, December 10, 2005 7:40 AM
  • Doesn't work for me. MyWeb is a WebBrowser object which doesn't seem to have any ExecWB method.

    Setting text size in VB6:


    wb1.ExecWB OLECMDID_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, CLng(z), Null
       

     


    OLECMDID_ZOOM = 19 and OLECMDIDEXECOPT_DONTPROMPTUSER =2.

    Although I did get some problems with this in that it seemed to set the textsize for all Internet Explorer Browsers on the user's system, not just for the web browser control.
    Sunday, December 11, 2005 6:12 PM
  • to set the textsize for all Internet Explorer Browsers



    That doesn't surprize me. Sorry your not handling my three lines of code. Going from your statement and example I'll give you this. At first I assumed Webbrowser1 as drawn on your form. Maybe you'll eyeball it this way. Where Webbrowser1 exists on a form with three generically named radio buttons. Try C & P with a form with those four elements and see if it doesn't still work.


        Private Enum iZooms As Long
            Small = 0
            Medium = 2
            Large = 4
        End Enum
        Private Sub Select_Zoom(ByVal Zoom As iZooms, Optional ByVal SelectText As Boolean = False)
            Dim MyWeb As Object
            MyWeb = Me.WebBrowser1.ActiveXInstance
            If SelectText = True Then MyWeb.ExecWB(17, 2)
            If Zoom = iZooms.Small Then MyWeb.execwb(19, 2, 0)
            If Zoom = iZooms.Medium Then MyWeb.execwb(19, 2, 2)
            If Zoom = iZooms.Large Then MyWeb.execwb(19, 2, 4)
        End Sub

        Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
            Select_Zoom(iZooms.Small)
        End Sub

        Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
            Me.Select_Zoom(iZooms.Medium)
        End Sub

        Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
            Me.Select_Zoom(iZooms.Large)
        End Sub

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Me.Select_Zoom(iZooms.Medium, True)
        End Sub

     


    Randy

    Tuesday, December 13, 2005 6:03 AM
  • Thanks very much, that works, including for selecting all the text, not sure what I was doing wrong before. It's strange that the TypeName of MyWeb is "WebBrowser", yet if you declare MyWeb as a WebBrowser, ExecWB can't be accessed. But anyway it works as you've written it.

    As expected, this does indeed set the text size for all Internet Explorer browsers which are subsequently opened. The solution I came up with before was to have a hidden webbrowser control in which I set the text size back to what it had been previously so that this wouldn't interfere with the user's normal settings. I think it worked, although it's not very attractive.

    The other thing I need to do is copy the selected text. I can do

    Clipboard.SetText(WebBrowser1.Document.Body.InnerText)
     
    for where the code to select all text has been executed, but it would be better if I could handle the situation where the user has selected just part of the page using the mouse. Any thoughts on how to do this? EDIT: It seems there's an OLECMDID_COPY constant (=12) which I can use to achieve this.


    Tuesday, December 13, 2005 10:41 AM