Hi,

I am creating a program on behalf of my company and am stuck in one particular matter. I am fairly new to C#.

At the moment I am creating a textbox, label and a button at the push of a diffrent button with the following code.

TextBox textBox6 = new TextBox();
                textBox6.Location = new Point(260, 120);
                textBox6.Size = new Size(42, 20);
                textBox6.TextChanged += new System.EventHandler(textBox6_TextChanged);
                this.Controls.Add(textBox6);

                Label label11 = new Label();
                label11.Location = new Point(990, 120);
                label11.Size = new Size(0, 13);
                this.Controls.Add(label11);

                Button button2 = new Button();
                button2.Location = new Point(860, 120);
                button2.Size = new Size(40, 20);
                button2.Text = "Add";
                button2.Click += new System.EventHandler(button2_Click);
                this.Controls.Add(button2);

This all works fine however I need to be able to preform calculations using the textbox and label at the push of the recently created button for which I am using the following code. (I will change the Variable names to Varaible 1..2...3 to avoid confusion)

Variable1 = Convert.ToDecimal(Variable2 * Variable3);
                   
                Control[] ctrls = Controls.Find("textBox6", false);
                for (int i = 0; i < ctrls.Length; i++)
                {
                    TextBox textBox6 = (TextBox)ctrls[0];
                    Variable4 = Variable5 * Convert.ToDecimal(textBox6.Text);
                }
                    
            Variable6 = Convert.ToDecimal(Variable1 * Variable4);
                
            
             ctrls = Controls.Find("label11", false);
                for (int i = 0; i < ctrls.Length; i++)
                {
                    Label label11 = (Label)ctrls[0];
                    label11.Text = Convert.ToString(Variable6 + Variable7);
                    windowTotal1.Window2 = Convert.ToDecimal(label11.Text);
                }

This should of given me a finished decimal in Label11, I think the problem is having to find the controls generated. Any help is more than welcome and thanked in advance.

Just incase here is my original without changed variables.

private void button2_Click(object sender, EventArgs e)
        {
            Control[] ctrls = Controls.Find("textBox6", false);
            for (int i = 0; i < ctrls.Length; i++)
            {
                TextBox textBox6 = (TextBox)ctrls[0];
                Area2 = Convert.ToDecimal(Height2 * Width2);
                glassCost2 = glassType2 * Convert.ToDecimal(textBox6.Text);
                glassTotal2 = Convert.ToDecimal(Area2 * glassCost2);
            }
                    
            ctrls = Controls.Find("label11", false);
            for (int i = 0; i < ctrls.Length; i++)
            {
                Label label11 = (Label)ctrls[0];
                label11.Text = Convert.ToString(glassTotal2 + Labour2);
                windowTotal1.Window2 = Convert.ToDecimal(label11.Text);
            }

Use TryParse or TryParseExact methods of value types.

Can you upload a sample project? Something doesn't sound right about what you are doing here....

What the at runtime created controls? TextBoxes or Buttons ?
If TextBoxes
You can add reference to recent created textbox name and perform the calculation
I.e: We created textbox its name textbox15

string recentTextBoxName = GetRecentCreatedTextBox();
string GetRecentCreatedTextBox()
{
///creating textbox at runtime
///return its name
}
void PerformCalcualtion(string textBoxName)
{
foreach(Control c in Controls)
if(c is TextBox)
if(c.Name == textBoxName)
//perform calculation on c
}

If buttons
when you create button assign a handler to it

///initialize controls properties
button1.Click+=new EventHandler(button_Click);
button2.Click+=new EventHandler(button_Click);
button3.Click+=new EventHandler(button_Click);

void button_Click(object sender, EventArgs)
{
///some code
}

Hello.
You seem a bit enmeshed with variable names and Cotrol names.
E.g. let's take a look at TextBox creation:

TextBox textBox6 = new TextBox();
                textBox6.Location = new Point(260, 120);
                textBox6.Size = new Size(42, 20);
                this.Controls.Add(textBox6);

you have a variable named textBox6 , which keeps your newly created TextBox. The point is that you don't specify the TextBox's name and trying to find it using your variable name (which have neither part nor lot in TextBox name).

So, if you want to get this to work:

Controls.Find("textBox6", false);

then you should set the name of TextBox:

textBox6.Name = "textBox6";

For accessing dynamically created controls - you can look here:
C# Cannot Read Dynamic Textbox

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.