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
|