简体   繁体   中英

C#: How can I programmatically loop through all controls on a Windows Form

I'm in the process of creating a front-end Windows Forms application, which will have multiple uses. One of those uses will be to allow the end-user to manage the contents of a database table.

At the moment, my form contains a DataGridView object which uses a DataTable as its source. That datatable is populated from a stored procedure which selects every row from the table based on criteria specified by the end user. I want to put code into the DataGridView which will look at the row that's been selected by the user, and will populate a series of text boxes with the details from that row.

I want to avoid hard-coding a text box for each column in the DataGridView, since it's highly possible that in the future I might add columns to the underlying stored procedure. Therefore, my plan is that the first time the user selects a row, the application would dynamically create a text box for each column, size that text box appropriately (both height and width) and add the appropriate labels.

So far, the code I've written creates a brand-new DataTable, and adds columns to store the following information about each column in the DataGridView:

  1. The actual name of the DataGridView column;
  2. The name that I wish to give to the associated label;
  3. The text that I wish to assign to the associated label;
  4. The top and left positions of the associated label;
  5. The name that I wish to give to the associated text box;
  6. The value that I wish to display in the associated text box;

So far when the user initially clicks on a row, everything is working well: the text boxes get created in a vertical column, with the associated label to the left of each text box and the appropriate value displayed in each one. Each text box has its width dynamically set based on the contents of that text box so that everything can easily be seen.

The difficulty comes when the user changes from one row of the DataGridView to another. The code that runs to create each control looks like this:

            if (pnlManageJobManagerOutcomes.Controls.Contains(txtCurrent))
            {

                pnlManageJobManagerOutcomes.Controls.Remove(txtCurrent);

            }
            if (!pnlManageJobManagerOutcomes.Controls.Contains(txtCurrent))
            {

                pnlManageJobManagerOutcomes.Controls.Add(txtCurrent);

            }

When the code first runs, all seems to be fine. However, if it gets fired a second time (at which point it should remove and re-create the control) it doesn't seem to detect the prior existence of the control and simply creates a second copy of it in the form.

For information, the form I'm building has multiple uses which are dictated by a series of buttons. Therefore, I'm creating the controls for this particular use inside a Panel (pnlManageJobManagerOutcomes). I'm completely stumped as to why the code doesn't spot the existence of those controls during subsequent executions.

TIA

I've figured out a way to do it, which may seem to be clunky but which seems to work.

Since it doesn't appear that I can check to see whether the Panel I'm working with Contains the field name, I've written a method which will loop through the controls in that Panel and return a count of the number of instances of that control in the Panel's Controls array. If the method returns a 0, this means the Control doesn't exist in that Panel and I can go ahead and create it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM