0

I have many group Boxes in a flow layout panel all are generated programmaticaly. when I try to find any specific groupbox at run time nothing comes out. Here is my code, Please help.

    foreach (Control ctr in flowLayoutPanel1.Controls)
    {
        if (ctr.Name=="BSE")
        {
            MessageBox.Show("Control is found");                     
        }                
    }

Code, which creates controls:

var Allzone = (from a in db.Zones select a.name).ToList();
foreach (var z in Allzone)
{ 
    GroupBox g = new GroupBox(); 
    g.Text = z; 
    g.Name = z; 
    g.Tag = z; 
    g.Font = new Font("Verdana", 8,FontStyle.Bold); 
    g.ForeColor = Color.White; 
    g.Width = 49; 
    g.Height = 90; 
    flowLayoutPanel1.Controls.Add(g); 
} 
StaWho
  • 2,488
  • 17
  • 24
Umar Abbas
  • 4,041
  • 2
  • 23
  • 21
  • Show the code that creates the controls and assigns them to `flowLayoutPanel1`. – DonBoitnott Jun 12 '13 at 14:07
  • >var Allzone = (from a in db.Zones select a.name).ToList(); > foreach (var z in Allzone) > { > GroupBox g = new GroupBox(); > g.Text = z; > g.Name = z; > g.Tag = z; > g.Font = new Font("Verdana", 8,FontStyle.Bold); > g.ForeColor = Color.White; > g.Width = 49; > g.Height = 90; > flowLayoutPanel1.Controls.Add(g); > } – Umar Abbas Jun 12 '13 at 14:18
  • What is `Allzone` and what does it contain? – DonBoitnott Jun 12 '13 at 14:18
  • AllZone contain the name of all zone But this is not a problem Because All Control generated perfectly! the only problem i m facing is how to find a control in flow panel – Umar Abbas Jun 12 '13 at 14:23
  • C# is case-sensitive, so if the name you assigned was `bse` and you test for `BSE` that will not equate. – DonBoitnott Jun 12 '13 at 14:25
  • "i also try this nothing happens" if (ctr.Name.ToUpper() == "BSE".ToUpper()) – Umar Abbas Jun 12 '13 at 14:30
  • I just setup a test that works. In place of `Allzone` I used a `String[]` (since I don't know what yours _really_ is). Otherwise, my code is identical to yours. I suspect there is more going on here, or `Allzones` is not simply a collection of `String` objects. – DonBoitnott Jun 12 '13 at 14:38
  • I got it just add trim() in control name..Now the working Code is foreach (Control ctr in flowLayoutPanel1.Controls) { if (ctr.Name.Trim()=="BSE") { MessageBox.Show("Control is found"); } } this Trim() Ruined my all day.Thanks DonBotinott – Umar Abbas Jun 12 '13 at 14:42
  • Do the Trim() and ToUpper() when you set the Name property: `g.Name = z.Trim().ToUpper();` Then your check should behave better without needing all the extra stuff... – Idle_Mind Jun 12 '13 at 15:06

1 Answers1

0

I got it just add trim() in control name.

Now the working Code is

 foreach (Control ctr in flowLayoutPanel1.Controls) 
{ 

if (ctr.Name.Trim()=="BSE") 

   {
     MessageBox.Show("Control is found");
   } 
}

this Trim() Ruined my all day. Thanks DonBotinott

Umar Abbas
  • 4,041
  • 2
  • 23
  • 21