vibheesh mp

vibheesh mp

  • 1.7k
  • 64
  • 5.5k

windows form in c# dynamically create multiple buttons

Feb 16 2015 5:20 AM
Below  i m giving start time ,endtime,timepervisit 
interval = TimeSpan.FromMinutes(int.Parse(Convert.ToString(dtColldata.Rows[0]["timepervisit"])));
DateTime opentime = DateTime.Parse(Convert.ToString(dtColldata.Rows[0]["Starttime"]));
DateTime closetime = DateTime.Parse(Convert.ToString(dtColldata.Rows[0]["Endtime"]));
 example opentime=10.00
timepervisit =10 
end time=1.00 
my purpose is  dynamically create button in grid or listview and each button to show time only the format like 10:10,10:20,because timepervisit is 10. 
button creating based on how many inter
how many button is want to create based on opentime and timepervisit, like first button with time dispaly in button text 10.00,next button time dispaly 10.10,next 10.20 like that
its urgent can any one help me by coding in c# 
The below code i used
i need the below buttons in multiple row(means each row contain 8 buttns),now all button dispaly in single row 

private void CreateButtons()
       {


           listView1.Controls.Clear();
           int x = 0, y = 0;
           interval = TimeSpan.FromMinutes(int.Parse(Convert.ToString(dtColldata.Rows[0]["timepervisit"])));
           DateTime opentime = DateTime.Parse(Convert.ToString(dtColldata.Rows[0]["Starttime"]));
           DateTime closetime = DateTime.Parse(Convert.ToString(dtColldata.Rows[0]["Endtime"]));
           DateTime t1, t2;
           t1 = DateTime.Parse(opentime.ToString("HH:mm"));
           t2 = DateTime.Parse(closetime.ToString("HH:mm"));
           for (DateTime current = t1; current <= t2; current += interval)
           {        
                       Button b = new Button();
                       b.Location = new System.Drawing.Point(x, y);
                       b.Size = new System.Drawing.Size(125, 125);
                       b.BackColor = Color.Green;
                       b.Font = new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                       b.Text = current.ToString("HH:mm");
                       listView1.Controls.Add(b);
                       x += 130;
                      //listView1.View = View.List;
                       b.Click += new EventHandler(this.DynamicButton_Click);
           }
       }




Answers (1)