473,385 Members | 1,324 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Inheriting from UserControl...

I'm trying to create a C# composite control and have inherited
from UserControl. This new control contains 9 buttons with images on
them ( no text ).

Firstly, what is the best way to expose these 9 buttons to the
developers who will be using my control. I want them to be able to
change visibility, enablement, onClick etc for each button, but not be
able add any new buttons. I initially thought of exposing them as some
kind of fixed array, but wanted the opinions of those with more
experience. In the case of an array or collection, what is the correct
way of exposing this so it can be customised via the object inspector.

Secondly, if I want a specific icon/bmp to appear in the ToolBox for my
component, where must this icon/bmp be stored. I know I need to use
ToolboxBitmap(typeof(MyComponent), "MyComponentBitmap") attribute, but
can this exist in an image list or should it be placed somewhere else?
Dec 4 '05 #1
9 1906
"Dominique" <Do*******@discussions.microsoft.com> wrote in message
news:47**********************************@microsof t.com...
I'm trying to create a C# composite control and have inherited
from UserControl. This new control contains 9 buttons with images on
them ( no text ).

Firstly, what is the best way to expose these 9 buttons to the
developers who will be using my control. I want them to be able to
change visibility, enablement, onClick etc for each button, but not be
able add any new buttons. I initially thought of exposing them as some
kind of fixed array, but wanted the opinions of those with more
experience. In the case of an array or collection, what is the correct
way of exposing this so it can be customised via the object inspector.
Pass them out as a collection the same way the toolbar does it with the
toolbar buttons.
Secondly, if I want a specific icon/bmp to appear in the ToolBox for my
component, where must this icon/bmp be stored. I know I need to use
ToolboxBitmap(typeof(MyComponent), "MyComponentBitmap") attribute, but
can this exist in an image list or should it be placed somewhere else?


As a resource I would say, although I haven't done it. Just add the bitmap
to your project and specify it's build action as resource.

Michael
Dec 5 '05 #2
"Michael C" wrote:
Pass them out as a collection the same way the toolbar does it with the
toolbar buttons.


OK, but what is the proper syntax for exposing this collection? I think I
will use an inherited ArrayList for simplicity.

Dec 5 '05 #3
Hi,
OK, but what is the proper syntax for exposing this
collection? I think I will use an inherited ArrayList for >

simplicity.

You should use a HashTable, it will give you much better performance.

Happy Coding,

Stefan
C# GURU
www.DotNETovation.com

*** Sent via Developersdex http://www.developersdex.com ***
Dec 5 '05 #4
OK thanks for that. I now have my collection appearing in the Property
inspector, but I cannot set each button's event from there. Is there
something else I need to do for this to be enabled?

Dominique.

"Stefan" wrote:
Hi,
OK, but what is the proper syntax for exposing this
collection? I think I will use an inherited ArrayList for >

simplicity.

You should use a HashTable, it will give you much better performance.

Happy Coding,

Stefan
C# GURU
www.DotNETovation.com

*** Sent via Developersdex http://www.developersdex.com ***

Dec 5 '05 #5
"Dominique" <Do*******@discussions.microsoft.com> wrote in message
news:CA**********************************@microsof t.com...
OK thanks for that. I now have my collection appearing in the Property
inspector, but I cannot set each button's event from there. Is there
something else I need to do for this to be enabled?


Have one event which passing in the button as an enum parameter.

Michael
Dec 5 '05 #6
"Stefan" <we******@dotnetovation.com> wrote in message
news:Ob****************@TK2MSFTNGP14.phx.gbl...
Hi,
OK, but what is the proper syntax for exposing this
collection? I think I will use an inherited ArrayList for >

simplicity.

You should use a HashTable, it will give you much better performance.


I'd just define my own collection in a case like this that does not need
performance. That way you can have the exact functionality you need.

Michael
Dec 5 '05 #7
I would take a completely different tack on this than the other
responders.

First, are sure that there will only ever by 9 buttons? Do they
indicate something specific? I can, for example, having them in a
three-by-three square, something like the text alignment buttons in
Visual Studio Designer. You know: "upper left", "middle left", "lower
left", "upper middle"... that sort of thing.

If that's the case, then I submit that you don't want to just expose
them in a collection. Sure, that's the easy way to do it, but what you
really want is to create a UserControl that returns an indication of
what position the user chose. If you expose the nine buttons in a
collection then you're showing your client classes _how_ you solved the
problem: you're exposing implementation.

If we're taking the position buttons user control as an example, then
what you really _want_ to do is expose an interface that says, "This
control allows the user to choose one of nine positions, but I'm not
telling you how it does that." So, I would make it expose events /
properties like this:

1. A "position" enumeration: TopLeft, MiddleLeft, BottomLeft, etc. It
should have a [Flags] attribute so that the positions can be combined.
2. A "position chosen" event (just one event) that carries in its event
arguments which position the user chose.
3. A method to set which positions the user is allowed to choose (via a
combination of the "position" enumeration).
4. A method to set which positions the user is allowed to see (via a
combination of the "position" enumeration).
5. Nine properties for each button property that you want to expose to
clients, for example, "TopLeftImage," "MiddleLeftImage", etc.

Yes, this is a whole lot more work, but the advantage is that your
callers don't know how the control does its job. You could change it
from buttons to a radio button list and the same interface would still
apply.

There are exceptions (the ToolBox being one), but as a general rule
it's bad form to allow clients to see inside your UserControl and get
directly at controls therein, because then that locks you into one
implementation, and doesn't allow for multiple UserControls that do the
same thing (implement the same interface) but do it in different ways
(look & feel).

If you do decide to go the easy way and just expose an ArrayList of
buttons or something, make sure that you take care not to give your
clients direct access to your inside structures so that they can
cheerfully add and remove buttons from your control without your
consent. If you want them to never be able to change how many buttons
there are (just their attributes), build the collection / array to
return every time your client asks for the button list. Clients will
still have access to the original buttons, but not your internal
collection structure.

Dec 5 '05 #8
>> Bruce Wood << >> Wrote <<
I would take a completely different tack on this than >> the other

responders.

I agree with Bruce if you have the time make it scalable as Bruce
explained.

To add to Bruce's thought you take it one step further and create your
own 'Button' Class inheriting from 'Button', here is a quick example I
created just to give you an idea of were to start:

namespace Sle.Control
{
[System.ComponentModel.DefaultProperty("Text"),
System.Web.UI.ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1: System.Web.UI.WebControls.WebControl
{
private string text;

[System.ComponentModel.Bindable(true),
System.ComponentModel.Category("Appearance"),
System.ComponentModel.DefaultValue("")]
public string Text
{
get
{
return text;
}

set
{
text = value;
}
}

protected override void Render(System.Web.UI.HtmlTextWriter output)
{
output.Write(Text);
}
}
}
Happy Coding,

Stefan
C# GURU
www.DotNETovation.com

"You always have to look beyond the horizon and can never be complacent
-- God forbid we become complacent."

Jozef Straus

*** Sent via Developersdex http://www.developersdex.com ***
Dec 5 '05 #9
"Bruce Wood" <br*******@canada.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
I would take a completely different tack on this than the other
responders.


You've misinterpreted my response. I wasn't suggesting to expose a
collection of actual command buttons. Instead I would create a simple class
with the properties desired and expose a collection of those. This still
hides the implementation underneath but avoids the situation of having
several related functions, eg ButtonPressed(index), ButtonColor(index) etc.

Michael
Dec 6 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kevin Blackwell | last post by:
This is probably a basic c# question, but I'm having problems with it. I currently have a project that contains two classes. From the calss view. Class1 Class2 I have included a usercontrol...
4
by: Joel Barsotti | last post by:
On certain pages I want to have a user control that is derived not directly from the usercontrol class, but from a class that extends the UserControl class so my objects will have a some...
4
by: Alan Silver | last post by:
Hello, I am displaying product details on a page and am allowing the site owner to specify the style in which the product details are displayed. I am debating which of two approaches to use here...
2
by: Charles Law | last post by:
I have a base class - ComponentBase - that inherits from UserControl. My class implements IComponentBase, which defines a minimal set of core properties and methods. All my other components inherit...
4
by: elziko | last post by:
I would like to do the following: Create a class that inherits from a usercontrol. Then add a control to the designer Call this new class ClassA. Many controls I need will start off like this...
3
by: YYZ | last post by:
I swear I've done my research, and now I was just hoping someone could explain this to me. I've got a base class (usercontrol) that I am using just as an interface. Meaning, I've defined...
4
by: DanG | last post by:
Howdy, On past .NET projects, I only had System.Web.UI.Page forms. One application needed a set of functions to do processing against the Page, Session and Request objects associated with the...
1
by: =?Utf-8?B?a2Fu?= | last post by:
hi, Consider i have built a UserControl say 'ColorMixer' in proj 'Step1'(Windows Control Proj). Now when create another proj 'Step2' as Windows Control and try adding 'ColorMixer' as inherited...
2
by: Charlie Brown | last post by:
I have two classes, one that inherits from UserControl called Base. I have a second class that inherits from Base. I do not want to expose a lot of the properties of usercontrol beyond the Base...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.