How to use foreach for the textboxes in a panel

12,033

Solution 1

Sorry disturbing, foreach loop begins from the greater tabindex to the lower. As I was hiding the textboxes unused, this is the greater tabindex textboxes which is receiving the value of CmpStr.

if (inglist.SelectedIndex > -1 && rows > 0 && SelectedExist == false)
{
            
    foreach (Control txt in mypanel.Controls.Cast<Control>().OrderBy(c => c.TabIndex))
    {
        if (txt is TextBox && txt.Text == "")
        {
            txt.Text = CmpStr;
            break;
        }
        else if (txt is TextBox && txt.Text == CmpStr)
            break;
    }
}

    

And that works well.

Solution 2

I think your question can be improved, but IF I understood it right, you want to iterate trough your textoxes.

EDIT (to adapt to below comment):

Also, some code is missing, as I don't know about your switch stantement, your case (j+1), I don't know the context of the code.

Ignoring your switch statemente, I'd suggest you code to look something like this:

string CmpString;
Boolean SelectedExist;
CmpStr=Convert.ToString(inglist.Items[inglist.SelectedIndex]);
SelectedExist = IfThisExist(CmpStr);

if (inglist.SelectedIndex > -1 && rows > 0 && SelectedExist == false)
{
    var sortedTextboxes = mypanel.Controls
        .OfType<TextBox>() // get all textboxes controls
        .OrderBy(ctrl => ctrl.TabIndex); // order by TabIndex

    foreach (TextBox txt in sortedTextboxes)
    {
        // No need to cast :)

        if(txt.Text == "") 
        {  
            tBox.Text = CmpStr;
            break;
        }

        if(txt.Text == CmpStr) { break; }
    }
}

Notice the OfType<TextBox> method, which returns only textboxes.

However, this code is not of much help without modification, because text will only be set to textbox if it is empty. Once again, adapt the code to fit your needs.

Share:
12,033
Ismail Gunes
Author by

Ismail Gunes

Like play guitar and read and learning programming

Updated on June 04, 2022

Comments

  • Ismail Gunes
    Ismail Gunes almost 2 years

    I have 9 textboxes created in design time. By clicking a listbox (inglist created in design time) i select an item, IfThisExist(CmpStr) function verify if i've allready choosed this one. When I debug the value of CmpStr is passed to txt.Text but it's not displayed in on screen.

    string CmpString;
    bool SelectedExist;
    CmpSt r = Convert.ToString(inglist.Items[inglist.SelectedIndex]);
    SelectedExist = IfThisExist(CmpStr);
    
    if (inglist.SelectedIndex > -1 && rows > 0 && SelectedExist == false)
    {
      foreach (Control txt in mypanel.Controls)
      {
        if (txt is TextBox && txt.Text == "" )
        {
          txt.Text = CmpStr;
          break;
        }
        else if (txt is TextBox && txt.Text == CmpStr)
            break;
      }
    

    If I do same thing with the method below it works.

    //for (int j = 0; j < rows; ++j)
    //{
    //   switch (j + 1)
    //{
    //  case 1:
    //    if (textBox1.Text == "" && textBox1.Text!= CmpStr) 
    //  {
    //    textBox1.Text = CmpStr;
    //  passed = true;
    //   }
    //  break;
    

    .....

    What can be the reason ?