|
-
Aug 4th, 2010, 01:12 PM
#1
Get edit control from type
Hi,
I am retrieving some records from a database dynamically, not knowing exactly which table I need to query, or even which fields to retrieve. I display this data in a control that looks a bit like a listbox, except each item is two lines: a label and a value.
Now, the user needs to be able to browse through this list until he finds the item he needs, and then he should be able to edit the item.
This application will (eventually, this is just a 'proof of concept') run on a mobile device or PDA or something like that, so space is limited. To allow the user to edit a value, I simply show a panel containing some edit control (textbox, checkboxes, combobox, etc), let the user edit whatever he's editing, save the change and display the list of items again.
The problem now is that the items I'm getting are completely dynamic. They are not known during design-time, and in fact most of them don't even exist yet. The application can display items from any simple database by simply providing it with the tablename and a list of attributes to retrieve. I could go out and create a new table right now, tell the application which attributes to show (also in the same database) and it would happily show them.
So, I cannot possibly know the type of the data I am getting, so I can also not know which kind of editing control I need to provide!
For example, if the data is of type String, I could display a Textbox. If the data is of type Color, I could display a color combobox. If the data is any kind of collection (IEnumerable maybe), I show a regular combobox.
Boolean - Checkbox
Integer - Numeric textbox (with only integer values)
Double/Single - Numeric textbox (with floating point values)
etc
etc...
The most straightforward way of doing this is of course just a very long Select Case statement:
vb.net Code:
Public Function GetEditingControl(ByVal data As Object) As Control Select Case data.GetType() Case GetType(String) Return New TextBox() Case GetType(Integer) Return New NumericTextBox() Case GetType(Boolean) Return New CheckBox() Case '... End Select End Function
Problems with this:
1. It's ugly, long code, hard to maintain and hard to change.
2. I am going to run into data once that I did not foresee. So I am going to need a 'Case Else' to catch those cases. What kind of control do I return then? A TextBox maybe? I dunno.
3. Probably more that I can't think of right now...
Does anyone know any better way to do this? The Framework itself does it too somehow: if you've ever used the designer ActionLists (the shortcut windows that pop up if you click the [>] button on the top-right corner of some controls) you would know. You simply provide the designer with a bunch of properties, and somehow it determines, from those properties alone (presumably their type) which kind of control to show. It can show comboboxes, textboxes, numeric textboxes, checkboxes, Font dropdowns, Color dropdowns, etc...
I am kind of doubtful that they use a Select Case statement... But I can't think what else they would use?
EDIT
I also thought of a Dictionary(Of Type, Control) or something, but except for being long and ugly this approach has the same problems...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|