I’m still a bit green on C# so please forgive me if I’m asking the question in the wrong way.
I’m writing a windows Forms program. I have a form with a Tab control on it. Inside the Tab control on one of the tabs is a FlowlayoutPanel control
I have a class with various properties. This class represents the data on a contacts card, i.e. Name Address, Phone etc. and is designed to look like a Panel control. Here is some code:
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;
}
}
The idea is to show the card object in a FlowLayoutPanel control akin to MS Outlook contacts list. I have the code to populate the object properties, but when I try to add the card object (panel) to the FlowlayoutPanel control the compiler complains about the types:
Error 2 Argument 1: cannot convert from ‘SAPS.clsContactCard’ to ‘System.Windows.Forms.Control’
The code is a s follows:
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);
}
Can anyone shed a light on what I’m doing wrong and how to fix it? I thought the panel control could be added to the flowlayout panel.
You can not add anything else that is not a control to container control like FlowLayoutPanel. You should instead add the panel itself rather than the card object. Do it like this:
Additionally you can
Tagthe panel with the card object if you have to retrieve the card object later on.