Good day, our .Net developer has flown the coop and I am attempting to decipher the syntax utilized. I have found the issue is that TextSize.Width
= 104.7736 and TextFieldRect.Width
= 100 so when it hits the if()
statement in the DrawLeftTextField
method the error is thrown. Well my issue is, how do I change the TextFieldRect.Width
to account for longer than 100? Below is the syntax, and if further explanation is needed please let me know and I will do my best to explain further.
Edit --- what I am after here is to write text and the value of the text box on the top of the print page.
protected override void OnPrintPage(PrintPageEventArgs e)
{
// Run base code
base.OnPrintPage(e);
//Declare local variables needed
int printHeight = 0;
int printWidth = 0;
int rightMargin = 0;
PaperSize ps = default(PaperSize);
for (int ix = 0; ix <= PrinterSettings.PaperSizes.Count - 1; ix++)
{
if (PrinterSettings.PaperSizes[ix].Kind == PaperKind.A4)
{
ps = PrinterSettings.PaperSizes[ix];
DefaultPageSettings.PaperSize = ps;
break;
}
}
DefaultPageSettings.Margins.Top = PAGE_TOP_MARGIN;
DefaultPageSettings.Margins.Left = PAGE_LEFT_MARGIN;
DefaultPageSettings.Landscape = true;
var CurrentPageSettings = base.DefaultPageSettings;
printWidth = CurrentPageSettings.PaperSize.Height - CurrentPageSettings.Margins.Top - CurrentPageSettings.Margins.Bottom;
printHeight = CurrentPageSettings.PaperSize.Width - CurrentPageSettings.Margins.Left - CurrentPageSettings.Margins.Right;
m_leftMargin = CurrentPageSettings.Margins.Left; //X
rightMargin = CurrentPageSettings.Margins.Top; //Y
//Create a rectangle printing are for our document
m_PrintArea = new RectangleF(m_leftMargin, rightMargin, printWidth, printHeight);
// Get Normal Row Height
int charactersFitted = 0;
int linesFilled = 0;
SizeF TextSize = new SizeF();
StringFormat textFormat = new StringFormat();
//Tell it to Alignment Text in its rectangle
textFormat.Alignment = StringAlignment.Near;
textFormat.FormatFlags = StringFormatFlags.NoClip;
TextSize = e.Graphics.MeasureString("NORMALROW", SUB_HEADING_FONT, m_PrintArea.Size, textFormat, out charactersFitted, out linesFilled);
m_NormalRowHeight = (int)TextSize.Height + 3;
DrawTextValuePair(e, "Employee Name: ", Control.maintenance.txtEmployee.Text, true, m_leftMargin);
//More here
}
void DrawTextValuePair(PrintPageEventArgs e, string strText, string strValue, bool bDrawLine, int iLeftMargin)
{
m_iCurrentLocationY += m_NormalRowHeight;
Point LeftTextFieldLocation = new Point(iLeftMargin, m_iCurrentLocationY);
DrawLeftTextField(e, strText, new Rectangle(LeftTextFieldLocation, m_TextValuePairSize), StringAlignment.Near);
Point ValueFieldLocation = new Point(iLeftMargin + m_TextValuePairSize.Width, m_iCurrentLocationY);
DrawValueField(e, strValue, new Rectangle(ValueFieldLocation, m_TextValuePairSize), StringAlignment.Far);
if (bDrawLine)
{
int iRightEnd = iLeftMargin + (2 * m_TextValuePairSize.Width);
int iYLocation = m_iCurrentLocationY + m_NormalRowHeight;
e.Graphics.DrawLine(new Pen(Color.DarkBlue, 2), iLeftMargin, iYLocation, iRightEnd, iYLocation);
}
}
void DrawLeftTextField(PrintPageEventArgs e, string strText, Rectangle TextFieldRect, StringAlignment TextAligment)
{
int charactersFitted = 0;
int linesFilled = 0;
SizeF TextSize = new SizeF();
StringFormat textFormat = new StringFormat();
//Tell it to Alignment Text in its rectangle
textFormat.Alignment = TextAligment;
textFormat.LineAlignment = StringAlignment.Center;
textFormat.FormatFlags = StringFormatFlags.NoClip;
TextSize = e.Graphics.MeasureString(strText, NORMAL_TEXT_FONT, m_PrintArea.Size, textFormat, out charactersFitted, out linesFilled);
if ((TextSize.Width > TextFieldRect.Width) || (TextSize.Height > TextFieldRect.Height))
{
Debug.Assert(false);
return;
}
e.Graphics.DrawString(strText, NORMAL_TEXT_FONT, new SolidBrush(Color.Black), TextFieldRect, textFormat);
}
EDIT
I have hardcoded a length which is now not throwing an error and using the below syntax, however the text I want to display is just garbage, look at the screenshot
protected override void OnPrintPage(PrintPageEventArgs e)
{
// Run base code
base.OnPrintPage(e);
//Declare local variables needed
int printHeight = 0;
int printWidth = 0;
int rightMargin = 0;
PaperSize ps = default(PaperSize);
for (int ix = 0; ix <= PrinterSettings.PaperSizes.Count - 1; ix++)
{
if (PrinterSettings.PaperSizes[ix].Kind == PaperKind.A4)
{
ps = PrinterSettings.PaperSizes[ix];
DefaultPageSettings.PaperSize = ps;
break;
}
}
DefaultPageSettings.Margins.Top = PAGE_TOP_MARGIN;
DefaultPageSettings.Margins.Left = PAGE_LEFT_MARGIN;
DefaultPageSettings.Landscape = true;
var CurrentPageSettings = base.DefaultPageSettings;
printWidth = CurrentPageSettings.PaperSize.Height - CurrentPageSettings.Margins.Top - CurrentPageSettings.Margins.Bottom;
printHeight = CurrentPageSettings.PaperSize.Width - CurrentPageSettings.Margins.Left - CurrentPageSettings.Margins.Right;
m_leftMargin = CurrentPageSettings.Margins.Left; //X
rightMargin = CurrentPageSettings.Margins.Top; //Y
//Create a rectangle printing are for our document
m_PrintArea = new RectangleF(m_leftMargin, rightMargin, printWidth, printHeight);
// Get Normal Row Height
int charactersFitted = 0;
int linesFilled = 0;
SizeF TextSize = new SizeF();
StringFormat textFormat = new StringFormat();
//Tell it to Alignment Text in its rectangle
textFormat.Alignment = StringAlignment.Near;
textFormat.FormatFlags = StringFormatFlags.NoClip;
TextSize = e.Graphics.MeasureString("NORMALROW", SUB_HEADING_FONT, m_PrintArea.Size, textFormat, out charactersFitted, out linesFilled);
m_NormalRowHeight = (int)TextSize.Height + 3; //Row is bigger than text
// draw logo
Image logoImage = global::SoilProfile.Properties.Resources.SoilProfileIcon.ToBitmap();
// get the size of the image
Rectangle LogoRect = new Rectangle(m_leftMargin, m_leftMargin, (int)(logoImage.Width * 0.75), (int)(logoImage.Height * 0.8));
e.Graphics.DrawImage(logoImage, LogoRect);
//e.Graphics.DrawRectangle(Pens.LightBlue, LogoRect);
//Draw First Heading
Rectangle MainHeaderRect = new Rectangle();
DrawMainHeading(e, LogoRect, ref MainHeaderRect);
m_iCurrentLocationY = MainHeaderRect.Bottom + (m_NormalRowHeight);
// Document Name
textFormat.Alignment = StringAlignment.Near;
Rectangle TextRect = new Rectangle(m_leftMargin, m_iCurrentLocationY, m_SubHeaderTextFieldSize.Width, m_SubHeaderTextFieldSize.Height);
e.Graphics.DrawString("Document Name: " + m_PrintData.DocumentName, SUB_HEADING_FONT, new SolidBrush(Color.Black), TextRect, textFormat);
int LeftSubHeadingWidth = 200;
m_SubHeaderTextFieldSize = new Size(LeftSubHeadingWidth, m_NormalRowHeight);
m_TextValuePairSize = new Size(m_SubHeaderTextFieldSize.Width / 30, m_SubHeaderTextFieldSize.Height);
DrawTextValuePair(e, "Name: ", Control.maintenance.txtEmployee.Text, true, m_leftMargin);
DrawTextValuePair(e, "Phone #: ", Control.maintenance.txtPhone.Text, true, m_leftMargin);
int iCurrentLocationX = m_leftMargin;
int iAlphaStart_Y = m_iCurrentLocationY;
iCurrentLocationX += m_SubHeaderTextFieldSize.Width + 30;
DrawTextValuePair(e, "Hire Date: ", DControl.maintenance.dtpDate.Text, true, iCurrentLocationX);
DrawTextValuePair(e, "Manager: ", Control.maintenance.txtManager.Text, true, iCurrentLocationX);
DrawTextValuePair(e, "District: ", Control.maintenance.txtDistrict.Text, true, iCurrentLocationX);
//More code here
}
Okay, beyond syntax, I'd say the first question is figuring out the 'what' and 'why' of this code.
What it's doing:
... okay, so why is that error there?
... that's where they're checking to make sure the rectangle is big enough to fit the string.
So what are your options?
Hope that helps out.