DSE
asked on
Scroll a window through code
Hi,
Does anyone know how to scroll a window up and down through code.
To elaborate, I have a webbroweser control within my application that contains images. What I need to be able to do is whenver a new image is added to the ftp site and the webbrowser window refreshed to have the window scroll to the bottom so that the most recent image is displayed.
Thanks for any help,
David.
Does anyone know how to scroll a window up and down through code.
To elaborate, I have a webbroweser control within my application that contains images. What I need to be able to do is whenver a new image is added to the ftp site and the webbrowser window refreshed to have the window scroll to the bottom so that the most recent image is displayed.
Thanks for any help,
David.
You could use scrollinto view method of document object of webbrowser control.
In your project, set a reference to Microsoft HTML object library
on Documentcomplete event of webbrowserpaste this:
if (pdisp is webbrowser1.object) then
dim wbDoc as HTMLDocument
set wbDoc = webbrowser1.document
wbDoc.scrollintoview
end if
on Documentcomplete event of webbrowserpaste this:
if (pdisp is webbrowser1.object) then
dim wbDoc as HTMLDocument
set wbDoc = webbrowser1.document
wbDoc.scrollintoview
end if
sorry:
if (pdisp is webbrowser1.object) then
dim wbDoc as HTMLDocument
set wbDoc = webbrowser1.document
wbDoc.body.scrollintoview
end if
from MSDN:
scrollIntoView
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Description
Causes the object to scroll into view, aligning it at either the top or bottom of the window.
Syntax
object.scrollIntoView([sta rt])
Parameter Description
start Optional. Boolean value specifying whether to place the object at the top of the window or at the bottom. If TRUE, the method causes the object to scrolls so that its top is visible at the top of the window. If FALSE, the bottom of the object is visible at the bottom of the window. If no value is given, the object scrolls to the top by default.
Return Value
No return value.
Remarks
The following example causes the element to scroll into view within the window, placing it at either the top or bottom of the window. The method is useful for immediately showing the user the result of some action without requiring the user to manually scroll through the document to find the result. This example underlines the content of the fifth paragraph and scrolls it into view at the top of the window.
var coll = document.all.tags("P");
if (coll.length>=5) {
coll(4).style.textDecorati on = "underline";
coll(4).scrollIntoView(tru e);
}
Depending on the size of the given object and the current window, this method might not be able to put the item at the very top or very bottom, but will always position the object as close to the requested position as possible.
Applies To
A, ADDRESS, APPLET, AREA, B, BIG, BLOCKQUOTE, BR, BUTTON, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, COMMENT, DD, DFN, DIR, DIV, DL, DT, EM, EMBED, FIELDSET, FONT, FORM, H1, H2, H3, H4, H5, H6, HR, I, IFRAME, IMG, INPUT, KBD, LABEL, LEGEND, LI, LISTING, MAP, MARQUEE, MENU, OBJECT, OL, P, PLAINTEXT, PRE, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, WBR, XMP, TextRange
if (pdisp is webbrowser1.object) then
dim wbDoc as HTMLDocument
set wbDoc = webbrowser1.document
wbDoc.body.scrollintoview
end if
from MSDN:
scrollIntoView
--------------------------
Description
Causes the object to scroll into view, aligning it at either the top or bottom of the window.
Syntax
object.scrollIntoView([sta
Parameter Description
start Optional. Boolean value specifying whether to place the object at the top of the window or at the bottom. If TRUE, the method causes the object to scrolls so that its top is visible at the top of the window. If FALSE, the bottom of the object is visible at the bottom of the window. If no value is given, the object scrolls to the top by default.
Return Value
No return value.
Remarks
The following example causes the element to scroll into view within the window, placing it at either the top or bottom of the window. The method is useful for immediately showing the user the result of some action without requiring the user to manually scroll through the document to find the result. This example underlines the content of the fifth paragraph and scrolls it into view at the top of the window.
var coll = document.all.tags("P");
if (coll.length>=5) {
coll(4).style.textDecorati
coll(4).scrollIntoView(tru
}
Depending on the size of the given object and the current window, this method might not be able to put the item at the very top or very bottom, but will always position the object as close to the requested position as possible.
Applies To
A, ADDRESS, APPLET, AREA, B, BIG, BLOCKQUOTE, BR, BUTTON, CAPTION, CENTER, CITE, CODE, COL, COLGROUP, COMMENT, DD, DFN, DIR, DIV, DL, DT, EM, EMBED, FIELDSET, FONT, FORM, H1, H2, H3, H4, H5, H6, HR, I, IFRAME, IMG, INPUT, KBD, LABEL, LEGEND, LI, LISTING, MAP, MARQUEE, MENU, OBJECT, OL, P, PLAINTEXT, PRE, S, SAMP, SELECT, SMALL, SPAN, STRIKE, STRONG, SUB, SUP, TABLE, TBODY, TD, TEXTAREA, TFOOT, TH, THEAD, TR, TT, U, UL, VAR, WBR, XMP, TextRange
And take a look at this (as reference):
Contents Index Topic Contents
Previous Topic: Using the TextRange Object
Next Topic: Positioning
Finding Text in the Document
You can search for a given string of text in a document by using the findText method on a TextRange object. This method starts the search at the beginning of the range and, if it finds the string, positions the range so that it entirely encloses the string. The following example uses findText to search for each instance of the word "sample" and displays a message identifying how many instances it found.
var rng = document.body.createTextRa nge();
for (i=0; rng.findText("sample")!=fa lse; i++) {
rng.collapse( );
}
alert("There are " + i + " instances of sample");
Show Me
In the above example, the collapse method moves the start of the text range to the same position as the end point, ensuring that the same instance of "sample" is not counted twice.
You can carry out a global search and replace by using findText and the text property. The following example searches for each instance of "sample" and replaces it with the word "final".
var rng = document.body.createTextRa nge();
for (i=0; rng.findText("sample")!=fa lse; i++) {
rng.text = "final";
}
Show Me
You can select the text that you find, making it easier for the user to see, by using the select method. Similarly, you can scroll the text into the user's view by using the scrollIntoView method. Together, these methods give you a way to make the result of a search clearly visible to the user. The following JScript example searches for the word "sample", selects the word, and then scrolls the word to the top of the window.
var rng = document.body.createTextRa nge();
if (rng.findText("sample")==t rue) {
rng.select();
rng.scrollIntoView();
}
Show Me
Note that you can also create a text range from a selection made by the user. The createRange method on the selection object returns a text range. You can use the same methods and properties on this range as you do for ranges created using createTextRange.
-----
Contents Index Topic Contents
Previous Topic: Using the TextRange Object
Next Topic: Positioning
Finding Text in the Document
You can search for a given string of text in a document by using the findText method on a TextRange object. This method starts the search at the beginning of the range and, if it finds the string, positions the range so that it entirely encloses the string. The following example uses findText to search for each instance of the word "sample" and displays a message identifying how many instances it found.
var rng = document.body.createTextRa
for (i=0; rng.findText("sample")!=fa
rng.collapse( );
}
alert("There are " + i + " instances of sample");
Show Me
In the above example, the collapse method moves the start of the text range to the same position as the end point, ensuring that the same instance of "sample" is not counted twice.
You can carry out a global search and replace by using findText and the text property. The following example searches for each instance of "sample" and replaces it with the word "final".
var rng = document.body.createTextRa
for (i=0; rng.findText("sample")!=fa
rng.text = "final";
}
Show Me
You can select the text that you find, making it easier for the user to see, by using the select method. Similarly, you can scroll the text into the user's view by using the scrollIntoView method. Together, these methods give you a way to make the result of a search clearly visible to the user. The following JScript example searches for the word "sample", selects the word, and then scrolls the word to the top of the window.
var rng = document.body.createTextRa
if (rng.findText("sample")==t
rng.select();
rng.scrollIntoView();
}
Show Me
Note that you can also create a text range from a selection made by the user. The createRange method on the selection object returns a text range. You can use the same methods and properties on this range as you do for ranges created using createTextRange.
-----
ASKER
Thanks for all the suggestions...
I attempted to implement the ScrollIntoView method using the HTMLDocument object but it doesnt seem to actually do anything even though the code is run, i could see this in a step-through.
Is there something else I should have done?
Thanks,
David.
I attempted to implement the ScrollIntoView method using the HTMLDocument object but it doesnt seem to actually do anything even though the code is run, i could see this in a step-through.
Is there something else I should have done?
Thanks,
David.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That's a sample code, we need to modifify a little to meet your requirements exactrly.
ASKER
Thanks Richie for that code sample, it did exactly what I needed it to do. Just wondering if it was possible to scroll the page before all of the document elements have loaded as some images can take a while?
Thanks to everyone else for their sugesstions.
David.
Thanks to everyone else for their sugesstions.
David.
we don't have control over document until it is full loaded so i think we can't, or at least, in a decent and no infinity loop way...
if you get stuck with example, just ask...
if you get stuck with example, just ask...
Good luck!!