Рет қаралды 4,920
csharp example to add, update and delete controls at runtime inside a flowlayoutpanel. A picturebox and labels are created to show a movie image, title and release date.
Files:
drive.google.com/drive/folder...
private void AddMovieToUI(Movie movie)
{
//Create panel
Panel panel;
panel = new Panel();
panel.Name = String.Format("PnlMovie{0}", movie.Id);
panel.BackColor = Color.White;
panel.Size = new Size(125, 205);
panel.Margin = new Padding(10);
panel.Tag = movie.Id;
//Create picture box
PictureBox picBox;
picBox = new PictureBox();
picBox.Name = String.Format("PbMovieImage{0}", movie.Id);
picBox.Size = new Size(100, 148);
picBox.Location = new Point(12, 10);
picBox.SizeMode = PictureBoxSizeMode.Zoom;
if (File.Exists(movie.ImagePath))
picBox.Image = Image.FromFile(movie.ImagePath);
picBox.Tag = movie.Id;
//Create title label
Label labelTitle;
labelTitle = new Label();
labelTitle.Name = String.Format("LblMovieTitle{0}", movie.Id);
labelTitle.Text = movie.Title;
labelTitle.Location = new Point(12, 165);
labelTitle.ForeColor = Color.Black;
labelTitle.Font = new Font(this.Font.FontFamily, 9.5f, FontStyle.Regular);
labelTitle.AutoSize = true;
labelTitle.Tag = movie.Id;
//Create year label
Label labelYear;
labelYear = new Label();
labelYear.Name = String.Format("LblMovieYear{0}", movie.Id);
labelYear.Text = movie.ReleaseDate.Year.ToString();
labelYear.Location = new Point(12, 185);
labelYear.ForeColor = Color.Gray;
labelYear.Font = new Font(this.Font.FontFamily, 9.5f, FontStyle.Regular);
labelYear.Tag = movie.Id;
//Set Context Menu
panel.ContextMenuStrip = contextMenuStrip1;
//Add controls to panel
panel.Controls.Add(picBox);
panel.Controls.Add(labelTitle);
panel.Controls.Add(labelYear);
//Add Event Handlers
panel.DoubleClick += new EventHandler(Edit_DoubleClick);
foreach (Control c in panel.Controls)
{
c.DoubleClick += new EventHandler(Edit_DoubleClick);
}
//Add panel to flowlayoutpanel
flowLayoutPanel1.Controls.Add(panel);
}
private void UpdateMovieInUI(Movie movie)
{
Control control;
PictureBox picBox;
string name;
//Find picturebox and update movie image
name = String.Format("PbMovieImage{0}", movie.Id);
control = this.Controls.Find(name, true).FirstOrDefault();
picBox = (PictureBox)control;
if (File.Exists(movie.ImagePath))
picBox.Image = Image.FromFile(movie.ImagePath);
else
picBox.Image = null;
//Find movie title label and update text
name = String.Format("LblMovieTitle{0}", movie.Id);
control = this.Controls.Find(name, true).FirstOrDefault();
control.Text = movie.Title;
//Find movie year label and update text
name = String.Format("LblMovieYear{0}", movie.Id);
control = this.Controls.Find(name, true).FirstOrDefault();
control.Text = movie.ReleaseDate.Year.ToString();
}
private void DeleteMovieFromUI(Movie movie)
{
Control panel;
string name;
//Find panel
name = String.Format("PnlMovie{0}", movie.Id);
panel = this.Controls.Find(name, true).FirstOrDefault();
//Remove event handlers
panel.DoubleClick -= new EventHandler(Edit_DoubleClick);
foreach (Control c in panel.Controls)
{
c.DoubleClick -= new EventHandler(Edit_DoubleClick);
}
//Remove panel
flowLayoutPanel1.Controls.Remove(panel);
panel.Dispose();
}