473,399 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Dynamically removing Lables Windows forms

Hi,

I'm trying to dynamically remove labels from a windows form in csharp. I
have a foreach loop similar to :

foreach(Control c in this.Controls)
{
c.Dispose();
}

The code does not find any labels but works fine for othet controls. I'm
clearing the entire form (excluding menus) so there may be a better way to do
this. I do not know the name(s) of the lables as they are created dynamically.

Thanks in advance, O
Mar 12 '07 #1
4 7334
Hi Olan,

I managed to hide them, change the text on them, but not to dispose
them.

// works fine
MessageBox.Show(this.Controls.Count.ToString());

foreach (Control c in this.Controls)
{
c.Hide();
c.Update();
}

this.Update();

// changed text
foreach (Control c in this.Controls)
{
c.Text = "abodaboo";
c.Update();
}

// only removed a button
foreach (Control c in this.Controls)
{
c.Dispose();
}

this.Update();

----

HTH,
Per

Home: http://www.pererikstrandberg.se/blog/ .
Optimization in .NET: http://tomopt.com/tomnet/ .

On 12 Mar, 16:21, Olan <O...@discussions.microsoft.comwrote:
Hi,

I'm trying to dynamically remove labels from a windows form in csharp. I
have a foreach loop similar to :

foreach(Control c in this.Controls)
{
c.Dispose();

}

The code does not find any labels but works fine for othet controls. I'm
clearing the entire form (excluding menus) so there may be a better way to do
this. I do not know the name(s) of the lables as they are created dynamically.

Thanks in advance, O

Mar 12 '07 #2
This only hides the labels

foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(Label))
{
// c.Dispose();
// c.Text = "abodaboo";
c.Hide();
c.Update();
}
}

this.Update();
----

HTH,
Per

Home: http://www.pererikstrandberg.se/blog/ .
Optimization in .NET: http://tomopt.com/tomnet/ .
On 12 Mar, 16:21, Olan <O...@discussions.microsoft.comwrote:
Hi,

I'm trying to dynamically remove labels from a windows form in csharp. I
have a foreach loop similar to :

foreach(Control c in this.Controls)
{
c.Dispose();

}

The code does not find any labels but works fine for othet controls. I'm
clearing the entire form (excluding menus) so there may be a better way to do
this. I do not know the name(s) of the lables as they are created dynamically.

Thanks in advance, O

Mar 12 '07 #3
This only hides the labels

foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(Label))
{
// c.Dispose();
// c.Text = "abodaboo";
c.Hide();
c.Update();
}
}

this.Update();
----

HTH,
Per

Home: http://www.pererikstrandberg.se/blog/ .
Optimization in .NET: http://tomopt.com/tomnet/ .
On 12 Mar, 16:21, Olan <O...@discussions.microsoft.comwrote:
Hi,

I'm trying to dynamically remove labels from a windows form in csharp. I
have a foreach loop similar to :

foreach(Control c in this.Controls)
{
c.Dispose();

}

The code does not find any labels but works fine for othet controls. I'm
clearing the entire form (excluding menus) so there may be a better way to do
this. I do not know the name(s) of the lables as they are created dynamically.

Thanks in advance, O

Mar 12 '07 #4
On Mar 12, 8:21 am, Olan <O...@discussions.microsoft.comwrote:
Hi,

I'm trying to dynamically remove labels from a windows form in csharp. I
have a foreach loop similar to :

foreach(Control c in this.Controls)
{
c.Dispose();

}

The code does not find any labels but works fine for othet controls. I'm
clearing the entire form (excluding menus) so there may be a better way to do
this. I do not know the name(s) of the lables as they are created dynamically.
Ignoring for a moment that I have no idea why anyone would want to do
what you want to do... and therefore suspect that your original
problem (whatever it is) is probably best solved in some other way....

List<ControlcontrolsToRemove = new List<Control>();
foreach (Control c in this.Controls)
{
if (c is Label)
{
controlsToRemove.Add(c);
}
}
foreach (Control remove in controlsToRemove)
{
this.Controls.Remove(remove);
remove.Dispose();
}

Mar 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
0
by: sameer mowade via .NET 247 | last post by:
Hello All, I have problem while dynamically removing row from the Datagrid which i have added dynamically as shown in the following code snippet. The problem is that while removing dynamically...
2
by: D Hass | last post by:
Radio Buttons are added to my form During the Load event, the quantity determined by User input. They are placed in pairs on groupboxes with code like this: (edited a bit for brevity) Public...
7
by: mef526 | last post by:
I would like to reference a dynamically created control and I know the name. I would like to use the following: Dim strName as String = "txtControl1" ' This is the ".Name" used when textbox was...
1
by: Jerry | last post by:
Hi again, I have two forms as controls. Both are on a main form and with a treeview I switch back and forth between the two. On the first form is a calculation. The result of this calculation...
1
by: npaulus | last post by:
Hi, I am trying to dynamically add user controls on to my web form but for some reason my form isnt displaying the user control. form1.cs: using System; using System.Drawing; using...
0
by: Mike | last post by:
Hi, I have a collection object bound to a data grid, after I remove an item from the collection, the minute I click on the datagrid I get an error saying the specified argument was out of the...
0
by: khalid galal | last post by:
Hi, i am having a problem with removing event handlers, it is when creating nested event handlers (an event handler raising another event handler) where a part of the code is private void...
5
by: Anthony P. | last post by:
Hello Everyone, I'm totally lost and I hope someone can help me out or point me to something that can. Here's the deal: I have an application that will implement a 'plugin' archetecture of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.