Scroll to top of WebBrowser control - c#

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);
}
}

Related

Insert a textbox on a specific word page

I am iterating through all storyranges in a word document, to find shapes that do not adhere to company standards. When I find such a shape, I would like to add a textbox in the upper right corner of it's page. I only managed to add it to the first page so far.
foreach (Word.InlineShape shape in storyRange.InlineShapes)
{
if (shape.Type == Word.WdInlineShapeType.wdInlineShapePicture)
{
if (shape.Width != CurrentWordApp.CentimetersToPoints(Constants.LogoWidth))
{
anchor = shape.Range;
shapePageNumber = (int)shape.Range.Information[Word.WdInformation.wdActiveEndPageNumber];
AddMarkerToPage(shapePageNumber, anchor);
}
}
}
This is an excerpt from the AddMarkerToPage method. The only place I found to add a textbox, is the header. And the only place I found the header was through the section object. But section does not equal page.
Word.HeaderFooter header = anchor.Sections.First.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary];
if (!pageHasMarker)
{
Word.Shape tbx = header.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerFromLeft),
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerFromTop),
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerWidth),
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerHeight), pageRange);
tbx.Name = "errorBox";
tbx.TextFrame.TextRange.Text = Resources.Strings.txtDesignCheckHeaderLogo;
}
}
How can I either get to the header on the page the shape is on, or have another object that allows me to position a textbox on a specific page (I have the number available from the shape object)
Don't use the header, use the Document.Shapes collection.
Word.Shape tbx = <document>.Shapes.AddTextbox(Microsoft.Office.Core.MsoTextOrientation.msoTextOrientationHorizontal,
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerFromLeft),
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerFromTop),
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerWidth),
CurrentWordApp.CentimetersToPoints(Helper.errorMarkerHeight), anchor);
tbx.Name = "errorBox";
tbx.TextFrame.TextRange.Text = Resources.Strings.txtDesignCheckHeaderLogo;
I think you'll also need these:
tbx.RelativeVerticalPosition = WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;
tbx.RelativeHorizontalPosition = WdRelativeHorizontalPosition.wdRelativeHorizontalPositionColumn;

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;

Ajax Toolkit - Modal popal position resetting after internal update panel refreshes

I have a modal popup that can be dragged and moved. It also contains an ajaxUpdatePanel to update certain elements within it.
My problem is that if the modal popup has been moved around and the update panel fires, the popup will pop back to center position. Nothing breaks, it's just annoying to end users.
Any clues?
You may use script below:
var x = null;
var y = null;
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(InitializeRequest);
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoaded);
function pageLoaded(sender, args) {
var extender = $find("ModalPopupExtender1"); // extender's BehaviorID
if (extender != null && x != null && y != null) {
extender.set_X(parseInt(x));
extender.set_Y(parseInt(y));
}
}
function InitializeRequest(sender, args) {
var extender = $find("ModalPopupExtender1"); // extender's BehaviorID
x = extender._foregroundElement.style.left;
y = extender._foregroundElement.style.top;
}
If you'll close popup and open it again without full postback it will save previous position, but the next time it will be centered again.

How to access the scrollbar of a webbrowser control in 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

Categories