473,804 Members | 3,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Controls left behind on a resize

As part of my app I am programatically adding some Label controls to a form
at runtime. The code for these is part of a function that is overrriding the
Paint event of the form. The other stuff that is drawn in the same function
works fine when I resize the screen (some lines and so on) but the labels
remain.

I tried deleting them by putting the following in the forms' resize event
(the labels are the only "controls" on the form):

for (int c = 0; c < this.Controls.C ount; c++)
{
this.Controls.R emoveAt(c);
}

and it sort of works, but I still get some labels left behind - not the
originals but one or two stragglers from when it's resized.
I then tried a putting a "this.refresh() " in there too, but the form just
went into a mad loop when I tried to refresh it and I had to kill the
process :)

Any suggestions as to where I'm going wrong?
James.
Sep 25 '06 #1
2 1841

james wrote:
As part of my app I am programatically adding some Label controls to a form
at runtime. The code for these is part of a function that is overrriding the
Paint event of the form. The other stuff that is drawn in the same function
works fine when I resize the screen (some lines and so on) but the labels
remain.

I tried deleting them by putting the following in the forms' resize event
(the labels are the only "controls" on the form):

for (int c = 0; c < this.Controls.C ount; c++)
{
this.Controls.R emoveAt(c);
}

and it sort of works, but I still get some labels left behind - not the
originals but one or two stragglers from when it's resized.
I then tried a putting a "this.refresh() " in there too, but the form just
went into a mad loop when I tried to refresh it and I had to kill the
process :)

Any suggestions as to where I'm going wrong?
Well, I'm not sure that I understand exactly what it is you're seeing,
but the code you posted is wrong. Consider the case in which you have
three labels on your form. Look at what the loop will do:

c = 0
this.Controls.R emoveAt(0); // Removes first label.
// Second label is now at index 0, third label is at index 1.
c = 1
// Loop repeats because this.Controls.C ount is now 2.
this.Controls.R emoveAt(1); // Removes what was third label.
// What was second label is still at index 0.
c = 2
// Loop terminates because Controls.Count is now 1,
// leaving what was originally the second label as the only
// control left in the collection.

In cases like this, you need to loop _backward_ through the controls,
so that the control you remove doesn't affect the "numbering" of the
other controls:

for (int c = this.Controls.C ount - 1; c >= 0; c--)
{
this.Controls.R emoveAt(c);
}

will clear all of the controls from the collection.

Sep 25 '06 #2

"Bruce Wood" <br*******@cana da.comwrote in message
news:11******** *************@i 42g2000cwa.goog legroups.com...
In cases like this, you need to loop _backward_ through the controls,
so that the control you remove doesn't affect the "numbering" of the
other controls:

for (int c = this.Controls.C ount - 1; c >= 0; c--)
{
this.Controls.R emoveAt(c);
}

will clear all of the controls from the collection.
Many thanks Bruce, the explanation made complete sense, and the code snippet
works a treat.
James.
Sep 26 '06 #3

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

Similar topics

7
2336
by: lauren quantrell | last post by:
A while back I got a requirement for the client to be able to adjust the relative heights of two subforms by click-dragging the mouse and I came up with a kludge solution using a border control between the two subforms. But I put my mind to this again recently and came up with a solution (code below) that works well with multiple subforms to size both the heights and widths of the sunforms relative to each other, and it uses no additional...
2
1669
by: ichor | last post by:
hi i am developing a c# app and i want that when i size the form the controls should also be automatically sized. thanx
11
2145
by: VJ | last post by:
I am looking to have 2 Tab Controls on a Windows Forms.. One with Tabs on Top and another with Tabs on the side... I want them both to occupy the entire area of the form, but I need the Tab Headers to be visible for both of them .. at all times... How do I accomplish this? Also can One Tab control have Tab Headers on Top and Side.. at the same time?..That might solve this problem easily :-) Thanks Vijay
3
2798
by: Mamatha | last post by:
Hi i have two listview controls on my form one besides another.When i resized the form ,i want to resize those controls automatically along with the form.Where can i set those properties in VB.NET or how can i do this... If anyone knows please letme know,thanks in advance. Mamatha
2
5007
by: Alan T | last post by:
I would like to know how do I align the controls on th Win form: Align TreeView on the left Align ListView on the client area Align datagridview to the right ?
1
2087
by: namewitheldbyrequest | last post by:
When I create a web form in VS 2005 I have a problem. I created a MasterPage with a ContentPlaceHolder object on it. Then I created a detail page that refrenced the Master Page. When I drag controls onto the ControlPlaceHolder on the Detail page, the controls end up at the upper left corner of the page and I have to drag them down onto the ControlPlaceHolder. The controls are coded in the HTML as position = absolute and I don't think...
0
1019
by: nicomp | last post by:
When I create a web form in VS 2005 I have a problem. I created a MasterPage with a ContentPlaceHolder object on it. Then I created a detail page that refrenced the Master Page. When I drag controls onto the ControlPlaceHolder on the Detail page, the controls end up at the upper left corner of the page and I have to drag them down onto the ControlPlaceHolder. The controls are coded in the HTML as position = absolute and I don't think...
15
6539
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt accept other controls. The control i drag drop on it becomes the child of my custom control's parent form and not the child of my custom control. Then i added this line "" before my custom control class (i dont know what this line does). Now
1
2035
by: Martin | last post by:
Hi all ! I use Visual Studio .NET 2005 SP1 (+ Updates for Vista) on Windows Vista. I have a strange problem in a WinForm application. In a UserControl, which inherit another UserControl (simply contains a SplitterPanel), I've added controls (most of them are TextBox with the property Anchors set to Left and Right) to Panel2. All theses controls fits in the UserControl and when I resize it, all the controls have the correct behavior....
0
10588
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10324
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
10085
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
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
7623
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
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5662
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.