How to access the scrollbar of a webbrowser control in c#? - c#

My webbrowser control has to be disabled (enabled = false) so that the user can't click in it.
This also disables the access to the scrollbar so I'm thinking about creating another scrollbar next to the control that gets and passes its values from and to the webbrowser's scrollbar.
For that, I need to access the webbrowser scrollbar control. How can I find it ?
webbrowser.Controls.Count returns zero.

Hmm I don't know if there is any method to acccess the scrollbar position programmaticly. You can however, scroll by element name:
private void ScrollToElement(String elemName)
{
if (webBrowser1.Document != null)
{
HtmlDocument doc = webBrowser1.Document;
HtmlElementCollection elems = doc.All.GetElementsByName(elemName);
if (elems != null && elems.Count > 0)
{
HtmlElement elem = elems[0];
elem.ScrollIntoView(true);
}
}
}
Also, see this question for other possibilities.
EDIT:
See question Scrolling WebBrowser programatically sometimes doesn't work

Related

C# - Determine the focused RichTextBox in a Split Container of the Active MDI Child

I have a Form (Form2) that contains a Split Container, the RichTextBox is on the Left Panel and the WebBroswer is on the Right Panel.
I am showing the Form as a child of a MDIParent Form 1. What I wanted to do is copy the selected text of the active MDI Child. However due to the RichTextBox being inside a Split Container, I cannot target the RichTextBox and it returns nothing.
Form activeChild = this.ActiveMdiChild;
if (activeChild != null)
{
try
{
RichTextBox theBox = (RichTextBox)activeChild.ActiveControl;
if (theBox != null)
{
// Put the selected text on the Clipboard.
Clipboard.SetDataObject(theBox.SelectedText);
}
}
catch
{
MessageBox.Show("Unable to Copy to Clipboard");
}
}
The result is, the Message Box shows so that means I wasn't able to target the RTB properly. How can I get the current active RTB?
Since the ActiveControl being returned from the child form is the SplitContainer control, then we will need to go one level deeper and get the ActiveControl from that container in order to finally get the RichTextBox control. Note that we have to check for the object types and null along the way, in case a different control may be selected.
Here's one way to get the rich text box text (or an empty string if it's not selected):
var childSplitContainer = this.ActiveMdiChild?.ActiveControl is SplitContainer
? (SplitContainer)activeChildForm.ActiveControl
: null;
var splitContainerRTB = childSplitContainer?.ActiveControl is RichTextBox
? (RichTextBox)childSplitContainer.ActiveControl
: null;
Clipboard.SetDataObject(splitContainerRTB?.Text ?? string.Empty);

How to access focused element programmatically WPF C#

Wondering how should I access elements property which does have the focus. I have found the following code to find the focused element :
var focusedControl = FocusManager.GetFocusedElement(this);
This seems to work well, in debug "focusedcontrol" is the right element however I don't know how to access it programmatically. Something like :
focusedControl.Text = "txt";
The reason why I wanna do this - in the same window as the TextBoxes I have several Buttons which form a keypad. After hitting the Button (Focusable = False) I want to get reference to focused TextBox and insert the corresponding digit in TextBox.Text.
Thanks
Lukas
The GetFocusedElement() method returns IInputElement, not a TextBox.
Since FrameworkElement implements IInputElement, and Control (and TextBox) are derived from FrameworkElement, you can just cast the result to a TextBox yourself:
var focusedControl = FocusManager.GetFocusedElement(this);
var tBox = focusedControl as TextBox;
if (tBox != null)
tBox.Text = "txt";

How to select HtmlElement in WebBrowser

I`m having a webBrowser control with some span elements.
Now user clicks on one of them, I do some manipulations and after that I need to select clicked element in browser. How can I do this?
HtmlElement hitElement = exerciseTextEditorControl.Document.GetElementFromPoint(e.ClientMousePosition);
if (lastHitElement == null)
return;
// Some stuff elided
// Now need to make a selection of this element in web browser
I know I can use IHTMLTxtRange for selecting some text, but how can I do similar thing with HtmllElement?
Thanks in advance.
Found an answer. In case someone needs this as well:
public void SetSelectedElement(HtmlElement element)
{
IHTMLSelectionObject selection = HtmlDocument2.selection;
var htmlTxtRange = selection.createRange() as IHTMLTxtRange;
var iHtml = element.DomElement as IHTMLElement;
htmlTxtRange.moveToElementText(iHtml);
htmlTxtRange.select();
}

WPF Web Browser Control with Custom Scrollbar

I'm trying to make a custom scroll bar for Web browser Control.
I used a Scroll Bar Control for this,so i attached Scroll Bar Control to Web browser Control
use following code:
Doc = (mshtml.HTMLDocument)browser.Document;
Doc.parentWindow.document.body.style.overflow = "hidden";
mshtml.IHTMLElement2 ScrolablePlace= (mshtml.IHTMLElement2)Doc.getElementById("ScrolablePlace");
ScrollBar.ViewportSize = browser.ActualHeight;
ScrollBar.Maximum = ScrolablePlace.scrollHeight;
and while scroling scrolbar:
private void ScrollBar_Scroll(object sender, ScrollEventArgs e)
{
if (Doc != null)
{
Doc.parentWindow.scroll(0, (int)e.NewValue);
}
}
this is work,but ScrollBar.Maximum value is always larger than scroll bar place.dose ScrollBar.ViewportSize and ScrollBar.Maximum
set correctly?
I Hope i could explain my problem correctly with this image:
After some research, I've found that a scroll bar maximum property must calculate from this formula :
scrolbar.maximum=(maxsize-scrolbar.ViewportSize)+scrolbar.smallchanges
so i simply do this and it work fine:
ScrollBar.Maximum = ScrolablePlace.scrollHeight- browser.ActualHeight+ScrollBar.SmallChange;

Scroll to top of WebBrowser control

How can you scroll to the top of a web broswer control. The page I am loading has an iframe and the scroll bar is starting 20px down. It only happens in my application. I would like to auto scroll to the top.
Quick search yields: webBrowser1.Document.Window.ScrollTo(0, 200);
If what you mean is wanting to scroll the content of the iframe to the top, the following should help.
First you will need 2 things:
Add reference to C:\Program Files\Microsoft.NET\Primary Interop Assemblies\Microsoft.mshtml.dll
edit your <iframe> tag so that it has an id, eg: id="something"
Finally, the code:
HtmlElement ele = webBrowser1.Document.GetElementById("something");
mshtml.HTMLIFrameClass frame = ele.DomElement as mshtml.HTMLIFrameClass;
if (frame != null)
{
mshtml.HTMLDocumentClass doc = frame.document as mshtml.HTMLDocumentClass;
if (doc != null)
{
object i = 0;
mshtml.HTMLWindow2Class win = doc.frames.item(ref i) as mshtml.HTMLWindow2Class;
if(win != null)
win.scrollTo(0, 0);
}
}

Categories