简体   繁体   中英

Text Box Tabbing not working in Enterprise Architect add-in

How can I set up tabbing in a windows form where the number of text boxes to tab through is dynamic (depending on previous user input)?

What I'm doing now

This creates the text boxes just fine, but I cannot tab through them. Note: numStates is an int that is inputed by the user in a previous form.

UPDATE : I've isolated just this code and tested it in VS 2010 and the tabbing works, but in my final version it does not. (See Background.)

BACKGROUND : This is used in an add-in for Enterprise Architect (EA). I'm deploying the add-in through a .msi installer and testing the final installation within EA and the tabbing does not work. I'm guessing now that there's some incompatibility with tabbing in a form created by an EA add-in??

System.Windows.Forms.TextBox[] textBoxes = new System.Windows.Forms.TextBox[numStates];
for (int index = 0; index < textBoxes.Length; index++)
{
    textBoxes[index] = new System.Windows.Forms.TextBox();
    textBoxes[index].Location = new System.Drawing.Point(126, yLocation);
    textBoxes[index].Name = "stateName" + index;
    textBoxes[index].Size = new System.Drawing.Size(161, 20);
    textBoxes[index].TabStop = true;
    textBoxes[index].TabIndex = index;
    this.Controls.Add(textBoxes[index]);
    textBoxes[0].Focus();
    yLocation += 25;
}

What I've looked at

c# windows form Tab Order

How to detect tab key pressing in C#?

  • The answers to the above two influenced what I'm doing now. I'm attempting to set the tab order programmatically.

Adding Event Handler for Dynamically Created to window Form

  • I don't think I can do this because there's no "_CheckedChanged" for text boxes and I only want to change when they press tab.

This seems to work just fine in Visual Studio 2013. Not sure which version you are using. I would suggest removing the AcceptsTab. That usually means (for RichTextBoxes at least), that the control will intercept the tab and insert a series of spaces, instead of jumping to the next tab stop. See code below:

  • Notice I took out the line: textBoxes[index].AcceptsTab = true;
  • Notice I added: this.Controls.Add(textBoxes[index]); ( Not sure if you're already addressing this )

      int numStates = 5; int yLocation = 0; System.Windows.Forms.TextBox[] textBoxes = new System.Windows.Forms.TextBox[numStates]; for (int index = 0; index < textBoxes.Length; index++) { textBoxes[index] = new System.Windows.Forms.TextBox(); textBoxes[index].Location = new System.Drawing.Point(126, yLocation); textBoxes[index].Name = "stateName" + index; textBoxes[index].Size = new System.Drawing.Size(161, 20); textBoxes[index].TabStop = true; textBoxes[index].TabIndex = index; this.Controls.Add(textBoxes[index]); textBoxes[0].Focus(); yLocation += 25; } 

Also wanted to point out that while there is a LostFocus event on a TextBox Control, which can be used like the following:

    textBoxes[index].LostFocus += Form1_LostFocus;

And handled thusly:

    void Form1_LostFocus(object sender, EventArgs e)
    {
        MessageBox.Show("Lost Focus From: " + ((Control)sender).Name);
    }

The following would be a bit of a hack, but it should work:

    private int numStates = 5;
    private void Form1_Load(object sender, EventArgs e)
    {            
        int yLocation = 0;
        System.Windows.Forms.TextBox[] textBoxes = new System.Windows.Forms.TextBox[numStates];
        for (int index = 0; index < textBoxes.Length; index++)
        {
            textBoxes[index] = new System.Windows.Forms.TextBox();
            textBoxes[index].Location = new System.Drawing.Point(126, yLocation);
            textBoxes[index].Name = "stateName" + index;
            textBoxes[index].Size = new System.Drawing.Size(161, 20);
            textBoxes[index].AcceptsTab = true;
            textBoxes[index].TabStop = false;
            textBoxes[index].TabIndex = index;

            textBoxes[index].KeyPress += Form1_KeyPress; //Added line

            this.Controls.Add(textBoxes[index]);
            textBoxes[0].Focus();                
            yLocation += 25;                
        }            
    }
    void Form1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if(e.KeyChar == '\t')
        {
            int currentState = int.Parse(((Control)sender).Name.Replace("stateName", ""));
            if(currentState == numStates - 1)
            {
                this.Controls["stateName" + (0).ToString()].Focus();
            }
            else
            {
                this.Controls["stateName" + (currentState + 1).ToString()].Focus();
            }
        }
    }

Note that I moved numStates outside to mimic that it is user input. Also, I set TabStop to false, just to ensure that the windows event doesn't fire in different environments, as it is now being handled by the KeyPress event.

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