我对C#还是有点绿,所以如果我以错误的方式提问,请原谅我。
我正在编写一个Windows窗体程序。我有一个带有Tab控件的表单。其中一个选项卡上的Tab控件内部是FlowlayoutPanel控件 我有一个有各种属性的课程。此类表示联系人卡片上的数据,即名称地址,电话等,并且设计为看起来像Panel控件。这是一些代码:
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面板中。
答案 0 :(得分:2)
您不能将其他任何不是控件的内容添加到容器控件中,如FlowLayoutPanel。您应该添加面板本身而不是卡对象。这样做:
Panel pnl = card.CreateCard();
pnl.Tag = card; //optional
uxContactDetsFlp.Controls.Add(pnl);
此外,如果您以后必须检索卡片对象,则可以Tag
使用卡片对象的面板。
答案 1 :(得分:1)
这不起作用,因为您没有从Control继承您的类。您只能添加从Control继承的类来控制集合,例如Panel.Controls等。
public class clsContactCard : Control
这时你需要覆盖OnPaint事件处理程序以将内容呈现给自定义面板。
如果要创建要在Windows上使用的完全自定义控件 表单,您应该从Control类继承。
您还可以继承Panel控件并覆盖其OnPaint,以使生活更轻松。
更新 - 以下是一些资源:
http://msdn.microsoft.com/en-us/library/5h0k2e6x.aspx
http://msdn.microsoft.com/en-us/library/b818z6z6%28v=vs.71%29.aspx
http://bytes.com/../c-sharp/...creating-custom-controls-movable-resizable-container-control
http://visualbasic.about.com/od/usingvbnet/a/firstnetctrl.htm
答案 2 :(得分:0)
感谢大家提供建议。大多数都超出了我的头脑,但我确实仔细看了看他们。最后,我遵循了nawfal的建议。我重读了我的代码,发现我没有将CreateCard
方法生成的面板对象分配给任何东西。我想使用card对象创建的Panel对象来填充flowlayoutpanel而不是card对象本身(愚蠢的我)。因此,我真正需要做的唯一更改是PopulateContactDetails
方法末尾的这个小代码段:
//Create a new panel ... Panel cd = card.CreateCard(); //... and show on screen uxContactDetsFlp.Controls.Add(cd);