如何重绘动态添加的控件 [英] How to redraw the controls which were added dynamically
问题描述
您好,
这里我从checkedListBox中选择项目并通过检查将它们保存在ListBox中,之后有一个按钮为>>它存储列表框中的已检查项目,我根据列表框的项目计数生成文本框。所有操作都在>>上进行按钮的点击事件。
以下是代码:
foreach (对象项目 checkedListBox1.CheckedItems)
{
listBox1.Items.Add(items);
}
int camCount = listBox1 .Items.Count;
arrCam = new TextBox [camCount];
int dist = 0 ;
for ( int i = 0 ; i < camCount; i ++)
{
TextBox tx = new TextBox();
arrCam [i] = tx;
}
foreach (TextBox fortx1 arrCam)
{
fortx1.Location = new Point( 448 , 13 + dist);
groupBox8.Controls.Add(fortx1);
dist + = 25 ;
}
这里问题是当我检查checkedlistbox中的所有项目时,项目转到列表框和然后根据列表框的项目计数生成文本框。到这里它工作正常,现在当我取消选中checkedlistbox中的任何项目总数。文本框也应该改变,但是那些文本框没有重新绘制,只有以前绘制的文本框保留在窗体中。
有人可以帮我解决我出错的地方或者在哪里缺少一段代码?
为checkedListBox1添加一个事件,当已检查的项目发生更改时,该事件将触发。在此事件中,您可以刷新groupBox8中的控件。
对于刷新,您可以清除所有现有的文本框并在此事件中添加新的onces或添加/删除differenz checkListBox中检查项的privius计数。
此后,您可以使用groupBox8.Refresh()<刷新组合框/ pre>重新绘制Control和所有子控件。
用于清除GroupBox我建议
for ( int i = 0 ; i < arrCam.Length; i ++)
{
arrCam [i] .Visible = false ;
arrCam [i] .Dispose();
}
groupBox8.Refresh();
我将TextBox的Visible设置为false以减少闪烁在处理控件时。
Hello,
Here i am selecting the items from checkedListBox and saving them in ListBox by checking and after that there is a button as ">>" which stores checked items in listbox and I am generating the textboxes according to the item count of list box. all operations are carried on ">>" button's click event.
Below is code:
foreach (Object items in checkedListBox1.CheckedItems)
{
listBox1.Items.Add(items);
}
int camCount = listBox1.Items.Count;
arrCam = new TextBox[camCount];
int dist = 0;
for (int i = 0; i < camCount; i++)
{
TextBox tx = new TextBox();
arrCam[i] = tx;
}
foreach (TextBox fortx1 in arrCam)
{
fortx1.Location = new Point(448, 13 + dist);
groupBox8.Controls.Add(fortx1);
dist += 25;
}
here the problem is when i check all the items in checkedlistbox,items go to listbox and then generate the textboxes according to the listbox's item count.Till here it working fine,now when i uncheck any item from checkedlistbox the total no. of textboxes should also change but those textboxes are not getting redrawn only previously drawn textboxes remain in form.
Can someone help me out where I am going wrong or where is the piece of code missing?
Add an event for the checkedListBox1 which fires when the checked items changed. On this event you can refresh the controls in you groupBox8.
For the refresh you can clear all existing Textboxes and add new onces in this event or add/remove the differenz of the privius count of the check items in the checkedListBox.
After this you can Refresh the groupbox withgroupBox8.Refresh()which redraws the Control and all child Controls.
for clearing the GroupBox i would suggest
for (int i = 0; i < arrCam.Length; i++) { arrCam[i].Visible = false; arrCam[i].Dispose(); } groupBox8.Refresh();
I set the Visible of the TextBox to false to reduce flicker while disposing the controls.
这篇关于如何重绘动态添加的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!