OOPS Newbie - Removing objects from form controls

A

AAJ

Hi all

I'm new to all this object programming, and I'm after a bit of advice on a
specific problem. I wonder if anyone can help?

I have a user control and on it are some text boxes bound to a database .
At a particular time I want to add it to a forms control, have the user work
on it, and then when the user is done remove it and clear up the memory.

Is there a recognised way/pattern of doing this type of thing

i.e. if I have a form with the following function

private void test()
{
UCQuestion Question1 = new UCQuestion(1,"Sample Question Test");
this.Controls.Add(Question1);
}

When the question has been answered I want to remove Question1 from the form
collection (and from memory) i.e. when the user closes the control.

I can put a button on the user control to generate an event, but back in the
form, Question1 is only within scope in the test function, so the event
handler that I write cannot see 'Question1'. I know I could make UCQuestion
Question1 global within the class, but I will have many dynamic Questions,
and so I need to be able to create and remove them dynamically.

So I'm wondering if Is it possible to use the event sender or args to find
the Specific object within the form controls and remove it.

I'm sure that this must be a faily common thing to want to do, so I'm
wondering if there is a best practice out there.

many thanks in advance

Andy
 
G

Guest

I suggest creating a QuestionCollection inheriting
System.Collections.CollectionBase. As you generate questions, add them to
the QuestionCollection. This will allow you to easily access or find your
control.

HTH
 
B

Bruce Wood

Do you want only one question at a time visible, or do you need
multiple questions visible at any one time, appearing and disappearing
as each one is answered?

If you need only one question at a time, then just have a private
member in the class for "current question" and store it there.

Regardless of your needs, remember that a UserControl is best designed
as a self-contained entity, a "black box" if you will. If I find out
more about what you're doing, I might be able to help you with the
design.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top