Here's a dumb one....

T

Tomas Vera

OK. This is something that is probably simple, but has me totally confused.

In all my previous work, whenver I needed on of my custom components I simply
dragged it onto my form and all was well.

But now, I need to develop a form that basically contains a panel into which I
need to dynamically insert one of several components.

On the main form, I have 3 buttons. When the user presses button 1, I need to
put Component1 into the panel on the main form. When The user presses button 2,
component2 will replace component1 on the main form. And so on for button3 (and
however many more buttons I will need).

In the response function for Button1_Click event, I tried this:

private Button1_Click(..)
{
component1 comp1 = new component1(); //<<== badline
mainPanel.Controls.Clear();
mainPanel.Controls.Add(comp1);
}



But, the line that tries to instantiate comp1 gives an "Cant create an instance
of abstract class 'component1'" (or something like that).

I tried removing the work "abstract" from the declaration of component1. And now
I'm able to compile, when the InitializeComponent function runs, the elements in
component1 are null and an exception is thrown when the system tries to assign
handlers for events.

Next, I've tried creating the component's elements in the constructor, and now
InitializeComponent is able to initialize the event handlers. But now, none of
the controls that I have placed on component1 show up on the main page.

How do I create a component (using the designer) that I can instantiate on the
fly?

So far, my only solution is to create the entire component on the fly, but
designing this way takes quite a long time. I need to start a project on Monday,
which has a four week deadline. This project will almost certainly require the
funcitonality that I have mentioned above, but I'm not sure how to get this
done.

As always, all help (ideas) are appreciated!

-tomas
 
B

Bjorn Abelli

"Tomas Vera" ...
On the main form, I have 3 buttons. When the user presses
button 1, I need to put Component1 into the panel on the
main form. When The user presses button 2, component2 will
replace component1 on the main form. And so on for button3 (and
however many more buttons I will need).

It sounds to me that you maybe would be better off with a TabControl?

In that way you can have all of your components on the form from start, but
visible only one "page" at the time.

// Bjorn A
 
T

Tomas Vera

Thanks for the suggestion.
However, I suspect that I will probably just do it the "hard way" and generate
the interface on-the-fly.
Not the ideal way, but I'm not sure how else to get this dynamic instantiation
of these controls one.

Again, thanks for the help!

-tomas
 
E

Eric Eggermann

Tomas Vera said:
OK. This is something that is probably simple, but has me totally confused.

In all my previous work, whenver I needed on of my custom components I simply
dragged it onto my form and all was well.

But now, I need to develop a form that basically contains a panel into which I
need to dynamically insert one of several components.

On the main form, I have 3 buttons. When the user presses button 1, I need to
put Component1 into the panel on the main form. When The user presses button 2,
component2 will replace component1 on the main form. And so on for button3 (and
however many more buttons I will need).

In the response function for Button1_Click event, I tried this:

private Button1_Click(..)
{
component1 comp1 = new component1(); //<<== badline
mainPanel.Controls.Clear();
mainPanel.Controls.Add(comp1);
}



But, the line that tries to instantiate comp1 gives an "Cant create an instance
of abstract class 'component1'" (or something like that).


Among other things, abstract means the class cannot be instantiated, so you
would not be able to create a component from that class. You can only derive
another component from it. So by removing the abstract keyword your first
problem taken care of.

Next you have several ways to go. As another poster already mentioned, you
could use a tab control. If that's no good, you could also go the way of
some wizard designers, and use panels, laid on top of one another, at the
same size and position and when you need to show a set of UI elements, set
that panel's Zorder to the top. This might be useful, especially if you know
which controls, and what their layout is at design time. This is probably
what you need if you know how many buttons you will have.

If it is truly necessary to create controls at runtime, you can do this as
well, but it's all on you, and the Windows forms designer can't help you
very much. It will even get in your way. You'll have to forget using the
designer if you go that route.

You could also instatiate all the controls on one panel, and set the ZOrder
on whatever control you like. Look up SetChildIndex method for the
ControlCollection class. Maybe that will help.

You haven't given your reasons for doing this, but I suspect that one panel
for each button you have is the easiest way avoid a tab control and still
make some use of the Forms designer.

HTH,
Eric
 
T

Tomas Vera

Thanks. The Z-order thing might be the way to go. We've worked with tabs and
panels in the past, but I've never messed with the Z-order of the panels as a
way of moving the user along a wizard.

As I mentioned, removing the 'Abstract' did, in fact, work (the code compiled).
But none of the elements were displayed at run-time. Still haven't figured that
one out.

I think that Dilbert would appreciate why I have to ask this question! <g>

In the end, we will probably do what we have been doing for a couple of years
now, and create specific forms to perform specific functions. Easier (but less
elegant) that way! In the end, it will work.

Thanks for all the suggestions.

-tomas
 
E

Eric Eggermann

Anyway, it seems you understand, but I'll still explain the zorder thing in
a bit more detail.
In the windows forms designer, you put your panels any way you like, put the
controls on each panel, then when the form opens, you write code to make all
the panels the same size, and decide which should be on top. Then in the
button click event, you set the zorder of the new panel with the
SetChildIndex property I think it's called. SetChild something or other
anyway. So, you can use the forms designer, and all you have to do yourself
is set the location and size of all the panels to the same rectangle when
the app is run. I've done this a couple times and it works a charm.

Eric
 

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

Similar Threads


Top