私はまだ C# に少し慣れていないので、質問の仕方が間違っている場合はご容赦ください。
Windowsフォームプログラムを書いています。Tab コントロールを含むフォームがあります。タブの 1 つのタブ コントロール内には、さまざまなプロパティを持つクラスがある 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);
}
誰かが私が間違っていることとそれを修正する方法に光を当てることができますか? フローレイアウト パネルにパネル コントロールを追加できると思いました。