inherits class.

  • Thread starter Thread starter Mr. X.
  • Start date Start date
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 :)
 
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?
 
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 :)
 
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
 
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).
 
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 :)
 
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 :)
 
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.
 
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.
 
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 :)
 
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.
 
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 :)
 
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/
 
Back
Top