Getting the Page Number at the Current Scroll Location
When using TX Text Control in read-only mode, it might be required to display the page number of the currently visible page. In order to get the page number at the current input position, you just need to get the appropriate property from the InputPosition object. But when scrolling through the pages, the actual input position is not changed, so that this property can't be used to get the page number. But thanks to the flexible and powerful class library, it is very easy to retrieve the page…

When using TX Text Control in read-only mode, it might be required to display the page number of the currently visible page. In order to get the page number at the current input position, you just need to get the appropriate property from the InputPosition object.
But when scrolling through the pages, the actual input position is not changed, so that this property can't be used to get the page number. But thanks to the flexible and powerful class library, it is very easy to retrieve the page number of the currently visible page.
In order to get the page number, we use the LineCollection to get the line in the middle of the visible TextControl. TX Text Control automatically factors the current scroll location into the calculation, so that we just need to pass the half of the control size to the GetItem method. It returns the Line object in the middle of the visible document part. And the Line object provides the according page number among other information.
private int GetPageAtScrollPosition()
{
return textControl1.Lines.GetItem(
new Point(0, textControl1.Height / 2)).Page;
}
We just need to update the information using the VScroll event:
private void textControl1_VScroll(object sender, EventArgs e)
{
int iPageNumber = GetPageAtScrollPosition();
Console.WriteLine("Page " +
iPageNumber.ToString() +
" of " +
textControl1.Pages.ToString());
Console.WriteLine("Section " +
textControl1.GetPages()[iPageNumber].Section +
" of " +
textControl1.Sections.Count.ToString());
}Related Posts
Create a Table of Contents in Windows Forms using C#
This article explains how to create a table of contents in Windows Forms using the ribbon or programmatically. Creating a table of contents is required to organize large documents.
Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub
This article gives a quick overview of the new repositories, their structure and our plans for the future.
ASP.NETJavaScriptDocument Editor
Detect Toggle Button Changes Using a MutationObserver
This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…
Two Ways to Restart Numbered Lists in TX Text Control
In TX Text Control, numbered lists are continued by default and need to be reset when required. There is more than one way if you want to restart numbered lists in a document. In this article, two…
Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More
This article shows how to disable CTRL + MOUSE WHEEL, implement zooming with keyboard and reset the zoom factor to its default value.
