我有一个用户控件,它有一个面板,我想在运行时在其中进行一些绘制操作。我想知道当我将用户控件面板的绘制事件放置到表单时应该如何处理它。因为绘画事件无法正常工作,并且当我将其保留在表单中时它会破坏控件。
这是我在用户控件中的面板的绘制事件
private void itemPnl_Paint(object sender, PaintEventArgs e)
{
drawString(e);
this.Invalidate();
}
private void drawString(PaintEventArgs e)
{
System.Drawing.Drawing2D.LinearGradientBrush myBrush = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, Color.Red, Color.Yellow, System.Drawing.Drawing2D.LinearGradientMode.Horizontal);
cBasketItemHelper objHelper = new cBasketItemHelper() { CanvasWidth = this.itemPnl.Width, CanvasHeight = this.itemPnl.Height, X = 3, Y = 3 };
objHelper.myBrush = myBrush;
objHelper.currOrder = Program.currOrder;
objHelper.g = e.Graphics;
objHelper.DrawBasketItems();
objHelper.g.Dispose();
e.Dispose();
}
但是,当我编写此绘制事件并在完成后将控件保留在表单中时,它会取消类型并说出一些错误,即drawBasketItems
对象引用未设置为对象实例。
这是容纳一切的班级。
public class cBasketItemHelper : uiITEM
{
object tag;
public cOrder currOrder { get; set; }
public List<btnObject> lstAcceptedCustomizatio { get; set; }
public List<btnObject> lstDeniedCustomization { get; set; }
public float qtyWidth { get; set; }
public float itemWidth { get; set; }
public float priceWidth { get; set; }
private Label _lbl;
public List<Label> lstLable { get; set; }
public override RectangleF itemRectangle
{
get
{
return new RectangleF(this.X, this.Y, this.Width, this.Height);
}
set { }
}
public cBasketItemHelper()
{
this.format = new StringFormat();
this.Font = new Font("Times New Roman", 10);
format.Alignment = StringAlignment.Center;
}
public List<Label> drawLabel()
{
lstLable = new List<Label>();
foreach (cOrderItem item in currOrder.OrderItems)
{
_lbl = new Label();
_lbl.Width = 200;// (int)CanvasWidth;
_lbl.BackColor = Color.AliceBlue;
_lbl.Height = 20;
_lbl.Dock = DockStyle.Top;
_lbl.Tag = item;
_lbl.Paint += new System.Windows.Forms.PaintEventHandler(this.label_Paint);
lstLable.Add(_lbl);
}
return lstLable;
}
private void label_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Label lbl = (Label)sender;
Graphics g = e.Graphics;
cOrderItem orderItem = (cOrderItem)lbl.Tag;
g.DrawString(orderItem.ProductInfo.ProductDesc, new Font("Arial", 10), Brushes.Red, new Point(1, 1));
}
public void DrawBasketItems()
{
this.qtyWidth = (this.CanvasWidth) * 25 / 100;
this.priceWidth = (this.CanvasWidth) * 25 / 100;
this.itemWidth = this.CanvasWidth * 50 / 100;
foreach (cOrderItem item in currOrder.OrderItems)
{
this.lstAcceptedCustomizatio = item.lstAcceptedCustomization;
this.lstDeniedCustomization = item.lstDeniedCustomization;
string basketItemDescription = item.AppropriateProductName + "\n" + "+++ ";
string toMeasure = "+++ ";//basketItemDescription;
foreach (btnObject custItem in this.lstAcceptedCustomizatio)
{
System.Drawing.SizeF newString = g.MeasureString(custItem.BtnName + ", ", this.Font); //get the size of the text property
System.Drawing.SizeF drawnString = g.MeasureString(toMeasure, this.Font);
if (newString.Width > this.itemWidth - drawnString.Width)
{
basketItemDescription = basketItemDescription + "\n" + custItem.BtnName + ", "; //+ "\n";
toMeasure = string.Empty;
toMeasure = custItem.BtnName + ", ";// string.Empty; //get the size of the text property
}
else
{
basketItemDescription = basketItemDescription + custItem.BtnName + ", ";
toMeasure = toMeasure + custItem.BtnName + ", ";
}
}
string[] removeString = { ", " };
foreach (string removeItem in removeString)
{
if (basketItemDescription.EndsWith(removeItem))
{
basketItemDescription = basketItemDescription.Substring(0, basketItemDescription.LastIndexOf(removeItem));
break;//only allow one match at most
}
}
basketItemDescription = basketItemDescription + "\n" + "--- ";
toMeasure = "--- ";
foreach (btnObject custItem in this.lstDeniedCustomization)
{
System.Drawing.SizeF newString = g.MeasureString(custItem.BtnName + ", ", this.Font); //get the size of the text property
System.Drawing.SizeF drawnString = g.MeasureString(toMeasure, this.Font);
if (newString.Width > this.itemWidth - drawnString.Width)
{
basketItemDescription = basketItemDescription + "\n" + custItem.BtnName + ", "; //+ "\n";
toMeasure = string.Empty;
toMeasure = custItem.BtnName + ", ";// string.Empty; //get the size of the text property
}
else
{
basketItemDescription = basketItemDescription + custItem.BtnName + ", ";
toMeasure = toMeasure + custItem.BtnName + ", ";
}
}
foreach (string removeItem in removeString)
{
if (basketItemDescription.EndsWith(removeItem))
{
basketItemDescription = basketItemDescription.Substring(0, basketItemDescription.LastIndexOf(removeItem));
break;//only allow one match at most
}
}
System.Drawing.SizeF mySize = g.MeasureString(basketItemDescription, this.Font); //get the size of the text property
float stringHeight = mySize.Height;
this.Height = mySize.Height;
/*Draw Item*/
this.Width = qtyWidth;
g.DrawString(item.Quantity.ToString(), Font, myBrush, this.itemRectangle, format);
g.DrawRectangle(Pens.Black, Rectangle.Round(itemRectangle));
this.X = X + Width;
this.Width = itemWidth;
/*Draw Item Descrption*/
format.Alignment = StringAlignment.Near;
g.DrawString(basketItemDescription, Font, myBrush, this.itemRectangle, format);
g.DrawRectangle(Pens.Black, Rectangle.Round(itemRectangle));
this.X = X + Width;
this.Width = priceWidth;
/*Draw item Total*/
format.Alignment = StringAlignment.Center;
g.DrawString(item.OrderItemPrice.ToString(), Font, myBrush, this.itemRectangle, format);
g.DrawRectangle(Pens.Black, Rectangle.Round(itemRectangle));
this.X = 3;
this.Y = this.Y + this.Height + 2;
this.Width = 0;
this.Height = 0;
}
}
public float GetTheActualHeight()
{
float actualHeight = 0;
foreach (cOrderItem item in currOrder.OrderItems)
{
this.lstAcceptedCustomizatio = item.lstAcceptedCustomization;
this.lstDeniedCustomization = item.lstDeniedCustomization;
string basketItemDescription = item.AppropriateProductName + "\n" + "+++ ";
string toMeasure = "+++ ";//basketItemDescription;
foreach (btnObject custItem in this.lstAcceptedCustomizatio)
{
System.Drawing.SizeF newString = g.MeasureString(custItem.BtnName + ", ", this.Font); //get the size of the text property
System.Drawing.SizeF drawnString = g.MeasureString(toMeasure, this.Font);
if (newString.Width > this.itemWidth - drawnString.Width)
{
basketItemDescription = basketItemDescription + "\n" + custItem.BtnName + ", "; //+ "\n";
toMeasure = string.Empty;
toMeasure = custItem.BtnName + ", ";// string.Empty; //get the size of the text property
}
else
{
basketItemDescription = basketItemDescription + custItem.BtnName + ", ";
toMeasure = toMeasure + custItem.BtnName + ", ";
}
}
string[] removeString = { ", " };
foreach (string removeItem in removeString)
{
if (basketItemDescription.EndsWith(removeItem))
{
basketItemDescription = basketItemDescription.Substring(0, basketItemDescription.LastIndexOf(removeItem));
break;//only allow one match at most
}
}
basketItemDescription = basketItemDescription + "\n" + "--- ";
toMeasure = "--- ";
foreach (btnObject custItem in this.lstDeniedCustomization)
{
System.Drawing.SizeF newString = g.MeasureString(custItem.BtnName + ", ", this.Font); //get the size of the text property
System.Drawing.SizeF drawnString = g.MeasureString(toMeasure, this.Font);
if (newString.Width > this.itemWidth - drawnString.Width)
{
basketItemDescription = basketItemDescription + "\n" + custItem.BtnName + ", "; //+ "\n";
toMeasure = string.Empty;
toMeasure = custItem.BtnName + ", ";// string.Empty; //get the size of the text property
}
else
{
basketItemDescription = basketItemDescription + custItem.BtnName + ", ";
toMeasure = toMeasure + custItem.BtnName + ", ";
}
}
foreach (string removeItem in removeString)
{
if (basketItemDescription.EndsWith(removeItem))
{
basketItemDescription = basketItemDescription.Substring(0, basketItemDescription.LastIndexOf(removeItem));
break;//only allow one match at most
}
}
System.Drawing.SizeF mySize = g.MeasureString(basketItemDescription, this.Font); //get the size of the text property
actualHeight = actualHeight + mySize.Height;
}
return actualHeight;
}
}