0

This is a follow up question for my previous question.

However in this question, I created such a Control so whenever I click on the button, two TextBoxes will appear (Indefinite count/click) in a row. Whenever I input again a value, they will do the addition and put the result in the third TextBox of their row.

   private void buttonNewRow_Click(object sender, EventArgs e)
        {
            int count = 1;
            DateTimePicker date = new DateTimePicker();
            count = panel1.Controls.OfType<DateTimePicker>().ToList().Count;
            date.Location = new Point(0, 15 * count);
            date.Size = new Size(91, 20);
            panel1.Controls.Add(date);

           //Textbox 1
            TextBox textboxTranspo = new TextBox();
            textboxTranspo.Location = new Point(576, 45 * count);
            textboxTranspo.Size = new Size(81, 20);
            panel1.Controls.Add(textboxTranspo);

            //Textbox 2
            TextBox textboxDaily = new TextBox();
            textboxDaily.Location = new Point(663, 45 * count);
            textboxDaily.Size = new Size(81, 20);
            panel1.Controls.Add(textboxDaily);

            //Textbox 1 + Textbox 2 result
            TextBox textboxTotal = new TextBox();
            textboxTotal.Location = new Point(772, 45 * count);
            textboxTotal.Size = new Size(100, 20);
            panel1.Controls.Add(textboxTotal);
}

How can I accomplish this?

4
  • I think, You should try grid control. Commented Nov 18, 2016 at 8:56
  • Set the control's Name property then use <container>.Controls("name") to access the control. Commented Nov 18, 2016 at 15:22
  • The normal solution of this approach is to define a UserControl. You can add your logic to the user control and you just add it to the your panel programmatically. Commented Nov 19, 2016 at 12:07
  • FlowLayoutPanel or DataGridView controls instead of Panel can make it a bit easier. Commented Nov 21, 2016 at 5:21

2 Answers 2

0

You can add event handlers like this:

private void buttonNewRow_Click(object sender, EventArgs e)
{
    DateTimePicker date = new DateTimePicker();
    int count = panel1.Controls.OfType<DateTimePicker>().ToList().Count;
    date.Location = new Point(0, 15 * count);
    date.Size = new Size(91, 20);
    panel1.Controls.Add(date);

    //Textbox 1
    TextBox textboxTranspo = new TextBox();
    textboxTranspo.Location = new Point(576, 45 * count);
    textboxTranspo.Size = new Size(81, 20);
    panel1.Controls.Add(textboxTranspo);

    //Textbox 2
    TextBox textboxDaily = new TextBox();
    textboxDaily.Location = new Point(663, 45 * count);
    textboxDaily.Size = new Size(81, 20);
    panel1.Controls.Add(textboxDaily);

    //Textbox 1 + Textbox 2 result
    TextBox textboxTotal = new TextBox();
    textboxTotal.Location = new Point(772, 45 * count);
    textboxTotal.Size = new Size(100, 20);
    panel1.Controls.Add(textboxTotal);

    EventHandler textChanged = (o, e) => {
        int intTranspo, intDaily;
        if (int.TryParse(textboxTranspo.Text, out intTranspo)
         && int.TryParse(textboxDaily.Text, out intDaily))
            textboxTotal.Text = (intTranspo + intDaily).ToString();
    };
    textboxTranspo.TextChanged += textChanged;
    textboxDaily.TextChanged += textChanged;
}
Sign up to request clarification or add additional context in comments.

2 Comments

How can I get all the textboxTotal and add them all up to a different textbox?
good way is to add them to a List<TextBox> when created, or a less reliable way something like panel1.Controls.OfType<TextBox>().Where(t => t.Left == 772).Select(t => int.Parse(t.Text)).Sum()
0

I will address my comment above as an answer.

First you have to define a UserControl and named it whatever you like but let us name it Uc1.

In the designer drag the datetimepicker and the 3 textboxes to the Uc1. Supposed we name the control dt1 for datetimepicker and txtTranspo for textbox1 and txtDaily for textbox2 and txtTotal for the last one. And since we need to compute the value of txtTranspo and txtDaily i want to add 1 button named btnCompute for computation logic for simplicity sake.

then inside the Uc1

class Uc1
{

    public Uc1() 
    {
        InitializeComponent();
        this.wireEvents();
    }


    // This will handle all controls events for well organized form sakes.
    private void wireEvents()
    {
        this.btnCompute.Click += OnBtnComputeEvent;
    }


    private void OnBtnComputeEvent(object sender, EventArgs e)
    {
        // i used int since i do not know exactly how to do this.
        int transpo = Int32.Parse(this.txtTranspo.Text);
        int daily = Int32.Parse(this.txtDaily.Text);
        double total = ((transpo + daily) / 100 * 100) // <--- this is just a random computation.

        this.txtTotal.Text = string.Format("{0:c}", total); // this will format the total to a currency.
    }

}

Then build the solution. or click the combination of Ctrl + Shift + B. After this the Uc1 will appear to your toolbox as a new control.

Then you can simply do this to your own logic.

private void buttonNewRow_Click(object sender, EventArgs e)
{
        Uc1 c = new Uc1();
        c.Dock = DockStyle.Top;
        c.BringToFront();

        this.panel.Controls.Add(c);
}

You are good to go.

8 Comments

There is one thing. 'int' does not contain a definition for 'parseInt' that is the error im receiving.
Hello, I manage to debug some error so far the string.format should be string.Format but there is one last error which is btnTotal. It said that it does not contain a definition for btnTotal
Oh, i just mistype it is the same txtTotal let me update
Wait a minute, how can I fetch the variable from the user control and add them up to the main form? So that I can get all the value of the txtTotal and add them up to a different textbox in the main form.
@Noobster what do you mean? you want to get ALL the txtTotal value of each usercontrol? or the SUM of ALL the txtTotal in each usercontrol?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.