aronmatthew
Well-known member
- Joined
- Aug 5, 2019
- Messages
- 65
- Programming Experience
- Beginner
Using draw string method with scroll is only working with an explicit string contained in quotes
DrawString(..., "Hello, World", ...);
string s = "Hello, World";
DrawString(..., s, ...);
So you mean to say that this works:but this doesn't work?C#:DrawString(..., "Hello, World", ...);
I find that kind of hard to believe. Can you share the minimal code that reproduces the problem?C#:string s = "Hello, World"; DrawString(..., s, ...);
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;
}
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;
}
_Edate.dateToString(act.Date)
which is giving you bad values.string displayDate = _Edate.dateToString(act.Date);
System.Diagnostics.Debug.WriteLine(displayDate);
e.Graphics.DrawString(displayDate, drawFont, drawBrush, xPosDate, yPos);
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?
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]
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();
}
}
_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.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;
}
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}";
}
That method returns a good string
_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(); } }
int.Parse()
knows how to deal with any leading zeros.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;
}
DateTime
or Date
types?"