0
WindowsフォームPanel
と番号Label
を使用してユーザーコントロールを作成しようとしていますが、テキストは水平方向にPanel
に動的に追加されています。私は下のコードで試しており、Label
が無効になります。パネルとラベルを使用したユーザーコントロールの動的な追加
public partial class AllergyBar : Panel
{
public AllergyBar()
: base()
{
InitializeComponent();
}
int X = 0, Y=0;
int height, width;
public AllergyBar(List<String> lstAlerts)
: base()
{
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.Name = "panel2";
this.Size = new System.Drawing.Size(75, 23);
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
foreach (string alert in lstAlerts)
{
Label AllergyLabel = new Label();
AllergyLabel.Text = alert;
width = AllergyLabel.Size.Width;
Y = AllergyLabel.Location.Y;
AllergyLabel.Location = new System.Drawing.Point(X+width, Y);
AllergyLabel.Size = new System.Drawing.Size(75, 23);
AllergyLabel.AutoSize = true;
AllergyLabel.BorderStyle = BorderStyle.FixedSingle;
AllergyLabel.Dock = DockStyle.Fill;
this.Controls.Add(AllergyLabel);
}
InitializeComponent();
}
}
使用FlowLayoutPanel https://msdn.microsoft.com/en-us/library/system.windows.forms。 flowlayoutpanel(v = vs.110).aspx –