我对 C# 还有些陌生,所以如果我以错误的方式提出问题,请原谅我。
我正在编写一个 Windows 窗体程序。我有一个带有 Tab 控件的表单。在其中一个选项卡上的 Tab 控件内部是一个 FlowlayoutPanel 控件,我有一个具有各种属性的类。此类表示联系人卡片上的数据,即姓名地址、电话等,并且被设计为看起来像一个面板控件。这是一些代码:
public class clsContactCard
{
#region Fields (8)
private Color _backColour;
private BorderStyle _borderStyle;
private List<string> _detailLines = new List<string>();
private Color _foreColour;
private Size _size;
private string _subTitle;
private string _title;
#endregion Fields
#region Constructors (1)
public clsContactCard()
{}
#endregion Constructors
#region Properties (8)
public Color BackColour
{
get { return _backColour; }
set { _backColour = value; }
}
public BorderStyle BorderStyle
{
get { return _borderStyle; }
set { _borderStyle = value; }
}
public List<string> DetailLines
{
get { return _detailLines; }
set { _detailLines = value; }
}
public Color ForeColour
{
get { return _foreColour; }
set { _foreColour = value; }
}
public Size Size
{
get { return _size; }
set { _size = value; }
}
public string SubTitle
{
get { return _subTitle; }
set { _subTitle = value; }
}
public string Title
{
get { return _title; }
set { _title = value; }
}
#endregion Properties
public Panel CreateCard()
{
// New Contact Card
Point labelLoc = new Point(18, 11); //Location for a label
Size labelSize = new Size(218, 16); //default label size
Panel pnl = new Panel(); //Instantiate a new Panel
pnl.BackColor = _backColour; //Set the new panel's properties
pnl.BorderStyle = _borderStyle;
pnl.Size = _size;
pnl.Visible = true;
//Title
Label l = new Label(); //Create new label object
l.Name = "uxTitle"; //Give it a name
l.Text = _title; //Assign it data from properties
l.Size = labelSize; //set its size & font
l.Font = new Font("Microsoft Sans Serif",10,FontStyle.Bold);
l.Location = labelLoc; //set its location
labelLoc.Y += labelSize.Height; //update next labels location
pnl.Controls.Add(l); //add label to panel controls
//Type
//Label l = new Label();
l.Name = "uxSubTitle";
l.Text = _subTitle;
l.Size = new Size(215, 15);
l.Font = new Font("Microsoft Sans Serif",7,FontStyle.Regular);
l.Location = new Point(21, 27);
labelLoc.Y += labelSize.Height + 5;
//Detail lines
int lineCount = 0;
bool firstPhone = true;
foreach (string line in _detailLines)
{
if (SAPSCommon.Instance.IsNumeric(line.Trim()) && firstPhone)
{
firstPhone = false;
labelLoc.Y += 5;
}
lineCount += 1;
//Label l = new Label();
l.Name = "uxLine" + lineCount;
l.Text = line;
l.Size = labelSize;
l.Font = new Font("Microsoft Sans Serif",8,FontStyle.Regular);
l.Location = labelLoc;
labelLoc.Y += labelSize.Height + 5;
}
return pnl;
}
}
这个想法是在类似于 MS Outlook 联系人列表的 FlowLayoutPanel 控件中显示卡片对象。我有填充对象属性的代码,但是当我尝试将卡片对象(面板)添加到 FlowlayoutPanel 控件时,编译器会抱怨以下类型:
错误 2 参数 1:无法从“SAPS.clsContactCard”转换为“System.Windows.Forms.Control”
代码如下:
foreach (clsContacts contact in _pensioner.Contacts)
{
clsContactCard card = new clsContactCard();
if (contact.OtherNames != "")
{
card.Title = string.Format("{0} {1} {2}", contact.Givname, contact.OtherNames,
contact.Surname);
}
else
{
card.Title = string.Format("{0} {1}", contact.Givname, contact.Surname);
}
card.SubTitle = contact.ContactTypeDescription;
card.DetailLines.Add(contact.Addr1);
string addr2 = contact.Addr2;
if (addr2.Length >= 0)
card.DetailLines.Add(addr2);
card.DetailLines.Add(string.Format("{0} {1} {2}", contact.Suburb, contact.State, contact.PCode).Trim());
string country = contact.Country;
if (country.Length >= 0)
card.DetailLines.Add(country);
foreach(clsPhoneNumbers phone in contact.PhoneNumbers)
{
card.DetailLines.Add(string.Format("{0} - {1}", phone.PhoneNumber, phone.PhoneType));
}
foreach(clsEmailAddresses email in contact.EmailAddresses)
{
card.DetailLines.Add(string.Format("{0} - {1}", email.EmailAddress, email.EmailType));
}
card.CreateCard();
uxContactDetsFlp.Controls.Add(card);
}
谁能阐明我做错了什么以及如何解决?我认为可以将面板控件添加到 flowlayout 面板中。