Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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..

C#
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
Updated 15-Nov-12 8:32am
v3
Comments
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".
--SA

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:

C#
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:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 15-Nov-12 19:50pm    
Good one :-D
Sergey Alexandrovich Kryukov 15-Nov-12 22:05pm    
Thank you, Espen.
--SA
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.
 
Share this answer
 
Comments
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.
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.
 
Share this answer
 
Done by handling TextChanged Event and given switch case to update text of required textBox.....
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900