Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 9:26am
Edited 15-Nov-12 9:32am
v3
Comments
joshrduncan2012 at 15-Nov-12 14:30pm
   
Listen for the textupdate event to update your textbox1...
Sergey Alexandrovich Kryukov at 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
Rate this: bad
good
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:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].
 
—SA
  Permalink  
Comments
Espen Harlinn at 15-Nov-12 19:50pm
   
Good one :-D
Sergey Alexandrovich Kryukov at 15-Nov-12 22:05pm
   
Thank you, Espen.
--SA
Rate this: bad
good
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.
  Permalink  
Comments
Robin Purbia at 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 at 15-Nov-12 14:39pm
   
Those are the only solutions.
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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.....
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 380
1 OriginalGriff 230
2 DamithSL 200
3 Zoltán Zörgő 149
4 BillWoodruff 120
0 OriginalGriff 7,610
1 DamithSL 5,559
2 Sergey Alexandrovich Kryukov 5,324
3 Maciej Los 4,961
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 24 Nov 2012
Copyright © CodeProject, 1999-2014
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