Click here to Skip to main content
Sign Up to vote bad
good
See more: C#
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:26
Edited 15 Nov '12 - 8:32

Comments
joshrduncan2012 - 15 Nov '12 - 14:30
Listen for the textupdate event to update your textbox1...
Sergey Alexandrovich Kryukov - 15 Nov '12 - 14:42
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

4 solutions

Done by handling TextChanged Event and given switch case to update text of required textBox.....
  Permalink  
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.
  Permalink  
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:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].
 
—SA
  Permalink  
Comments
Espen Harlinn - 15 Nov '12 - 19:50
Good one :-D
Sergey Alexandrovich Kryukov - 15 Nov '12 - 22:05
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.
  Permalink  
Comments
Robin Purbia - 15 Nov '12 - 14:39
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:39
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
Your Filters
Interested
Ignored
     
0 Aarti Meswania 230
1 OriginalGriff 220
2 Mahesh Bailwal 220
3 Ron Beyer 215
4 Rohan Leuva 178
0 Sergey Alexandrovich Kryukov 8,553
1 OriginalGriff 6,899
2 CPallini 3,648
3 Rohan Leuva 2,963
4 Maciej Los 2,308


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 24 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid