473,770 Members | 7,142 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7384
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...@discussio ns.microsoft.co mwrote:
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...@discussio ns.microsoft.co mwrote:
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...@discussio ns.microsoft.co mwrote:
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...@discussio ns.microsoft.co mwrote:
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<Controlcon trolsToRemove = new List<Control>() ;
foreach (Control c in this.Controls)
{
if (c is Label)
{
controlsToRemov e.Add(c);
}
}
foreach (Control remove in controlsToRemov e)
{
this.Controls.R emove(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
6554
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. When it completes, it can call a success, or a failure function. The names of these success, or failure functions will differ, and I'd like to know how I can pass the name of a function to my tool, and how my tool can call the function, using that...
0
2474
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 added row it also removes the row at the end along with the added row. Plz tell me if, I am missing any thing. Code </asp:datagrid>
2
4554
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 Function LayoutForm(ByVal intNumber As Integer) Dim intCount As Integer Panel1.Controls.Clear() For intCount = 1 To intGames
7
7586
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 dynamically created dim c as control = me.Controls(strName) Instead I have to do this:
1
1097
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 should also be shown on the second form when this is opened. The lables on the second form that need to be updated are on a tabcontol. I've tried gotfocus, activated, visiblechanged and god knows what else. The lables are changed only at the startup...
1
4080
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 System.Collections; using System.ComponentModel; using System.Windows.Forms;
0
1979
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 range of valid values. After I remove the element from the collection I clear the databindings from the data grid and rebind it but as far as I can tell the collection seems to be the right size but for some reason when I
0
1311
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 toolStripButton1_Click(object sender, EventArgs e) { this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown); } private void Form1_MouseDown(object sender, MouseEventArgs e) { this.MouseUp += new...
5
1036
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 sorts. The application requires the user to fill out various forms. Each agency that we deploy the application to will have different forms and those forms will change over time. If we build each form into the application through hard code,...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10004
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8886
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5313
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.