Click here to Skip to main content
15,888,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get working hours of day
i m using one datetimepicker for taking entry time and one datetimepicker for taking exit time.
using these times i calculate total hours by formula Exit time-Entry time
i want following format

entryTime : timePicker ; exit time : Timepicker; hours of day:exit-entry +


when i click this + button at run time other row should appear as

entryTime : timePicker ; exit time : Timepicker; hours of day:exit-entry +
entryTime : timePicker ; exit time : Timepicker; hours of day:exit-entry +

so on
how to add this row that is timepickers dynamically
Posted

On button click event for c# , do the following

C#
private void button1_Click(object sender, EventArgs e)
{
    DateTimePicker dtp = new DateTimePicker();
    //add the new datetimepicker below the previous one
    dtp.Location = new Point(dateTimePicker1.Location.X, dateTimePicker1.Location.Y+25);
    Button btn = new Button(); // add a new button
    btn.Size = button1.Size; //set button size
    btn.Text = "+"; // set button text
    btn.Location = new Point(button1.Location.X, button1.Location.Y+25);
    this.Controls.Add(dtp);
    this.Controls.Add(btn);
}


You would be able to add more controls like this to your form if you want, Won't you?
Also, please if you really find this one helpful , then show some appreciation to those who are working on behalf of you ; like voting or accepting the answer. ;)
 
Share this answer
 
Comments
Member 8081020 3-Aug-11 6:51am    
Thanks for solution
But can u tell me how can i add same controls multiple times when i click + button
that is the above code generates datetimepicker and button only once
i want that when user clicks dynamically created button then these timepickers and buttons should be keep on generating on form
Praveen Kullu 3-Aug-11 9:59am    
call the same function with every "+" button click event. You don't need to create different event handlers for every button click. just call the same function.

I am currently working on the solution that you want but i have been unable to figure it out. I am a student myself. I hope you can wait or you may try repeating your question again in the forum.
Member 8081020 4-Aug-11 8:03am    
Ya thanks for help but I Found the solution. I repeatedly called same event.
I was making mistake that in event i was giving controls position with respect to first controls. I just took positions of all controls in variables and with each button click i Kept on increamenting y-coordinate of all controls positions
so all controls came on new line. initially they where overlapping so was unable to get controls on new line on every click.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900