-
Jul 4th, 2017, 01:23 PM
#1
Thread Starter
Junior Member
[RESOLVED POORLY] Setting a PDF embedded inside a WebBrowser as the ActiveControl
Hi all,
I have a Windows Form with a TabControl, one of the tabs is named Tab_Help, and it contains a WebBrowser control which navigates to a PDF file that I generate.
My goal is to have the the PDF embedded inside the WebBrowser gain focus when the user selects the Help Tab. As it stands now, users need to click the PDF to begin scrolling through it, and I'd like to make it easier by shifting focus to the PDF the moment the Help tab is selected.
I set the WebBrowser to load the PDF on Form1 load, and aside from the fact that users need to click it before they can scroll through it, the PDF itself works perfectly.
vb.net Code:
Private Sub Form1.Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate("C:\temp\pdftest.pdf#toolbar=0") ' Works perfectly
End Sub
Private Sub TabControl_SelectedIndexChanged() Handles TabControl.SelectedIndexChanged
If TabControl.SelectedIndex = TabControl.TabPages.IndexOf(Tab_Log) Then
ActiveControl = Textbox_Log ' This works perfectly, allows scrolling immediately
ElseIf TabControl.SelectedIndex = TabControl.TabPages.IndexOf(Tab_Help) Then
ActiveControl = WebBrowser1 ' Doesn't work
'WebBrowser1.Focus() ' Also doesn't work
'WebBrowser1.Document.Focus() ' Also doesn't work
End If
End Sub
Any suggestions?
Last edited by RichardG; Jul 5th, 2017 at 01:15 PM.
Reason: Kinda resolved, I guess
-
Jul 5th, 2017, 12:39 PM
#2
Thread Starter
Junior Member
Re: Setting a PDF embedded inside a WebBrowser as the ActiveControl
Okay I have a solution, but it's pretty ugly... I'd certainly welcome any advice on how I could do this in a less..... stupid... way....
So the first thing I did was add an invisible button in my Help Tab.
Then I set the TabIndex of the invisible button to 9, and set the TabIndex of the WebBrowser to 10.
(Selecting 9 and 10 is arbitrary, but high numbers gives me room to add several more controls to the help tab in the future without messing things up.)
When the user selects the help tab, I set focus on the invisible button, then Tab into the WebControl, then Shift-Tab back to my button, then Tab into the Webcontrol again.
I needed to do it this way because simply selecting the button and tabbing once into the Webcontrol worked every other time. If I had to guess why, i'd say the WebControl has it's own internal tabbing system that would not reset.
Here's the new code in all it's ugly glory:
vb.net Code:
Private Sub Form1.Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load WebBrowser1.Navigate("C:\temp\pdftest.pdf#toolbar=0") ' Works perfectly End Sub Private Sub TabControl_SelectedIndexChanged() Handles TabControl.SelectedIndexChanged If TabControl.SelectedIndex = TabControl.TabPages.IndexOf(Tab_Log) Then ActiveControl = Textbox_Log ' This works perfectly, allows scrolling immediately ElseIf TabControl.SelectedIndex = TabControl.TabPages.IndexOf(Tab_Help) Then Button_WebControl_Focus_Helper.Focus() SendKeys.Send("{TAB}") ' Tab into the Webbrowser once SendKeys.Send("+{TAB}") ' +{TAB} is Shift-Tab (back to the invisible button) SendKeys.Send("{TAB}") ' Tab into the Webbrowser again End If End Sub
Last edited by RichardG; Jul 5th, 2017 at 12:44 PM.
Reason: Found a slightly less stupid method
-
Jul 5th, 2017, 12:46 PM
#3
Re: Setting a PDF embedded inside a WebBrowser as the ActiveControl
Hi,
I think it's..
Code:
'label1.Caption ist you Pdf File and Path
WebBrowser1.Navigate "" & Label1.Caption
regards
Chris
-
Jul 5th, 2017, 01:12 PM
#4
Thread Starter
Junior Member
Re: Setting a PDF embedded inside a WebBrowser as the ActiveControl
WebBrowser.Navigate("C:\temp\pdftest.pdf#toolbar=0") works perfectly, my issue was regarding difficulties focusing the WebBrowser component so that users would be able to scroll through the PDF without clicking the WebBrowser control first.
-
Jul 5th, 2017, 01:42 PM
#5
Re: [RESOLVED POORLY] Setting a PDF embedded inside a WebBrowser as the ActiveControl
Oh sorry,
did not see that
try this...
Code:
With Form1.WebBrowser1.Document.All("Text1.Text")' or your Button
.focus()
.RaiseEvent("Click")
End With
regards
Chris
-
Jul 5th, 2017, 04:07 PM
#6
Thread Starter
Junior Member
Re: [RESOLVED POORLY] Setting a PDF embedded inside a WebBrowser as the ActiveControl
Thanks for trying, but I get a NullReferenceException on Me.WebBrowser1.Document.All("Text1.Text") no matter what I put inside All("")
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|