简体   繁体   English

我需要更改文本框的名称,而不是文本框中的文本。 然后使用该名称来命名一个单独的文本框

[英]I need to change the name of a TextBox, not the text in the textbox. Then use that name to name a seperate textbox

I have six textboxes.我有六个文本框。

|TB1|   |TB102|    |TB103|
|TB2|   |TB202|    |TB203|
  • I need to use TB1 as a full integer, say 100.我需要使用 TB1 作为一个完整的整数,比如 100。
  • TB102 needs to divide TB1 in half. TB102 需要将 TB1 一分为二。
  • TB103 needs to divide in quarter. TB103 需要按季度划分。

Here's my code:这是我的代码:

private void TB_Half_TextChanged(object sender, EventArgs e)
{
    TextBox tbFull = (TextBox)sender;   //determine which textbox is changed
    TextBox tbHalf = (TextBox)sender;   //save the name to add 02 to it later
                                        // tbHalf == tbFull + "02";?

    int ATTFull = Convert.ToInt32(tbFull.Text); //convert textbox to integer
    int ATTHalf = ATTFull / 2;               //divide by 2
    string STRback = Convert.ToString(ATTHalf); //convert integer to textbox
    //tb.Name = TBHalf + "02";                         //add 02 to tb name

    TB102.Text = STRback;   //result in TB box 02
    //how do I use TbHalf instead?
    //I need to take tbHalf and add 02 to it so I can use this code on any TextBox.
}

I've been trying to figure this out but once I get into [get][set] and more elaborate code I lose it.我一直在试图解决这个问题,但是一旦我进入 [get][set] 和更复杂的代码,我就失去了它。 I'm a graphic designer, not really a programmer, but I'm trying to learn.我是一名平面设计师,并不是真正的程序员,但我正在努力学习。

What you are trying to do is "change" the names of declared variables, which doesn't make any sense.您要做的是“更改”已声明变量的名称,这没有任何意义。 The name of a variable is immutable - once you set the name, you can't change it.变量的名字是不可变的——一旦你设置了名字,你就不能改变它。 That's because the name of a variable isn't a value itself, but rather an arbitrary identifier of what variable you are working with.这是因为变量的名称不是值本身,而是您正在使用的变量的任意标识符。

From what I understand, you are trying to determine whether which of two text boxes triggered an event.据我了解,您正在尝试确定两个文本框中的哪一个触发了事件。 However, using:但是,使用:

TextBox tbFull = (TextBox)sender;
TextBox tbHalf = (TextBox)sender;

isn't going to help you determine which text box triggered the event.不会帮助您确定哪个文本框触发了事件。 All it does is give you two variables that point to the exact same value in memory, which is redundant.它所做的只是为您提供两个指向内存中完全相同值的变量,这是多余的。

To do this, you need to give your text boxes distinct names in the designer view (under "Name" in the Properties box), then use those names against sender to determine which text box triggered the event.为此,您需要在设计器视图中(在“属性”框中的“名称”下)为文本框指定不同的名称,然后针对发件人使用这些名称来确定哪个文本框触发了事件。

private void TB_Half_TextChanged(object sender, EventArgs e)
{
    // Determine the source and target boxes
    TextBox source = (TextBox)source;
    TextBox target;

    if (sender == TB1)
        target = TB102;        // TB1triggered the event, so update TB102
    else if (sender == TB102)
        target = TB103;        // TB102 triggered the event, so update TB103
    else
        return;                // Somehow something else triggered the event, 
                               // and we don't know what to do when that happens

    // Whichever text box triggered the event, the math is the same
    target.Text = Convert.ToString(Convert.ToInt32(source.Text) / 2);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM