Need an alternative to multiple inheritance

  • Thread starter Thread starter **Developer**
  • Start date Start date
D

**Developer**

I have a control called MyPanel that inherits from Panel.
Also a control called MyRTB that inherits from RichTextBox.

They are used on a form as follows:

MyPanel in on the form and MyRTF is placed in MyPanel.

I do this so that the properties of MyRTF and of MyPanel are available to
the form.

What I'd rather do is have the MyPanel control contain MyRTF so only one
control need be used on the form.

Of course if I just do that the properties of MyRTF will not be available to
the form unless I make MyRTF public and use something like
MyPanel1.MyRTF1.Text

It not nice the way I have it nor is the alternative mentioned above much
better.

Seems I've read that multiple inheritance is not needed because Interfaces
can be used instead, but I don't know how.

Is there a neat way to do what I need to do?


Thanks in advance
 
Create a user control.Add MyPanel to the user control, add MyRTF inside the
panel.
Create properties inside this user control to expose whatever properties
need to be exposed for both the textbox and the panel.

Now you can use the user control on forms.
 
Thanks for the reply.
The number of properties is very large. I was hoping there might be another
way.

Thanks
 
Marina said:
Create a user control.Add MyPanel to the user control, add MyRTF inside
the panel.
Create properties inside this user control to expose whatever properties
need to be exposed for both the textbox and the panel.

Now you can use the user control on forms.

Mhm... To do that, it's not even necessary to create a usercontrol. It's
sufficient to create a class which inherits from panel and adds a
richtextbox control to its 'Controls' collection. Then you can extend the
inherited panel by additional properties.
 
Yes, you are right. I tend to think in terms of completely boxing up the
whole thing. So that if ever you decide you don't want a panel, but you want
a tab control, you just swap out the control in the user control, and
re-implement all the properties, and know that everything is still valid.
Whereas with a panel, panel specific properties may have ended up being set
directly on the consuming form.

But you are right, in most cases that wouldn't matter, and inheriting from
panel works fine.
 
As Herfried suggested, you can inherit from Panel.

You can also expose the panel and richtextbox as properties, so that you can
set properties directly on the objects.
 
Thanks for the replies.

BTW I misspoke, the first two sentences of my post should have used "class"
instead of "control"

I use the IDE to place the MyPanel and then add MyRTF
Me.ControlPanel1.Controls.Add(Me.ControlRichTextBox1)

I was hoping the panel could expose a RichTextBox interface or something.



thanks
 
**Developer** said:
BTW I misspoke, the first two sentences of my post should have used
"class" instead of "control"

I use the IDE to place the MyPanel and then add MyRTF
Me.ControlPanel1.Controls.Add(Me.ControlRichTextBox1)

I was hoping the panel could expose a RichTextBox interface or something.

No, that's not possible. You can either make a reference to the richtextbox
control available by your control or reimplement its public members and
delegate the calls to the richtextbox control.
 
Thanks

Herfried K. Wagner said:
No, that's not possible. You can either make a reference to the
richtextbox control available by your control or reimplement its public
members and delegate the calls to the richtextbox control.
 

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

Back
Top