Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
foreach (Object items in checkedListBox1.CheckedItems)
            {
                listBox1.Items.Add(items);

            }

C#
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;
            }

C#
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?
Posted
Updated 12-Jan-15 23:52pm
v2

1 solution

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 with
C#
groupBox8.Refresh()
which redraws the Control and all child Controls.


for clearing the GroupBox i would suggest

C#
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.
 
Share this answer
 
v3
Comments
[no name] 13-Jan-15 5:14am    
I have tried it but nothing happened.

foreach (TextBox fortx1 in arrCam)
{
fortx1.Clear();
}
groupBox8.Refresh();
[no name] 13-Jan-15 5:37am    
now it is saying: can't access disposed control textbox
Taucher Christoph 13-Jan-15 6:15am    
sorry my bad, you must use a for loop because you can't dispose it while you work with the foreach
[no name] 13-Jan-15 6:19am    
but the situation will remain the same,isn't it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900