473,387 Members | 1,492 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,387 software developers and data experts.

C# question: How do you delete a Grid object

I'm building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let's say I'm just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don't know how to delete the
objects. How do you delete a Grid object? I'd prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.

Any ideas?

Nov 10 '07 #1
11 2832
Turning the visible switch will not cause the GC to collect the object.
It will just set the visibility of the control to false, as the reference to
the object will still be held by the parent object.

I also don't know how you are using a Panel, and not a derived class, as
Panel is marked abstract.

Regardless, any class that derives from Panel that you are using has a
Children property which has all the child controls that are on the panel.
Pass your Grid to the Remove method on the UIElementCollection that is
exposed through the Children property and it will be removed.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

<do********@hotmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
I'm building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let's say I'm just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don't know how to delete the
objects. How do you delete a Grid object? I'd prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.

Any ideas?
Nov 10 '07 #2
Liz

<do********@hotmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
I'm building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let's say I'm just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don't know how to delete the
objects.
How do you delete a Grid object? I'd prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.
You always have to wait for the garbage collector to come around; you can't
deallocate memory explicitly in .NET .. in any case, making an object not
visible doesn't mark it for garbage collection, it makes it not visible

Can't say I've ever done what you're doing but can't you just grid.Dispose()
it?

Nov 10 '07 #3
Liz wrote:
<do********@hotmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
>I'm building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let's say I'm just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don't know how to delete the
objects.
>How do you delete a Grid object? I'd prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.

You always have to wait for the garbage collector to come around;
That's not entirely true. You can use stackalloc to allocate memory on
the stack (away from the garbage collector).

It's good if you frequently need to allocate moderately large arrays of
objects. Garbage collection hurts in that situation.

Alun Harford
Nov 10 '07 #4
Liz

"Alun Harford" <de*****@alunharford.co.ukwrote in message
news:OD**************@TK2MSFTNGP03.phx.gbl...
Liz wrote:
>>
You always have to wait for the garbage collector to come around;
That's not entirely true. You can use stackalloc to allocate memory on the
stack (away from the garbage collector).

It's good if you frequently need to allocate moderately large arrays of
objects. Garbage collection hurts in that situation.
glad you mentioned stackalloc, Alun ... it is useful and too often ignored
(or so it seems); so I'll rephrase and say you always have to wait for the
GC where the GC is capable of doing anything ... certainly the OP has no
choice but to wait for it with respect to his dynamically instantiated grids

Nov 11 '07 #5
I kind of don't see the point of mentioning this, as you can't allocate
pointers to reference type arrays using stackalloc. You can only use value
types.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Liz" <li*@tiredofspam.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>
"Alun Harford" <de*****@alunharford.co.ukwrote in message
news:OD**************@TK2MSFTNGP03.phx.gbl...
>Liz wrote:
>>>
You always have to wait for the garbage collector to come around;
>That's not entirely true. You can use stackalloc to allocate memory on
the stack (away from the garbage collector).

It's good if you frequently need to allocate moderately large arrays of
objects. Garbage collection hurts in that situation.

glad you mentioned stackalloc, Alun ... it is useful and too often ignored
(or so it seems); so I'll rephrase and say you always have to wait for
the GC where the GC is capable of doing anything ... certainly the OP has
no choice but to wait for it with respect to his dynamically instantiated
grids


Nov 11 '07 #6
Controls in WPF do not implement IDisposable, so this isn't a feasible
solution.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Liz" <li*@tiredofspam.comwrote in message
news:eY*************@TK2MSFTNGP06.phx.gbl...
>
<do********@hotmail.comwrote in message
news:11**********************@19g2000hsx.googlegro ups.com...
>I'm building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let's say I'm just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don't know how to delete the
objects.
>How do you delete a Grid object? I'd prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.

You always have to wait for the garbage collector to come around; you
can't deallocate memory explicitly in .NET .. in any case, making an
object not visible doesn't mark it for garbage collection, it makes it not
visible

