Click here to Skip to main content
15,887,083 members
Home / Discussions / C#
   

C#

 
GeneralRe: Remove Controls Pin
Zeyad Jalil4-Mar-14 0:41
professionalZeyad Jalil4-Mar-14 0:41 
AnswerRe: Remove Controls Pin
OriginalGriff3-Mar-14 23:54
mveOriginalGriff3-Mar-14 23:54 
GeneralRe: Remove Controls Pin
GuyThiebaut4-Mar-14 3:51
professionalGuyThiebaut4-Mar-14 3:51 
GeneralRe: Remove Controls Pin
OriginalGriff4-Mar-14 4:33
mveOriginalGriff4-Mar-14 4:33 
GeneralRe: Remove Controls Pin
BillWoodruff4-Mar-14 14:45
professionalBillWoodruff4-Mar-14 14:45 
AnswerRe: Remove Controls Pin
Dave Kreskowiak4-Mar-14 3:40
mveDave Kreskowiak4-Mar-14 3:40 
AnswerUse a curtain! Pin
Ravi Bhavnani4-Mar-14 4:41
professionalRavi Bhavnani4-Mar-14 4:41 
AnswerRe: Remove Controls Pin
BillWoodruff4-Mar-14 14:57
professionalBillWoodruff4-Mar-14 14:57 
At the risk of kicking a dead-horse, I'll throw my 2 cents in:

1. OriginalGriff's answer is spot-on. You can see my comment on his answer for an observation on the curious fact that using 'foreach on a Collection of Controls, and removing Controls in the loop, will not throw a run-time error, but will have "unexpected results."

2. Dave Kreskowiak's answer is spot-on: if all the Labels are in one Container Control, like a Panel, why not dispose of the Panel, or remove all the Labels by using PanelName.Controls.Clear();

The more interesting case is when you have a bunch of Controls that are visually "mixed-in" ... in the same containing object ... and you want to do something to some of those Controls, but not other Controls. In that case you can use a 'Tag to "mark" the Controls of interest for some future operation.

1. at design-time select all the Controls you want to tag: bring up the property browser, enter some text in the 'Tag property field.

2. assuming you tagged all 150 labels you want to, eventually, remove with "removable," and that all those labels are in a panel named 'panel1:

C#
// requires Linq
panel1.SuspendLayout();

var labels = panel1.Controls.OfType<Label>()
    .Where(lbl => lbl.Tag == "removable")
    .ToList();

for (int i = (labels.Count - 1); i >= 0; i--) panel1.Controls.Remove(labels[i]);

panel1.ResumeLayout();

“The best hope is that one of these days the Ground will get disgusted enough just to walk away ~ leaving people with nothing more to stand ON than what they have so bloody well stood FOR up to now.” Kenneth Patchen, Poet


modified 4-Mar-14 21:49pm.

QuestionHow to use the GoF in project? Pin
wmikas3-Mar-14 15:22
wmikas3-Mar-14 15:22 
AnswerRe: How to use the GoF in project? Pin
BillWoodruff3-Mar-14 17:35
professionalBillWoodruff3-Mar-14 17:35 
GeneralRe: How to use the GoF in project? Pin
wmikas4-Mar-14 1:10
wmikas4-Mar-14 1:10 
AnswerRe: How to use the GoF in project? Pin
dan!sh 3-Mar-14 19:40
professional dan!sh 3-Mar-14 19:40 
GeneralRe: How to use the GoF in project? Pin
harold aptroot3-Mar-14 22:35
harold aptroot3-Mar-14 22:35 
AnswerRe: How to use the GoF in project? Pin
gkrannich3-Mar-14 22:58
gkrannich3-Mar-14 22:58 
AnswerRe: How to use the GoF in project? Pin
Jubayer Ahmed4-Mar-14 0:44
professionalJubayer Ahmed4-Mar-14 0:44 
AnswerRe: How to use the GoF in project? Pin
BobJanova4-Mar-14 6:06
BobJanova4-Mar-14 6:06 
QuestionDll References and Dependencies Pin
MarkB1233-Mar-14 7:58
MarkB1233-Mar-14 7:58 
AnswerRe: Dll References and Dependencies Pin
Richard Andrew x643-Mar-14 8:38
professionalRichard Andrew x643-Mar-14 8:38 
GeneralRe: Dll References and Dependencies Pin
MarkB1233-Mar-14 8:57
MarkB1233-Mar-14 8:57 
AnswerRe: Dll References and Dependencies Pin
Pete O'Hanlon3-Mar-14 10:09
mvePete O'Hanlon3-Mar-14 10:09 
GeneralRe: Dll References and Dependencies Pin
Richard Andrew x643-Mar-14 13:16
professionalRichard Andrew x643-Mar-14 13:16 
AnswerRe: Dll References and Dependencies Pin
Matt T Heffron3-Mar-14 13:59
professionalMatt T Heffron3-Mar-14 13:59 
AnswerRe: Dll References and Dependencies Pin
BobJanova4-Mar-14 5:27
BobJanova4-Mar-14 5:27 
GeneralRe: Dll References and Dependencies Pin
MarkB1234-Mar-14 5:34
MarkB1234-Mar-14 5:34 
Questiondatatype in DataGridview Pin
naylynn3-Mar-14 4:29
naylynn3-Mar-14 4:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.