0

I'm having an irritating problem with textbox. I enter data in one textbox and with the click of the button i need to display result in another textbox. I really dont know why my method wouldnt work. I dont get any errors, but it wouldnt display the result. Thanks! All of the textboxes are dynamically created during the run time. here is the code:

     private void button2_Click(object sender, EventArgs e)
    {
        TextBox tbox8 = new TextBox();
        tbox8.Name = "textBox8";
        tbox8.Location = new System.Drawing.Point(54 + (0), 55);
        tbox8.Size = new System.Drawing.Size(53, 20);
        this.Controls.Add(tbox8);
        tbox8.BackColor = System.Drawing.SystemColors.InactiveCaption;
        tbox8.TextChanged += new EventHandler(tbox8_TextChanged);

        TextBox tbox9 = new TextBox();
        tbox9.Name = "textBox9";
        tbox9.Location = new System.Drawing.Point(54 + (60), 55);
        tbox9.Size = new System.Drawing.Size(53, 20);
        this.Controls.Add(tbox9);
        tbox9.BackColor = System.Drawing.SystemColors.InactiveCaption;
        tbox9.TextChanged += new EventHandler(tbox9_TextChanged);

     }//button_click

    //input data into texbox8
    TextBox tbox;
    double var1;
    private void tbox8_TextChanged(object sender, EventArgs e)
    {
        tbox = sender as TextBox;
        var1 = Convert.ToDouble(tbox.Text);
    }

    //display the result in textbox9
    TextBox tbox2;
    private void tbox9_TextChanged(object sender, EventArgs e)
    {
        tbox2 = sender as TextBox;
    }

    //perform calculation and on button click display data on referenced textbox
    private void button3_Click(object sender, EventArgs e)
    {
        double result2 = var1 * 2;
        if( tbox2 != null)
        {
            tbox2.Text = result2.ToString(); 
        }      
    }
4
  • 5
    Remove that all. At button3_Click, perform tbox14.Text = Convert.ToDouble(tbox8) * 2; Commented Aug 22, 2012 at 17:11
  • sorry, i forgot to write that these texboxes are dynamically created during run time, so i woudlnt able to access them as you pointed out Commented Aug 22, 2012 at 17:22
  • How do you know which textbox will receive the result? Is this one static, at least? Commented Aug 22, 2012 at 17:27
  • @Andre Calil, not sure what you mean, but here is code for generating textboxes on the button click, thanks Commented Aug 22, 2012 at 17:31

3 Answers 3

3

Here is your code with the unnecessary bits commented out, and some new lines added:

TextBox tbox8 = new TextBox(); //make it a member variable
TextBox tbox9 = new TextBox(); //same for this one

private void button2_Click(object sender, EventArgs e)
{
    tbox8.Name = "textBox8";
    tbox8.Location = new System.Drawing.Point(54 + (0), 55);
    tbox8.Size = new System.Drawing.Size(53, 20);
    this.Controls.Add(tbox8);
    tbox8.BackColor = System.Drawing.SystemColors.InactiveCaption;
    tbox8.TextChanged += new EventHandler(tbox8_TextChanged);

    tbox9.Name = "textBox9";
    tbox9.Location = new System.Drawing.Point(54 + (60), 55);
    tbox9.Size = new System.Drawing.Size(53, 20);
    this.Controls.Add(tbox9);
    tbox9.BackColor = System.Drawing.SystemColors.InactiveCaption;
    tbox9.TextChanged += new EventHandler(tbox9_TextChanged);

 }//button_click

//input data into texbox8
//TextBox tbox;
double var1;
private void tbox8_TextChanged(object sender, EventArgs e)
{
    //tbox = sender as TextBox;
    var1 = Convert.ToDouble(tbox8.Text);//tbox8
}

//display the result in textbox9
//TextBox tbox2;//tbox2 is actually tbox9 anyway
private void tbox9_TextChanged(object sender, EventArgs e)
{
//    tbox2 = sender as TextBox;//so we don't need a tbox2
    //do something
}

//perform calculation and on button click display data on referenced textbox
private void button3_Click(object sender, EventArgs e)
{
    double result2 = var1 * 2;
    //if( null != tbox9 ) 
    //{
        tbox9.Text = result2.ToString(); //changed to tbox9
    //}      
}

However, this still has problems. What happens when the user clicks button2 more than once?

Sign up to request clarification or add additional context in comments.

6 Comments

I have assumed here that the textboxes on your form haven't had their names changed since the event handlers were generated.
I forgot to mention, all my textboxes are dynamically created. So, this woudlnt work
@AliFolks, why won't this work? I mean even if the text boxes were created dynamically the operations against them would net the same result.
var1 = Convert.ToDouble(tbox8.Text); here, i get a error saying tbox8 doesnt exist in the current context
@AliFolks: post the code that generates the textboxes if you like, see what we can do to work it into the answer.
|
0

Assuming tbox8 (try using meaningful names BTW) is the one that the text is supposed to be entered and tbox14 is where you expect the output to go, why do you only set the tbox2 variable in the TextChanged event of tbox14? You code probably isn't working because tbox2 is always null, because you never execute the TextChanged event for tbox14. And why are you using the variable tbox2 instead of just using tbox14 anyway?

Also, parsing the text in the TextChanged event of tbox8 is probably the wrong place to do it. The TextChanged event will fire on every keystroke. Why not just do it once in the button3 Click handler? Or, do it when it loses focus.

Try this (note: not that this isn't still crappy code). First, get rid of all the TextChanged events, then:

private void button3_Click(object sender, EventArgs e)
{
    var1 = Convert.ToDouble(tbox8.Text);
    double result2 = var1 * 2;
    tbox14.Text = result2.ToString();    
}

1 Comment

it's dynamic creation of texboxes, so i do need textchanges
0

Something like this should work assuming result * 2 doesn't cause an arithmetic overflow for a double. If the value entered into tbox8 can't be parsed into a double, the result displayed in tbox9 after clicking button3 will be 0.

Since I'm not using any of the events of the TextBoxes, I don't need to create handlers for them. I'm also disabling button2 after it is clicked so that it can't be clicked again.

If you need to click button2 again, you'll need to enable it from another button event handler or something so that you have the chance to remove tbox8 and tbox9 from the this.Controls collection before enabling it (button2) to prevent the application from attempting to create additional copies of them.

private void button2_Click(object sender, EventArgs e) {
    button2.Enabled = false;
    TextBox tbox8 = new TextBox();
    tbox8.Name = "textBox8";
    tbox8.Location = new System.Drawing.Point(54 + (0), 55);
    tbox8.Size = new System.Drawing.Size(53, 20);
    this.Controls.Add(tbox8);
    tbox8.BackColor = System.Drawing.SystemColors.InactiveCaption;

    TextBox tbox9 = new TextBox();
    tbox9.Name = "textBox9";
    tbox9.Location = new System.Drawing.Point(54 + (60), 55);
    tbox9.Size = new System.Drawing.Size(53, 20);
    this.Controls.Add(tbox9);
    tbox9.BackColor = System.Drawing.SystemColors.InactiveCaption; }

private void button3_Click(object sender, EventArgs e) {
    double result = 0;
    double.TryParse(tbox8.Text, out result);
    tbox9.Text = (double)(result * 2).ToString(); }

Comments

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.