如何将一个TextBox与另一个TextBox关联? [英] How to associate one TextBox with other TextBox?

查看:76
本文介绍了如何将一个TextBox与另一个TextBox关联?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿盖兹,
帮帮我,我是C#的新手.
我正在使用一种工具进行一些计算.假设我有4个文本框,
textBox1包含一些文本,而textBox2将更新textBox1的SUM,类似,textBox3包含一些文本,而textBox4将更新textBox3的SUM,依此类推....

有什么办法可以将一个文本框与另一个文本框关联.现在我在textBox1和textBox3上提供了一个常见的TextChanged事件.

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???
            }
        }

推荐答案

您应该记住,您的textBox_TextChanged是实例,而不是静态函数.也就是说,除了您使用的两个参数外,它还隐藏了"this"参数.在什么类中声明此方法?使用此实例可以将引用传递给另一个控件.例如,想象这是一种形式(很有可能),并且您的UI为System.Windows.Forms.我也不想使用命名处理程序,它看起来很丑.所以,让我们看看:

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
       };
   }
}



文本框的格式不同,差别很小:您需要以某种方式将对AnotherTextBox的引用传递给另一种格式.这被简化为关于表单协作的流行问题.最健壮的解决方案是在Form类中实现适当的接口,并传递接口引用而不是对Form的整个实例"的引用.请查看我过去的解决方案以获取更多详细信息:如何以两种形式在列表框之间复制所有项目 [ http://en.wikipedia.org/wiki/Accidental_complexity [ http://en.wikipedia.org/wiki/Loose_coupling [



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


hv不是一个字.您不在通话中.真烦人.

您可以从id算出您拥有哪个文本框,然后在适当的情况下相应地更改其他文本框.或者如果处理方式不同,也可以编写不同的事件.
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.


当控件失去焦点或将数据输入到控件中时,可以使用文本框上的"Leave"或"TextChanged"事件来使文本变色.它.执行完此操作后,您可以重新调整其他每个文本框的值.我只会在主控件上使用偶数.
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.


这篇关于如何将一个TextBox与另一个TextBox关联?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