Resolved draw string

aronmatthew

Active member
Joined
Aug 5, 2019
Messages
41
Programming Experience
Beginner
Using draw string method with scroll is only working with an explicit string contained in quotes
 
So you mean to say that this works:
C#:
DrawString(..., "Hello, World", ...);
but this doesn't work?
C#:
string s = "Hello, World";
DrawString(..., s, ...);

I find that kind of hard to believe. Can you share the minimal code that reproduces the problem?
 
So you mean to say that this works:
C#:
 DrawString(..., "Hello, World", ...);
but this doesn't work?
C#:
 string s = "Hello, World"; DrawString(..., s, ...);
I find that kind of hard to believe. Can you share the minimal code that reproduces the problem?


scroll destroys text
C#:
  foreach (_action act in currentClaim.ClaimCaseActions)
  {
     
     
       //e.Graphics.DrawString("10-17-82", drawFont, drawBrush, xPosDate, yPos);
     
      e.Graphics.DrawString(_Edate.dateToString(act.Date), drawFont, drawBrush, xPosDate, yPos);
    
      e.Graphics.DrawLine(linePen,
            xPosDate,
             yPos,
             actionPanel.Right,
            yPos);
      yPos += 15;


  }
scroll text ok
C#:
  foreach (_action act in currentClaim.ClaimCaseActions)
  {
     
     
       e.Graphics.DrawString("10-17-82", drawFont, drawBrush, xPosDate, yPos);
     
     // e.Graphics.DrawString(_Edate.dateToString(act.Date), drawFont, drawBrush, xPosDate, yPos);
    
      e.Graphics.DrawLine(linePen,
            xPosDate,
             yPos,
             actionPanel.Right,
            yPos);
      yPos += 15;


  }
drawString implicit.png
drawString explicit.png
 
I suspect that it is your _Edate.dateToString(act.Date) which is giving you bad values.
What happens if you do:
C#:
string displayDate = _Edate.dateToString(act.Date);
System.Diagnostics.Debug.WriteLine(displayDate);
e.Graphics.DrawString(displayDate, drawFont, drawBrush, xPosDate, yPos);

What do you see in the Output pane in the debugger?
 
I suspect that it is your _Edate.dateToString(act.Date) which is giving you bad values.
What happens if you do:
C#:
string displayDate = _Edate.dateToString(act.Date);
System.Diagnostics.Debug.WriteLine(displayDate);
e.Graphics.DrawString(displayDate, drawFont, drawBrush, xPosDate, yPos);

What do you see in the Output pane in the debugger?

That method returns a good string
 
here is the date to string method
C#:
 public static string dateToString(_date rh)
 {

     if (isEmpty(rh))
         return "";

     int m = int.Parse(rh.Month);
     int d = int.Parse(rh.Day);
     int y = int.Parse(rh.Year);

     if (m < 10)
         rh.Month = "0" + rh.Month;

     if (d < 10)
         rh.Day = "0" + rh.Day;

     if (y < 10)
         rh.Year = "0" + rh.Year;


     return rh.Month + '-' + rh.Day + '-' + rh.Year;

 }
[\code]
 
formatting in the from string method instead of the to string method odd as it seems works:
   public static _date dateFromString(string rh)
   {

       try
       {
           _date d = new _date();
           string[] sp = rh.Split('-');

           int month = int.Parse(sp[0]);
           int day = int.Parse(sp[1]);
           int year = int.Parse(sp[2]);

           if (month < 10)
               d.Month = '0' + month.ToString();
           else d.Month = month.ToString();
           if (day < 10)
               d.Day = '0' + day.ToString();
           else
               d.Day = day.ToString();
           if (year < 10)
               d.Year = '0' + year.ToString();
           else
               d.Year = year.ToString();
          
           return d;
       }
       catch (Exception ex)
       {

           return new _date();


       }
   }
 
*sigh*

Assuming that your _date is a class and not a struct, then lines 11, 14, and 17 will destructively keep adding more leading zeros into rh.Month, rh.Day, and rh.Year each time you repaint because of the scroll event.

C#:
public static string dateToString(_date rh)
 {
     if (isEmpty(rh))
         return "";

     int m = int.Parse(rh.Month);
     int d = int.Parse(rh.Day);
     int y = int.Parse(rh.Year);

     if (m < 10)
         rh.Month = "0" + rh.Month;

     if (d < 10)
         rh.Day = "0" + rh.Day;

     if (y < 10)
         rh.Year = "0" + rh.Year;

     return rh.Month + '-' + rh.Day + '-' + rh.Year;
}

Better could would have been something like:
C#:
public static string dateToString(_date rh)
 {
     if (isEmpty(rh))
         return "";

     int m = int.Parse(rh.Month);
     int d = int.Parse(rh.Day);
     int y = int.Parse(rh.Year);

     return $"{m:d2}-{d:d2}-{y:d2}";
}

The "d2" formats out the value as a decimal number 2 digits. Leading zeros will be inserted to fill out the 2 digits if necessary.
 
That method returns a good string

It likely returns a good string on the first iteration time the paint happens, but if _date is a class rather than a struct, then the method will keep adding leading zeros.
 
formatting in the from string method instead of the to string method odd as it seems works:
   public static _date dateFromString(string rh)
   {

       try
       {
           _date d = new _date();
           string[] sp = rh.Split('-');

           int month = int.Parse(sp[0]);
           int day = int.Parse(sp[1]);
           int year = int.Parse(sp[2]);

           if (month < 10)
               d.Month = '0' + month.ToString();
           else d.Month = month.ToString();
           if (day < 10)
               d.Day = '0' + day.ToString();
           else
               d.Day = day.ToString();
           if (year < 10)
               d.Year = '0' + year.ToString();
           else
               d.Year = year.ToString();
         
           return d;
       }
       catch (Exception ex)
       {

           return new _date();


       }
   }

That's because int.Parse() knows how to deal with any leading zeros.

That code can probably be simplified to something like:
C#:
public static _date dateFromString(string rh)
{
        _date d = new _date();
        DateTime date;
        var culture = CultureInfo.InvariantCulture;
        var style = DateTimeStyles.AllowWhiteSpaces;
        bool success = DateTime.TryParseExact(rh, "M-d-yyyy", culture, style, out date) ||
                       DateTime.TryParseExact(rh, "M-d-y", culture, style, out date);
        
        if (success)
        {
            d.Month = $"{date:MM}";   
            d.Day = $"{date:dd}";   
            d.Year = $"{date:yy}";   
        }
        return d;
}
 
A more interesting "why" question would be "why not use the built in DateTime or Date types?"
 
Back
Top Bottom