Creating a control in Class1 ... and showing it in Class2 (winform)

  • Thread starter Guillaume BRAUX
  • Start date
G

Guillaume BRAUX

Hello,

What I want to do is to add a userControl to a form class witch is a
different class from the one the button is generated.
For example, I want to instanciate a label in "class1" and add it ("show
it") on a WinForm situated in "class2" (without having to add code to class2
!)

The problem is that I need to instanciate "class2" from "class1" to be able
to do a class2.controls.add(myLabel) in class1..
Instaciating class2 means that a new form will be created, but i want my
control to be added to the current running form.

Thanks for your help,

Guill
 
G

Guillaume BRAUX

Perhaps an inherited form/control?

Already tried ... My control class inherit from my form class, but il does
not help ... I still have to instaciate the form to be able to do a control
add (class2.controls.add(myLabel)) ...

Thanks
 
B

Bruce Wood

Guillaume said:
Already tried ... My control class inherit from my form class, but il does
not help ... I still have to instaciate the form to be able to do a control
add (class2.controls.add(myLabel)) ...

Thanks

What are you trying to do? I see questions like this posted to
newsgroups from time to time, in which the OP asks how to achieve some
effect, then after a half-dozen posts it turns out that the problem is
to set the label text at run-time, or make a label disappear at
run-time, or something like that, all of which is very easy to do using
other means.

So... could you step back a bit and tell us what you're trying to
achieve? Why do you want to add a label at runtime? What is that going
to do for you?

Perhaps there's another, much easier way to do what you want to do.
 
G

Guillaume BRAUX

Here is exactly what I want to do in my app :

The main problem is that my app will work with a totaly dynamic GUI, so
buttons, label and so on have to be created at runtime.
When I click on a runtime created button, I want to load a UserControl
not yet instaciated and put it on a TabPage also created at runtime

How can I achieve this goal ?

Thanks,

Guillaume
 
B

Bruce Wood

So, you have a dynamic GUI that must be built at runtime. It sounds
like you have one form being built, and another form in which the user
is making choices that builds things into the first form. Am I right?

I'm going to make an assumption. I'm going to assume that the form
you're building is dynamic, but not completely arbitrary. By that I
mean that the user isn't saying things like, "Place a radio button
here." Rather, the user is making some choice like "I want a topping on
my sundae" and that causes certain controls to be placed on the second
form. Probably nothing so trivial, but you get the idea.

The other possibility is that you're writing a completely
general-purpose GUI builder, in which case the following advice doesn't
apply.

I would write methods and properties on the first form (the one being
dynamically built) that are based _not_ on what controls get added, but
on _why_ they're being added. So, to continue my silly example, you
might have a method on Form1 like this:

public void AllowToppingSelection(ArrayList toppings)
{ ... }

This method the contains the code to place the correct radio buttons on
the form to select amongst the indicated sundae toppings.

The idea here is that the client form (the one asking to add controls
to the form being built) doesn't have to know how the dynamic form is
offering that selection to the user. The client is concerned with
client-side things: ice cream flavours, toppings, cone type, cone
size... stuff like that. It builds the dynamic form by telling it
_what_ the user wants, not _how_ to add the controls. This allows the
client to work in a higher level of abstraction, and leaves the
complexities of how to place controls entirely within the dynamic
form's code.

As I said, the alternative is that you're building a completely generic
GUI builder, where there is no higher level of abstraction. That's
beyond my expertise... perhaps someone else here can comment on how to
do that, if that's your case.
 

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