Dynamic control creating and naming

mjohnson3091

Freshman
Joined
Aug 10, 2004
Messages
31
Location
Lancashire, UK
I'm looking at creating a form layout dynamically from an XML document.

The parsing of the XML data i'm fine with and the creating of the controls I think I'm ok with, it's just the naming of them.

The code below is a cutdown example of what I'm using to draw the form.

I'm using C# and hope to deploy this onto mobile devices at some point.

Code:
// Loop around the number of Data items and draw the form
for(intLoop=0;intLoop<dtData.Rows.Count;intLoop++)
{
   switch(dtData.Rows[intLoop]["Type"])
   {					
       case "Button":
       {
	Button btn1 = new Button();
	btn1.Width=dtData.Rows[intLoop]["Width"];
	btn1.Height=dtData.Rows[intLoop]["Height"];
//	..... etc. for the properties	
	this.Controls.Add(btn1);
	btn1.Visible=true;
      }
   }
}

In this example I'm using the name btn1, would I be better passing the names in the XML message? If so, how can I use the string field I get as a name (i.e.: If I pass the string "Button1" in the XML message and assign it to a string variable, strName, then will the new Button not take the name strName instead of its value)?

Hope that makes sense.

Thanks in advance.
 
Machaira said:
Unless I'm missing something, you just set the Name property like you would any other.

No - you're right, it was me that was missing something this morning - like the brain I left at home! :p

It has advanced onto another issue which I have, which is controlling the events of the newly created controls.

I've managed to creat and handle the click event for the button without a problem, but when I try to handle the mouse events on an image I seem to have a problem.

Code:
// img1 is my newly created PictureBox control
img1.MouseMove += new System.Windows.Forms.MouseEventHandler(subWrite);


private void subWrite(object sender, System.Windows.Forms.MouseEventArgs e)
{
  bool test;
  if(mblnPenDown)
  {
     // How do I associate the Sender Object to the PitcureBox??
     xxxxx.BackColor=Color.Blue;
     test=true;
  }
}

How do I associate the Sender Object to the actual picturebox I wish to use??
 
Back
Top