How do I separate form code and put it into classes?? inheritance?

G

Guest

hey, how do I take form code and put it into separate classes? more
specifically,
I want to create like a panel class, and put buttons in it and give these
buttons click events. and all my form has to show is the empty panel. then
tell the panel to inherit from the panel class so it has all those buttons
and click events... that way I can make like 10 panel classes, and when I
want to display a panel, I just tell it to inherit from one of the classes.

I know that the base class is going to have to inherit from the UserControl
class, but how exactly do I do it? any help? thanks.
 
P

PokerMan

You'd make a control that inherits from panel.

Then you stick all your butons on and do your requirements and make some
events accessbile from outside so that when you use it they can hook to that
event and fire code based ont he clicks of the buttons.

Then when done, include that dll of it in any app and it will appear in the
designer. just drag and drop it into a form.

You don't get your form to inherit the panel itself tho.
 
K

Kevin Spencer

You don't really want to do this. Inheritance is a way of defining classes.
A class definition is not the same thing as a class instance. A Control in
an application is not a class, but an instance of a class. So, let's say you
have an empty Panel. You want to populate it with some Controls. If you
create an instance of some Control that inherits that Panel class, you are
creating a new Control, rather than populating the Panel that existed in the
first place. As to what you *should* do, I can't say, because you haven't
identified your requirements clearly.

--
HTH,

Kevin Spencer
Microsoft MVP

Help test our new betas,
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
G

Guest

I just want to have as little code in the presentation layer as possible. how
could this be accomplished then? any articles or tips ? thanks.
 
J

j1mb0jay

roger_27 said:
hey, how do I take form code and put it into separate classes? more
specifically,
I want to create like a panel class, and put buttons in it and give
these buttons click events. and all my form has to show is the empty
panel. then tell the panel to inherit from the panel class so it has
all those buttons and click events... that way I can make like 10
panel classes, and when I want to display a panel, I just tell it to
inherit from one of the classes.

I know that the base class is going to have to inherit from the
UserControl class, but how exactly do I do it? any help? thanks.


create a standard windows form ...
public partial class Meety : Form

override the events you require for example
private void Meety_MouseDoubleClick(object sender, MouseEventArgs e)
{}
private void MeetyForm_MouseMove(object sender, MouseEventArgs e)
{}

save the project and then create a new "inherited form"

public partial class Login : MeetyForm (assuming both forms are in the same
namespace).

anything that is on the super form will appear on the child .... buttons
events methods ......

Have fun
 
G

Guest

awesome!! I did it, but I used inherited controls. instead of using an
inherited form. I used panels.

so now I have one form, and I just use

this.Controls.Add(pnlUsers);
pnlUsers.BringtoFront();

to show a panel, and

this.Controls.Remove(pnlUsers);

to hide the panel and show a different one.

thanks!
 

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