inherits class.

M

Mr. X.

Hello.
I want to create a new class (VB 2008).

What I did :
add new class.
and wrote :

Public Class MenuButton
Inherits Panel
....

The code isn't compiled :
Error 1 Base class 'System.Windows.Forms.Panel' specified for class
'MenuButton' cannot be different from the base class
'System.Windows.Forms.UserControl' of one of its other partial types.

I need little sample code, so I can go on ..., please.

Thanks :)
 
A

Armin Zingler

Am 19.03.2010 11:54, schrieb Mr. X.:
Hello.
I want to create a new class (VB 2008).

What I did :
add new class.
and wrote :

Public Class MenuButton
Inherits Panel
....

The code isn't compiled :
Error 1 Base class 'System.Windows.Forms.Panel' specified for class
'MenuButton' cannot be different from the base class
'System.Windows.Forms.UserControl' of one of its other partial types.

I need little sample code, so I can go on ..., please.

Thanks :)

You've (also) created a UserControl with the name "MenuButton", didn't you?
 
M

Mr. X.

Sorry.
What I did : add new item - > user control.
On the new class/control (menuButton.vb) I wrote :
Public Class MenuButton
Inherits Panel

That's all.

Thanks :)
 
F

Family Tree Mike

Mr. X. said:
Sorry.
What I did : add new item - > user control.
On the new class/control (menuButton.vb) I wrote :
Public Class MenuButton
Inherits Panel

That's all.

Thanks :)



.

In the solution explorer, there is a button that says "Show All Files" when
you hover over it. Your user control has a second .designer file that is
associated with your code file. You changed the inheritance in one file, but
not the other, thus the confusion.

It is actually a lot easier in the long run to not do what you did. You
should just add a class as normal, so you don't get the designer file. Then
you don't run into this issue when you add the inherit statement.

Mike
 
A

Armin Zingler

Am 19.03.2010 12:42, schrieb Mr. X.:
Sorry.
What I did : add new item - > user control.
On the new class/control (menuButton.vb) I wrote :
Public Class MenuButton
Inherits Panel

That's all.

Do you want to derive your class from Usercontrol or from Panel?

If you've created a Usercontrol as you've desribed, you can
not inherit from Panel.

If you want to inherit from Panel, you must not create a Usercontrol
as described. Instead just add a new class (add new item -> class).
 
M

Mr. X.

Yes.
But when I create a class, and inherits Panel - I cannot see it as a
component, which I can put buttons, etc.
How can I make my class, that is inherited the Panel, and it could be seen,
with property editor, and component editor, and put some elements on it,
like the userControl ?

Thanks :)
 
M

Mr. X.

OK.
Now I see that when I declare my new class as a class (and not userControl)
Public Class MenuButton
Inherits Panel
....

I can right click, and choose : "View designer".

A little problem is, when I put a button component on it (from toolbox) - I
cannot control the behavior of the button (size, color, etc.)

Is there anything else I should declare ?

Thanks :)
 
A

Armin Zingler

Am 19.03.2010 17:15, schrieb Mr. X.:
Yes.
But when I create a class, and inherits Panel - I cannot see it as a
component, which I can put buttons, etc.
How can I make my class, that is inherited the Panel, and it could be seen,
with property editor, and component editor, and put some elements on it,
like the userControl ?

The Visual Studio IDE has designers for Forms and UserControls, and for Components.
A Panel is none of these. Therefore, you must create a Usercontrol if you
want to use a designer. Is there something special about a Panel that a Usercontrol
can not do?

Panel inherits from ScrollableControl.
Usercontrol inherits from ContainerControl that again inherits from ScrollableControl.
 
T

Tom Shelton

Yes.
But when I create a class, and inherits Panel - I cannot see it as a
component, which I can put buttons, etc.
How can I make my class, that is inherited the Panel, and it could be seen,
with property editor, and component editor, and put some elements on it,
like the userControl ?

Thanks :)

Mr. X - it sounds like you are tryign to create a custom container control?
Basically, a user control that when you add it to a form at design time you
can add stuff to it?

Step 1:

Add a new user control to your project (or library)

Step 2:
Add the following attribute to your user control:

Option Explicit On
Option Strict On

Imports System.ComponentModel
Imports System.ComponentModel.Design

<Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", GetType(IDesigner))> _
Public Class UserControl1


End Class

Step 3:
Compile

Step 4: drag the control to your form - you can now add other controls to it
at design time.
 
M

Mr. X.

OK.
Thanks.

I put the code you gave me,
and with or without it step 4 doesn't work :
Maybe I need a code sample, of how to create simple user-control and put it
on my form.

I saw the user-control of mine on the toolbox, and just did : drag & drop.
What I get is an exception.
Too long, so I can give it on brief :
Failed to create component 'MenuButton'. The error message follows :
'System.MissingMethodException : Constructor on type 'myApp.MenuButton' not
found at
System.RuntimeType.CreateInstanceImp(BindingFlag ...) ...
at ...
at ...
at ...
at System.Drawing.Design.ToolboxItem.CreateComponentsCore(IDesigner Host
host, IDictionary defaultValues) at Sy ...

****
I cannot see the whole exception, but it seems I didn't declare the
constructor as I should do.
What I have written in code :
-------------------------------------
Public Sub New(ByVal aImageFile As String, ByVal aText As String)
InitializeComponent()
End sub

I need sample code, or tutorial to create a simple user-control, please.

Thanks :)
 
T

Tom Shelton

OK.
Thanks.

I put the code you gave me,
and with or without it step 4 doesn't work :

I copied and pasted that out of a working project. So, if something isn't
working then you have something else going on.
Maybe I need a code sample, of how to create simple user-control and put it
on my form.

You grab it in the toolbox and drop it on the form :)
I saw the user-control of mine on the toolbox, and just did : drag & drop.
What I get is an exception.
Too long, so I can give it on brief :
Failed to create component 'MenuButton'. The error message follows :
'System.MissingMethodException : Constructor on type 'myApp.MenuButton' not
found at
System.RuntimeType.CreateInstanceImp(BindingFlag ...) ...
at ...
at ...
at ...
at System.Drawing.Design.ToolboxItem.CreateComponentsCore(IDesigner Host
host, IDictionary defaultValues) at Sy ...

****
I cannot see the whole exception, but it seems I didn't declare the
constructor as I should do.
What I have written in code :
-------------------------------------
Public Sub New(ByVal aImageFile As String, ByVal aText As String)
InitializeComponent()
End sub

I need sample code, or tutorial to create a simple user-control, please.

To work in the designer, you control MUST have a default constructor...

Public Sub New()
InitializeComponent()
End Sub

You can add additional constructors, but they will not be called by the
designer. They are usually only useful for dynamically created controls.

Your probably better off not having a custom constructor and then making the
imagefile and text parameters correspond to properties that can be set in the
designer.
 
M

Mr. X.

Thanks a lot.

That's solved the problem (of the exception).

Now :
I want that the user-control, should be an external one, so I can load it to
some projects.
How can I do so ?
With some property editors, and component editors, etc....

Thanks :)
 
T

Tom Shelton

Thanks a lot.

That's solved the problem (of the exception).

Now :
I want that the user-control, should be an external one, so I can load it to
some projects.
How can I do so ?
With some property editors, and component editors, etc....

Thanks :)

Then you create a Windows Forms Control library project, and make it a public
control. The other stuff... Well, your asking a lot for one post :) But, I
do have an example of a component that does some of that on my blog:

http://tom-shelton.net/index.php/2009/10/07/creating-a-simple-hotkey-component-using-registerhotkey/
 

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