Can't say I've ever done what you're doing but can't you just
grid.Dispose() it?
Nov 11 '07 #7
Nicholas Paldino [.NET/C# MVP] wrote:
I kind of don't see the point of mentioning this, as you can't
allocate pointers to reference type arrays using stackalloc. You can
only use value types.
I just didn't want people to think .NET/C# is a inherrently slow. In
situations where GC is very bad, you *can* avoid it (if you really must).

Alun Harford
Nov 11 '07 #8
Alun,

That's the thing, unless you are going to use value types for
everything, then no, you are not going to be able to avoid GC. Even in
IDisposable implementations, you might be able to eliminate the chance of
^finalization^ occuring, the object itself is still subject to garbage
collection. You can't avoid it for reference types.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Alun Harford" <de*****@alunharford.co.ukwrote in message
news:eu**************@TK2MSFTNGP04.phx.gbl...
Nicholas Paldino [.NET/C# MVP] wrote:
> I kind of don't see the point of mentioning this, as you can't
allocate pointers to reference type arrays using stackalloc. You can
only use value types.

I just didn't want people to think .NET/C# is a inherrently slow. In
situations where GC is very bad, you *can* avoid it (if you really must).

Alun Harford
Nov 11 '07 #9
Liz

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:ub**************@TK2MSFTNGP06.phx.gbl...
Controls in WPF do not implement IDisposable, so this isn't a feasible
solution.

didn't know that ... so what do we do in WPF? grid = null; ??
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Liz" <li*@tiredofspam.comwrote in message
news:eY*************@TK2MSFTNGP06.phx.gbl...
>>
<do********@hotmail.comwrote in message
news:11**********************@19g2000hsx.googlegr oups.com...
>>I'm building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let's say I'm just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don't know how to delete the
objects.
>>How do you delete a Grid object? I'd prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.

You always have to wait for the garbage collector to come around; you
can't deallocate memory explicitly in .NET .. in any case, making an
object not visible doesn't mark it for garbage collection, it makes it
not visible

Can't say I've ever done what you're doing but can't you just
grid.Dispose() it?

Nov 11 '07 #10
On Nov 10, 2:22 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
Turning the visible switch will not cause the GC to collect the object.
It will just set the visibility of the control to false, as the reference to
the object will still be held by the parent object.

I also don't know how you are using a Panel, and not a derived class, as
Panel is marked abstract.

Regardless, any class that derives from Panel that you are using has a
Children property which has all the child controls that are on the panel.
Pass your Grid to the Remove method on the UIElementCollection that is
exposed through the Children property and it will be removed.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
Thanks,

<parent Control>.Children.Remove(UIElement) did what I wanted.

Regards,
D

Nov 11 '07 #11
See my original response for the answer.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Liz" <li*@tiredofspam.comwrote in message
news:uA*************@TK2MSFTNGP06.phx.gbl...
>
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:ub**************@TK2MSFTNGP06.phx.gbl...
>Controls in WPF do not implement IDisposable, so this isn't a feasible
solution.


didn't know that ... so what do we do in WPF? grid = null; ??
>--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Liz" <li*@tiredofspam.comwrote in message
news:eY*************@TK2MSFTNGP06.phx.gbl...
>>>
<do********@hotmail.comwrote in message
news:11**********************@19g2000hsx.googleg roups.com...

I'm building an application (WPF) and I have a Panel in the
application which will be populated dynamically with objects of the
type Grid, TextBox, etc. For simplicity, let's say I'm just adding and
deleting Grid objects. I can add the objects easily by creating Grid
objects. The problem I have is that I don't know how to delete the
objects.

How do you delete a Grid object? I'd prefer to not have to
turn the "visible" switch off and wait for the garbage collector to
come around.

You always have to wait for the garbage collector to come around; you
can't deallocate memory explicitly in .NET .. in any case, making an
object not visible doesn't mark it for garbage collection, it makes it
not visible

Can't say I've ever done what you're doing but can't you just
grid.Dispose() it?

Nov 11 '07 #12

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

Similar topics

4
by: Stephen | last post by:
I have got an event below to remove items from an arraylist and then to rebind the arraylist to the datagrid subsequently deleting the appropriate row. My problem is that my code makes sense and I...
11
by: enki | last post by:
I am writing a game and I am having trouble with moving the character on the map. Here is what I have right now. It involves win32 programming but that not my problem. I would like some...
3
by: simon | last post by:
I have one question. DataGrid control is performance cost.Use any other control if possible. That is by the book. But I need sorting and paging on all my pages. So, is there any better...
4
by: z. f. | last post by:
Hi, i stated that this is an advanced question because i have a post from few days ago that i received answers to with suggestions that looked good but did not work, so please if you post a...
6
by: Paul | last post by:
Hi I have 2 data grids and several controls on a web page. The grids will vary in size, just wondering if the lower grid could be covered by part of the upper grid depending on its size or is there...
3
by: vcornjamb | last post by:
Hello, I am developing a web form that contains some buttons and a data grid which has as its last column link buttons that will delete the data associated with that row. Everything works fine,...
2
by: Frank | last post by:
Hello, I am developing with VS.Net 2003 on the asp 1.1 platform. I am a few days new to using a datagrid. I found a nice tutorial and had no problems using an editable datagrid with textboxes...
6
by: Terry Holland | last post by:
I have an asp page that contains a user control. This control is a panel containing a number of link buttons that get displayed if certain conditions in the db are met and these conditions can...
1
by: Kiran | last post by:
Hello All, I created a grid, where I register events every time the user changes an existing value inside the grid control. Right now, I am using the event: EVT_GRID_CELL_CHANGE. However, I...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.