繁体   English   中英

文本框选项卡在Enterprise Architect加载项中不起作用

[英]Text Box Tabbing not working in Enterprise Architect add-in

我该如何在Windows窗体中设置制表符,使要浏览的文本框数量是动态的(取决于以前的用户输入)?

我现在在做什么

这样可以很好地创建文本框,但是我无法通过它们进行制表。 注意: numStates是用户以以前的形式输入的int。

更新 :我只隔离了这段代码,并在VS 2010中对其进行了测试,并且制表符有效,但在我的最终版本中却没有。 (请参阅背景。)

背景 :这用于Enterprise Architect(EA)的加载项中。 我正在通过.msi安装程序部署外接程序,并在EA中测试最终安装,所以制表不起作用。 我现在猜想,由EA加载项创建的表单中的制表符有些不兼容??

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

我看过的

C#Windows窗体Tab顺序

如何检测C#中的Tab键按下?

  • 以上两个问题的答案影响了我现在正在做的事情。 我试图以编程方式设置选项卡顺序。

动态添加事件处理程序到窗口窗体

  • 我认为我无法执行此操作,因为文本框没有“ _CheckedChanged”,我只想在按下Tab键时进行更改。

在Visual Studio 2013中,这似乎可以正常工作。不确定所使用的版本。 我建议删除AcceptsTab。 这通常意味着(至少对于RichTextBoxes而言),该控件将拦截制表符并插入一系列空格,而不是跳转到下一个制表位。 参见下面的代码:

  • 注意,我删除了这一行:textBoxes [index] .AcceptsTab = true;
  • 注意,我添加了:this.Controls.Add(textBoxes [index]); 不知道您是否已经解决了这个问题

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

还想指出,虽然TextBox控件上有一个LostFocus事件,但可以使用以下事件:

    textBoxes[index].LostFocus += Form1_LostFocus;

并这样处理:

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

以下内容可能有点hack,但应该可以使用:

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

请注意,我将numStates移到外面以模仿它是用户输入。 另外,我将TabStop设置为false,只是为了确保Windows事件不会在不同的环境中触发,因为它现在由KeyPress事件处理。

暂无
暂无

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

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