I have a list of controls and their locations in a datatable and I would like to go through the whole datatable and set the location of each control. The difficulty I have is defining which control I want to set the location for. For example one can set the location of a control as follows:
button1.Location = new Point(xpos, Ypos);
button2.Location = new Point(xpos, Ypos);
The thing is that I can't hard code the names of all the controls before hand. This is the code that I currently use: The datatable contains one row for every control in the form, and has three columns: the control name, the x location and the y location.
int rowPosition = 0;
Control x;
string controlName;
int xCoord;
int yCoord;
foreach (DataRow row in dtControlPosition.Rows)
{
controlName = dtControlPosition.Rows[rowPosition]["Control"].ToString();
xCoord = Convert.ToInt32(dtControlPosition.Rows[rowPosition]["XCoord"].ToString());
yCoord = Convert.ToInt32(dtControlPosition.Rows[rowPosition]["YCoord"].ToString());
// don't have any idea what to do here:
// I tried x.Name = controlName
// x.Location = new Point(Convert.ToInt32(xCoord),Convert.toInt32(yCoord));
rowPosition = rowPosition +1;
}
When I try the above I receive an error "use of unassigned local variable "x"."