【问题标题】:Text Box Tabbing not working in Enterprise Architect add-in文本框选项卡在 Enterprise Architect 插件中不起作用
【发布时间】:2015-01-17 07:25:30
【问题描述】:

如何在 Windows 窗体中设置选项卡,其中要通过选项卡的文本框数量是动态的(取决于以前的用户输入)?

我现在在做什么

这可以很好地创建文本框,但我无法通过它们进行选项卡。 注意:numStates 是用户在之前的表单中输入的 int。

更新:我只隔离了这段代码并在 VS 2010 中对其进行了测试,并且选项卡可以正常工作,但在我的最终版本中却没有。 (见背景。)

BACKGROUND:用于 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 form Tab Order

How to detect tab key pressing in C#?

  • 以上两个问题的答案影响了我现在所做的事情。我是 尝试以编程方式设置跳位顺序。

Adding Event Handler for Dynamically Created to window Form

  • 我认为我不能这样做,因为没有“_CheckedChanged” 文本框,我只想在它们按 Tab 时进行更改。

【问题讨论】:

  • 如果你有动态数量的文本框 - 尝试使用DataGridView control

标签: c# winforms visual-studio-2010 visual-studio enterprise-architect


【解决方案1】:

这似乎在 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);
    }

【讨论】:

  • 我已取出“接受”选项卡并在问题中更新了我的代码 sn-p。我在其他地方添加了控件。我用我尝试过的方法更新了我的问题。你是对的,当我只调试这个表单时它确实有效,但在我的最终实现中仍然无效。
【解决方案2】:

以下内容可能有点麻烦,但应该可以:

    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 事件处理。

【讨论】:

  • 我喜欢这个 hack,在调试器中它可以工作,但在最终实现中不行。我很确定问题与 EA 无法识别表单中的按键或拦截它们有关?我不知道。我认为这已经离开了 Windows 窗体问题的领域。
猜你喜欢
  • 1970-01-01
  • 2018-01-20
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-17
相关资源
最近更新 更多