Click here to Skip to main content
13,146,739 members (80,218 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hey Guyz,
Help me out, I am a newbie to C#.
I am making one tool for some calculations. Suppose i hv 4 textboxes,
textBox1 contains some text and textBox2 will update SUM of textBox1, similarly textBox3 contains some text and textBox4 will update SUM of textBox3 and so on....

Is there any way i can associate one textbox with other. right now i am giving a common TextChanged event on textBox1 and textBox3.... like this..

TextBox TempTextBox;
private void textBox_TextChanged(object sender, EventArgs e)
            TempTextBox = (TextBox)sender; //TextBox in which text is updated
            if (TempTextBox.Text != "")
                // how can i update textBox2.text here???
                // Is there any way i can get textBox2 handle here???
                // or i can associate textBox1 with textBox2???
Posted 15-Nov-12 8:26am
Updated 15-Nov-12 8:32am
joshrduncan2012 15-Nov-12 14:30pm
Listen for the textupdate event to update your textbox1...
Sergey Alexandrovich Kryukov 15-Nov-12 14:42pm
Always tag UI library you are using. In other words, if you write "TextBox", write its full type name. There are different types named "TextBox".
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Done by handling TextChanged Event and given switch case to update text of required textBox.....
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You can use the "Leave" or "TextChanged" Events on the textbox to detext either when the control looses focus or you input data into it. After you do that you can recalulate the value for each of the other text boxes. I would only reccomend using the even on the main control.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You should remember that your textBox_TextChanged is an instance, not a static function. That said, in addition to the two parameter you use, it also has hidden "this" parameter. In what class this method is declared? Use this instance to pass a reference to another control. For example, imaging this is a form (very likely), and your UI is System.Windows.Forms. I also don't want to use a named handler, it looks ugly. So, let's see:

partial public class MyForm { // MyForm : System.Windows.Forms.Form is specified in some other part of "partial", typically generated from template
   TextBox OneTextBox = //...
   TextBox AnotherTextBox = //...
   public MyForm() {
       OneTextBox.TextChanged += (sender, eventArgs) => { //types of sender, eventArgs are inferred by a compiler from known event type
           TextBox thisTextBox = (TextBox)sender; // this cast cannot fail, this is how controls invoke events
           // at this time, thisTextBox == OneTextBox; but you could use OneTextBox directly, because this is actually passed with "this":
           thisTextBox = this.OneTextBox;
           // or just
           thisTextBox = OneTextBox; // because there is no ambiguity
           // and finally, drums...:
           TextBox anotherTextBox = this.AnotherTextBox; // another totally redundant declaration and "this.", just for explanation
           // use AnotherTextBox, which is this.AnotherTextBox

Of the text boxes are in different forms, the difference is minor: you need somehow pass a reference to AnotherTextBox to another form. This is reduced to the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:[^],[^].

Espen Harlinn 15-Nov-12 19:50pm
Good one :-D
Sergey Alexandrovich Kryukov 15-Nov-12 22:05pm
Thank you, Espen.
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

hv is not a word. you're not on a phone. It's annoying.

You can work out from the id which textbox you have, and then change other textboxes accordingly, if appropriate. Or just write different events if they are handled differently.
Robin Purbia 15-Nov-12 14:39pm
I have already tried both the solutions given by you, but I just want reduce code, and if possible just make it with a single function...
Christian Graus 15-Nov-12 14:39pm
Those are the only solutions.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 24 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100