开发者

Controls added dynamically don't display on my form

开发者 https://www.devze.com 2023-04-12 19:15 出处:网络
I´m adding controls dynamically onto my form, but I can\'t see those new controls. My code is: frmFormulario myform = new frmFormulario();

I´m adding controls dynamically onto my form, but I can't see those new controls.

My code is:

frmFormulario myform = new frmFormulario();

for (int i = 0; i < elements.Count; i++)
{
   String nm = elements[i].name;
   String chk = 开发者_如何学编程"chk"+nm;
   CheckBox checkboxWS = new CheckBox();
   checkboxWS.Name = chk;
   checkboxWS.Checked = true;
   checkboxWS.Visible = true;

   checkboxWS.Width.Equals(40);


   myform.Controls.Add(checkboxWS);
   myform.Controls.SetChildIndex(checkboxWS, 0);
}

Can anybody spot the problem?

Thanks

My environment is C# Visual Studio 2010


Try adding

myform.Show();  //or myform.ShowDialog;

after the for loop. If you want to completely work on this new window (myform) and discard the former or parent then try creating it in a new thread.

Hope it helps.


You are creating a new form inside of the loop as well as the controls. Once the loop is done it falls out of scope. I'm sure you didn't want to do that, rather add them to an existing form.


Where do you show myform, the instance of frmFormulario?
Currently you are creating a new form and you don't display it.

0

精彩评论

暂无评论...
验证码 换一张
取 消