Finding Index Through Control Control Collection In FLP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheshwag
    New Member
    • Aug 2010
    • 71

    Finding Index Through Control Control Collection In FLP

    I have a FlowlauoutPanel and Two Textboxes In it on Runtime like below:

    Code:
    1st TextBox
    
    TextBox tb=new TextBox();
    tb.Location=tbpoint; // declared in Class Body
    tb.Name="tb"+i.tostring();
    tbpoint.Y+=70;
    this.FlowLayoutPanel1.Controls.Add(tb);
    i++;
    
    2nd TextBox
    
    TextBox bb=new TextBox();
    bb.Location=tbbpoint; // declared in Class Body
    bb.Name="bb"+i.tostring();
    tbbpoint.Y+=70;
    this.FlowLayoutPanel1.Controls.Add(bb);
    i++;
    both works fine. my question is how to find out which textboxes are empty.

    Suppose user created five textboxes each. and enter data into three textboxes each. and other are empty so how to find which textboxes are empty.

    Note: above are runtime textboxes which may be more that five or less than five.

    Kindly provide some example coding.

    I am Practicing in C#,VS-2005
  • hype261
    New Member
    • Apr 2010
    • 207

    #2
    Couldn't you cycle through all the controls contained in the FlowLayoutPanel casting them to a textbox and check if they are empty?

    FlowLayoutPanel has a Controls property that gets the collection of controls contained within the control.

    Here is the MSDN article.

    Comment

    • Christian Binder
      Recognized Expert New Member
      • Jan 2008
      • 218

      #3
      Another approach would be if you add the generated textBoxes to a List<TextBox> over which you can iterate with foreach an check for empty TextBoxes.

      Using hype261's approach, you could simply doing that with LINQ:
      Code:
      var emptyTextBoxes = from textBox in Controls.OfType<TextBox>()
                           where textBox.Text != string.Empty
                           select textBox;
      //or
      var emptyTextBoxes = Controls.OfType<TextBox>().Where(textBox => textBox.Text == string.Empty);

      Comment

      • maheshwag
        New Member
        • Aug 2010
        • 71

        #4
        Christian Binder Sir,

        Sir the above answer is about to LINQ basis but i am using VS-2005 how can apply it in this version of software.

        thx for ur reply. please guide me.

        Comment

        • maheshwag
          New Member
          • Aug 2010
          • 71

          #5
          my apologize i am not read ur full post just looking into coding only. that's why i am not concentrate towards ur line Using hype's261 approach.
          how can use it in VS-2005 as i have no idea about it.

          Comment

          • Christian Binder
            Recognized Expert New Member
            • Jan 2008
            • 218

            #6
            You should use a for-each-loop to iterate over all controls within the FlowLayoutPanel .
            Code:
            TextBox txt;
            foreach(Control control in this.FlowLayoutPanel1.Controls) {
              if((txt = control as TextBox) != null && txt.Text == string.Empty) {
                //txt is an empty textbox ...
              }
            }
            Explanation: As you're iterating over Controls (which can be others than TextBoxes), you have to cast them to TextBox. Using control as TextBox this will cast your control to a TextBox if that's possible, otherwise this command will return null.
            If you're sure, that the FlowLayoutPanel only contains TextBoxes, you could to the following:
            Code:
            foreach(TextBox textBox in this.FlowLayoutPanel1.Controls) {
              if(textBox.Text == string.Empty) {
                //...
              }
            }
            But this would throw an Exception, if other objects than TextBoxes are within your FlowLayoutPanel

            Comment

            • maheshwag
              New Member
              • Aug 2010
              • 71

              #7
              Christian Binder Sir,

              Thx for ur answer and i am apreciated it. but sir am already voted for this question. am feel sorry for it. is this possible to voting two times?

              Comment

              • Christian Binder
                Recognized Expert New Member
                • Jan 2008
                • 218

                #8
                Oh, I don't think that's possible. But it's not that bad, if I dont get a vote :-)

                Comment

                Working...