简体   繁体   中英

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.
  • TB102 needs to divide TB1 in half.
  • TB103 needs to divide in quarter.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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