Hi, I try to develop Forms App which will count my working hours, taxes, expenses etc...
At moment Iam working on calendar. On the first form I have combobox which sends the name of month into the ReturnNumberOfDaysInCurrentMonth().

The Class returns the number of month for DateTime.DaysInMonth(int,int), which returns the quantity of days.

That's the problem. I need to create dynamically changeable set of textboxes according to quantity of days in current month. Something like:

int numbeOfDays;
TextBox tb=new TextBox();
for(int i=0; i<=numberOfDays; i++)
{
tb=(TextBox)i;
Form1.Controls.Add(tb);
}

Does anyone can advice me something? Thank you.

Dani AI

Generated

Thread summary and practical suggestions for : the goal is a month-driven set of inputs (one per day) that can be rebuilt whenever the selected month changes. The answers from (manual placement) and (naming controls) show workable starting points, but a more maintainable UI uses a single container and small reusable day widgets instead of juggling coordinates and raw TextBox instances.

Recommended pattern (no code here, just steps): host day controls inside a FlowLayoutPanel or TableLayoutPanel so the layout adapts to resizing and scrolling; encapsulate a day label plus a numeric input into a tiny UserControl (or use NumericUpDown) so validation and formatting live in one place; when the month changes, clear the container, create one day widget per date, store references in a Dictionary keyed by day number (or use each control�s Tag) and update the underlying model instead of iterating Controls later. Use SuspendLayout/ResumeLayout while rebuilding to avoid flicker, and unsubscribe event handlers before removing controls to prevent leaks.

Practical cautions and links: prefer numeric controls for hours to prevent parsing bugs; persist values in a simple data model (year-month-day => value) rather than living only in UI controls; consider TableLayoutPanel or FlowLayoutPanel for automatic layout (FlowLayoutPanel, TableLayoutPanel) and read about NumericUpDown for constrained numeric entry (NumericUpDown).

Recommended Answers

All 2 Replies

Start a new forms app and fill in the code below. Hope it helps you out.

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            int numberOfDays = 30;
            int Xpos = 10;
            int Ypos = 0;
            for (int i = 0; i <= numberOfDays; i++)
            {
                Ypos = 20 * (i % 6) + 3;
                if ((i % 6) == 0) Xpos += 50;
                TextBox tb = new TextBox();
                tb.Location = new Point(Xpos, Ypos);
                tb.Width = 50;
                //other properties for tb here
                this.Controls.Add(tb); //add to form
            }
        }
    }
}
commented: solved! +8

Uhm you can try this

for(int i = 0 ;i < numberOfDays; i++)
 {
    TextBox tb = new TextBox();
    tb.ID = "txt" + i;
    form1.Controls.Add(tb);
 }

regards.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.