Dynamically Adding Controls to a Windows Form - Creating the Controls
(Page 4 of 5 )
You will note that the above method calls a method named CreateRow. This is where the actual dynamic creation takes place. Here is what the method looks like:
private int CreateRow(string name, string dosage) {
// Calculate the vertical position of the controls.
// Do this by multiplying the number of items already added
// (and stored in nudControls)
// by the height of each item, plus 32 for the spacing at the top
int yPos = (nudControls.Count * 24) + 32;
// Calculate the tab order.
// Do this by multiplying the number of items already added
// (and stored in nudControls)
// by the number of controls we are adding on each row
int tabIndex = (nudControls.Count * 3);
// Create three form controls, a check box (cbName),
// a text box (txtDosage), and a numeric up down (nudNum).
// cbName
CheckBox cbName = new CheckBox();
cbName.Location = new System.Drawing.Point(8, yPos);
cbName.Name = "cbName" + name;
cbName.Size = new System.Drawing.Size(144, 22);
cbName.TabIndex = tabIndex++;
// Note: Here we not only set the text of the item by setting it
// to the given name, storing this here will allow us to
// retrieve it on a mouse up event.
cbName.Text = name;
cbName.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.cbName_MouseUp);
// txtDosage
TextBox txtDosage = new TextBox();
txtDosage.Location = new System.Drawing.Point(160, yPos);
txtDosage.Name = "txtDosage" + name;
txtDosage.Size = new System.Drawing.Size(48, 22);
txtDosage.TabIndex = tabIndex++;
txtDosage.Text = dosage;
// nudNum
NumericUpDown nudNum = new NumericUpDown();
nudNum.Location = new System.Drawing.Point(224, yPos);
nudNum.Name = "nudNum" + name;
nudNum.Size = new System.Drawing.Size(40, 22);
nudNum.TabIndex = tabIndex++;
// Add the new controls to this form
this.Controls.Add(cbName);
this.Controls.Add(nudNum);
this.Controls.Add(txtDosage);
// Add the numeric up down to our internal list so we
// can later retrieve it. The key is the name of the row.
nudControls.Add(name, nudNum);
return yPos;
}
Again, the code is pretty self-explanatory.
A few notes: The controls are added to the form using the this.Controls.Add method (this is the same call used for all controls in the form, and can be found in the InitializeComponents method Visual Studio creates for you).
Also, observe that we store the NumericUpDown control for later retrieval in our hash table, nudControls. The key for the control value in the hash table is the name of the medication. Since all of my medications can be uniquely identified by the name, this makes sense. But since the hash table complains with an exception if you try to add an item with an identical key, if you have a situation where we tried to add medications with the same name (and perhaps different dosages), we would not be successful. You may need to modify the code and XML to have a unique ID used to store the values.
Regardless, note that I also set the checkbox Text property to the name of the medication. This comes in handy later when we handle the Mouse Up event. Note in the checkbox creation that adding an event handle is as simple as calling the following, passing in the method that will be called when the event takes place:
cbName.MouseUp += new
System.Windows.Forms.MouseEventHandler(this.cbName_MouseUp);
Lastly, I return the calculated yPos variable so I can store it (in LoadFromXML) for a final resizing of the form.
Next: Handling the Mouse Up Event >>
More .NET Articles
More By W. Daniel Skousen