如何查找单击哪组文本框

时间:2014-11-29 20:57:21

标签: c# textbox

我有两个Textbox []

_textBox1 = new TextBox[] {textBox1,textBox2...};
_textBox2 = new TextBox[] {textBox10,textBox11....};

第一个有11个元素,第二个有6个元素。

但两者都使用相同的光标向上/向下程序(使用标签)

对象发件人是否可以使用哪个文本框组?

在下面的程序中我如何使用_textBox1和_textBox2

    private void KeyMove_Click(object sender, MouseEventArgs e)
    {
        var textBox = sender as TextBox;                 //from there
        if (textBox != null)                 
        {
            if (textBox.AccessibleName == "first")
            {
                Yer = 0;
            }
            else if (textBox.AccessibleName ==null)
            {
                Yer = 1;
            }
        }                                                // till here with idea of MarcinJuraszek
        try
        {
            string id = (sender as Control).Tag.ToString();
            KeyIndex = Convert.ToInt32(id);
        }
        catch (Exception e1)
        {
            MessageBox.Show(e1.Message.ToString());
        }
        if (grp == 0)                         // I am using 1st group of textbox
        {
            _textBox[KeyIndex].BackColor = Color.Red;
            if (KeyIndex == 0 && !excurPnl.Visible)
            {
                _textBox[1].Text = "";
                DataReadIndex = 0;
                LoadParam = Suivant;
                SlidePlace = PopupPlace;
                Order_by = " id ";
                SortDir = " ASC";
                GeneralMethod.ChangeColor(0, _label, 4);
                PageNbr = GeneralMethod.GetPageNbr(" tours ", 1, "", "", "", "");
                ShowExcursion();
                excurPnl.Visible = true;
                UpPic.Left = excurPnl.Left;
                DnPic.Left = excurPnl.Left + excurPnl.Width - DnPic.Width;
                nextprevpanel.Top = excurPnl.Top + excurPnl.Height + 5;
                nextprevpanel.Left = excurPnl.Left + (excurPnl.Width - nextprevpanel.Width) / 2;
            }
            else if (KeyIndex != 0 && excurPnl.Visible)
                excurPnl.Visible = false;
            if (KeyIndex == 1)
            {
                takvim.Visible = true;
                nextprevpanel.Visible = false;
                UpPic.Visible = false;
                DnPic.Visible = false;
            }
            else if (KeyIndex != 1)
            {
                takvim.Visible = false;
                nextprevpanel.Visible = true;
                UpPic.Visible = true;
                DnPic.Visible = true;
            }
            if ((KeyIndex > 1 && !servicePnl.Visible) || (OldPass != KeyIndex && (KeyIndex == 6 || KeyIndex > 9)) || (OldPass == 6 || OldPass > 9) && KeyIndex != OldPass)
                {
                    DataReadIndex = 0;
                    Order_by = " service ";
                    SortDir = " ASC ";
                    LoadParam = Suivant;
                    SlidePlace = RightPlace;
                    GeneralMethod.ChangeColor(2, _label1, 5);
                    if (KeyIndex > 1 && KeyIndex < 6 || KeyIndex > 6 && KeyIndex < 10)
                    {
                        PageNbr = GeneralMethod.GetPageNbr("firme_prix", 3, " WHERE status = false ", "", "", "");
                    }
                    else
                    {
                        PageNbr = GeneralMethod.GetPageNbr("firme_prix", 3, " WHERE status = true ", "", "", "");
                    }
                    ShowService();
                    UpPic.Left = servicePnl.Left;
                    DnPic.Left = servicePnl.Left + servicePnl.Width - DnPic.Width;
                    nextprevpanel.Top = servicePnl.Top + servicePnl.Height + 5;
                    nextprevpanel.Left = servicePnl.Left + (servicePnl.Width - nextprevpanel.Width) / 2;
                    servicePnl.Visible = true;
                    panel1.Visible = false;
                    resultPnl.Visible = false;
                }
                else if (KeyIndex < 2 && servicePnl.Visible)
                    servicePnl.Visible = false;
            if (_textBox[0].Text != "" && _textBox[1].Text != "")
                createBtn.Enabled = true;
            else if (_textBox[0].Text == "" && _textBox[1].Text == "")
            {
                clearpnl1();
            }
            OldPass = KeyIndex;
            _textBox[KeyIndex].Select();
        }
        else                                      // 2nd group of textbox
        {
            _textBox2[KeyIndex].Select();
            CheckIfReady();
        }
    }

1 个答案:

答案 0 :(得分:4)

您可以尝试将sender投射到TextBox

var textBox = sender as TextBox;

然后检查哪个组包含该实例:

if(textBox != null)
{
    if(_textBox1.Contains(textBox))
    {
        // first group
    }
    else if(_textBox2.Contains(textBox))
    {
        // second group
    }
    else
    {
        // some other textbox?
    }
}
else
{
    // sender is not a TextBox instance
}

您需要在文件顶部using System.Linq;拨打Contains电话。