logo
Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

Options
Go to last post Go to first unread
BenOK350  
#1 Posted : Friday, September 8, 2017 5:30:30 PM(UTC)
BenOK350

Rank: Newbie

Groups: Registered
Joined: 9/8/2017(UTC)
Posts: 2
United States

I need to get the name of a field when it is clicked / gets focus.

Does anybody know if there is an event or other way to detect when a form field is clicked / gets focus?

Thanks!
-Ben

PS - I found PdfViewer.FillForms.FocusChanged, but I can't see how to get any information about the form field (I need the form field name). Thanks in advance!

Edited by user Friday, September 8, 2017 5:42:16 PM(UTC)  | Reason: Not specified

Paul Rayman  
#2 Posted : Friday, September 8, 2017 8:02:33 PM(UTC)
Paul Rayman

Rank: Administration

Groups: Administrators
Joined: 1/5/2016(UTC)
Posts: 1,138

Thanks: 10 times
Was thanked: 133 time(s) in 130 post(s)
To get the name of the field in which the mouse was clicked you can use the following code:
Code:

pdfViewer1.MouseClick += PdfViewer1_MouseClick;

...

private void PdfViewer1_MouseClick(object sender, MouseEventArgs e)
{
	var pageIndex = pdfViewer1.PointInPage(e.Location);
	if (pageIndex < 0)
		return;
	PointF pt = pdfViewer1.ClientToPage(pageIndex, e.Location);
	int zOrder;
	var ctrl = pdfViewer1.Document.Pages[pageIndex].GetControlAtPoint(pt.X, pt.Y, out zOrder);
	if (ctrl != null)
	{
		string fieldName = string.Format("Alternate name: {0}\r\nFull name: {1}\r\nMapping name: {2}",
					ctrl.Field.AlternateName, ctrl.Field.FullName, ctrl.Field.MappingName);
		MessageBox.Show(fieldName);
	}

}


But that code does not able to show the field name if the field was focused by pressing 'Tab' key.

You can also try to use the FocusChanged event.
Unfortunately it has two problems.

1. That event does not provide any information which can help you to find the appropriate field. But you may use the following workaround:

Code:

pdfViewer1.FillForms.FocusChanged += FillForms_FocusChanged;
pdfViewer1.FillForms.Invalidate += FillForms_Invalidate;

...

bool isFocusChanged = false;
private void FillForms_FocusChanged(object sender, Patagames.Pdf.Net.EventArguments.FocusChangedEventArgs e)
{
	isFocusChanged = e.IsFocused;
}


private void FillForms_Invalidate(object sender, Patagames.Pdf.Net.EventArguments.InvalidatePageEventArgs e)
{
	if(isFocusChanged)
	{
		foreach(PdfControl ctrl in pdfViewer1.FillForms.InterForm.Controls)
		{
			//The page can be rotated, so we must to take it into account when calculation bounding box.
			float bl = Math.Min(ctrl.BoundRect.left, ctrl.BoundRect.right);
			float br = Math.Max(ctrl.BoundRect.left, ctrl.BoundRect.right);
			float bt = Math.Max(ctrl.BoundRect.top, ctrl.BoundRect.bottom);
			float bb = Math.Min(ctrl.BoundRect.top, ctrl.BoundRect.bottom);

			float il = Math.Min(e.Rect.left, e.Rect.right);
			float ir = Math.Max(e.Rect.left, e.Rect.right);
			float it = Math.Max(e.Rect.top, e.Rect.bottom);
			float ib = Math.Min(e.Rect.top, e.Rect.bottom);

			RectangleF boundingBox = new RectangleF(bl, bt, br - bl, bt - bb);
			RectangleF invalidateBox = new RectangleF(il, it, ir - il, it - ib);
			if(bounding.IntersectsWith(invalidateBox))
			{
				string fieldName = string.Format("Alternate name: {0}\r\nFull name: {1}\r\nMapping name: {2}",
					ctrl.Field.AlternateName, ctrl.Field.FullName, ctrl.Field.MappingName);
				//do something with found name
				break;

			}
		}
	}
	isFocusChanged = false;
}


2. The FocusChanged event does not fires by the Comboboxes and the Checkboxes.
But the Invalidate event still fires.
so you can try to process the KeyDown event and, if the 'Tab' key was pressed then do something in the same manner as shown above.

Edited by user Friday, September 8, 2017 8:11:08 PM(UTC)  | Reason: Not specified

BenOK350  
#3 Posted : Monday, September 11, 2017 9:44:32 AM(UTC)
BenOK350

Rank: Newbie

Groups: Registered
Joined: 9/8/2017(UTC)
Posts: 2
United States

Wonderful - thank you for taking the time to post such a thorough solution and explanation! I will give this a shot!!
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.