Set Size of TextFieldRect.Width

99 views Asked by At

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
}

enter image description here

1

There are 1 answers

3
Kevin On

Okay, beyond syntax, I'd say the first question is figuring out the 'what' and 'why' of this code.

What it's doing:

  • It's getting a string that it wants to print
  • It's getting a rectangle it wants to print it at
  • It's making sure the string that it wants to print will fit in that rectangle

... okay, so why is that error there?

if ((TextSize.Width > TextFieldRect.Width) || (TextSize.Height > TextFieldRect.Height))
{
    Debug.Assert(false);
    return;
}

... that's where they're checking to make sure the rectangle is big enough to fit the string.

So what are your options?

  1. Change the code that, if something too long is passed in, instead of erroring, it simply shortens what it tries to print
  2. Figure out what's calling the function with a rectangle to small to fit the text, and change it so that it has a larger rectangle or a smaller string
  3. See if there's something that reduced the size of the rectangle recently (such as printer margin/size changes.

Hope that helps out.