2017-09-04 24 views
0

私は動的にパネルを追加しているという点で私はウィンドウフォームを持っています。そのパネルでは、ボタンといくつかのラベルを追加しています。ユーザーがボタンをクリックすると、ボタンからプログレスバーに変わります。動的にボタンの代わりにプログレスバーを追加

Present scenario

Required result

Panel p = new Panel(); 
int x=20; 
int y=20; 
p.Location = new System.Drawing.Point(x, y); 
p.Size = new Size(682, 80); 
p.BackColor = System.Drawing.Color.LightCyan; 
Label l1 = new Label(); 
l1.Text =" Hello "; 
l1.AutoSize = true; 
l1.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0))); 

    Label l2 = new Label(); 
    l2.Location = new System.Drawing.Point(20, 22); 
    l2.AutoSize = true; 
    l2.Text ="Description"; 

    Button b = new Button(); 
    b.Name = "UpdateButton"+i; 
    b.Text = "Update"; 
    b.Location = new System.Drawing.Point(551, 22); 
    b.Size = new Size(75, 23); 
    b.Click += new EventHandler(updateBtnClick); 
    p.Controls.Add(l4); 
    p.Controls.Add(l5); 
    p.Controls.Add(b); 

    ProgressBar pb = new ProgressBar(); 
    pb.Location = new System.Drawing.Point(551, 30); 
    pb.Size = new Size(125, 5); 
    p.Controls.Add(pb); 
    pb.Visible = false; 
    this.Controls.Add(p); 

    private void updateBtnClick(object sender, EventArgs e) 
    { 
     Button tempB = (Button)sender; 
     tempB.Visible = false; 
     //Need to add progress bar here. How to get object of progress bar? 
    } 
+0

そこに。 – Blessy

答えて

1

すべてが動的にする必要がある場合は、あなたが.Parentを取得するためにボタンを使用することができ、その.Controls、そのコレクションから最初のProgressBarを取得するために.OfType<ProgressBar>()を使用ここのように:

private void updateBtnClick(object sender, EventArgs e) 
{ 
    Button tempB = (Button)sender; 
    tempB.Visible = false; 

    ProgressBar pb = tempB.Parent.Controls.OfType<ProgressBar>().FirstOrDefault(); 
    if (pb != null) pb.Visible = true; 
} 
関連する問題