Macro-substitution is possible?

  • Thread starter Thread starter Ritesh Sompura
  • Start date Start date
R

Ritesh Sompura

Hi! All there,

I need to create controls on the form depending upon the values from
database.
If database contains -
Button then a button is tobe created,
Label then a label is to be created, and so on...

I can write select case or if elseif kind of structure. But, it would be
nice if I can have something like 'Macro substitution' in FoxPro.

That is, what I want is,
dim control = new <a text from DB>
Just by this, all my needs of writing select case and nested if is
eliminated.
If the text contains "Button" then button will be of button type.

Hence, just by changing value in DB, my forms gets changed at the time of
creation.

Hope I have explained properly. Any suggestion is welcomed.

Thank you in advance.
 
Hi Ritesh,

Dim sTypeName = "System.Windows.Forms.Button"
Dim oType = Type.GetType (sTypeName)
Dim oControl = DirectCast (Activator.CreateInstance (oType), Control)
 
Hi again Ritesh,

Please ignore this - it got sent off before I was finished and will not
work.

Sorry :-(

Regards,
Fergus
 
Ritesh Sompura said:
I need to create controls on the form depending upon the values from
database.
If database contains -
Button then a button is tobe created,
Label then a label is to be created, and so on...

I can write select case or if elseif kind of structure. But, it would be
nice if I can have something like 'Macro substitution' in FoxPro.

That is, what I want is,
dim control = new <a text from DB>
Just by this, all my needs of writing select case and nested if is
eliminated.
If the text contains "Button" then button will be of button type.

You can instantiate a class by its name, have a look at 'Activator.CreateInstance'.
 
Hi Ritesh,

Let's start again :-)

The code before was in the right form but needs some adjustment and
certainly some explanation.

Public Function MakeControl (sTypeName As String) As Control
Dim oType As Type = Type.GetType (sTypeName)
Return DirectCast (Activator.CreateInstance (oType), Control)
End Function

This will create a Control with the given name. First it finds out what
..NET Type is required, then it creates a Control of that type using the
Activator class. Finally it converts the resulting Object into a Control for
the caller. Pretty straightforward. The problem is in determining what that
name is going to be.

You can't simply use "Button" because there could be all sorts of Buttons
in different Namespaces. So you have to use "System.Windows.Forms.Button" to
specify that it's the Controls Namespace - as opposed to the
"MrsPottsAndCo.Products.Ivory.Button", for instance.

But <that's> not enough. You also have to supply information about the
Assembly that the Control comes from so that you get the correct version. With
my system, the full Type name for a Button Control is
"System.Windows.Forms.Button, System.Windows.Forms, Version=1.0.3300.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089".!!

For a label it's all of that except for "Label" instead of Button. So you
could have simply "Button" or "Label" in your database and add in the rest
using code. Let's see how.

The next issue is how do you find out what 'the rest' is on <your> system
?

Well, if you use this:
Dim sFormTypeName As String _
= GetType (Form).AssemblyQualifiedName
you'll get the type name for an ordinary Form.

You can then substitute in your Control name.
sTypeName = "Button"
sTypeName = sFormTypeName.Replace ("Form,", sTypeName & ",")
(Notice the ',' immediately after 'Form'.)

This will give you the correct type name for a Button based on that of a
Form. So finally, the Function that will properly do the job that you want is:

Public Function MakeControl (sTypeName As String) As Control
Dim sFormTypeName As String _
= GetType (Form).AssemblyQualifiedName
sTypeName = sFormTypeName.Replace ("Form,", sTypeName & ",")
Dim oType As Type = Type.GetType (sTypeName)
Return DirectCast (Activator.CreateInstance (oType), Control)
End Function

Now all you have to do is set the size, position, text, etc, etc. But I'm
assuming you have that bit already worked out. ;-)

Regards,
Fergus
 
Hi Herfried,

LOL, A little bit more specific, if you please?!!! Like maybe an example
using Button?

(And no peeking at my other post!) ;-))

Regards,
Fergus
 
Fergus Cooney said:
LOL, A little bit more specific, if you please?!!! Like maybe an example
using Button?

\\\
Dim b As Object = _
System.Activator.CreateInstance( _
System.Reflection.Assembly.LoadWithPartialName( _
"System.Windows.Forms" _
).GetType( _
"System.Windows.Forms.Button" _
) _
)
b.Location = New System.Drawing.Point(20, 20)
b.Size = New System.Drawing.Size(100, 100)
b.Text = "Foo the Bar"
Me.Controls.Add(b)
///

Keywords: button, windows forms, create instance, class name, string.
 
Hi Herfried,

Heeey, full-on, Bro. I like it.
And all on 'one' line too!!

;-)))

Regards,
Fergus
 
Herfried K. Wagner = MVP ? VB Classic : VB.NET

Runtime Error 6

SCNR

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
Back
Top