How add unknown control at runtime

  • Thread starter José Teixeira Junior
  • Start date
J

José Teixeira Junior

Hi,

How i can add one new unknown control at runtime in my form1?

Thaks.
 
M

Morpheu

I want create a control where i only have your type into a string.
Example:
c = "System.Windows.Forms.TextBox"

How can i do?
 
F

Fergus Cooney

Hi José, Morpheu,

Jose, I don't know how 'unknown' your control is but this may answer your
query. If not, tell me more about what you want to do.

Morpheu, the following will create a Control given a name such a "Button".

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

This was given in answer to a similar query a week ago. For an explanation
of how it works, see
http://tinyurl.com/r393

Regards,
Fergus
 
T

Tom Spink

Maybe you should put a bit of error checking in, You are using DirectCast,
but you aren't sure whether the user has specified a type derived from
control:

Replace:
Return DirectCast (Activator.CreateInstance (oType), Control)

With:

' ///
Dim oObj As Object = Activator.CreateInstance(oType)

If TypeOf oObj Is Control Then
Return DirectCast(oObj, Control)
Else
oObj = Nothing
Return Nothing
End If
' ///

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

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
F

Fergus Cooney

Hi again,

Oops, window too small - missed the bit below.

If I return Nothing, the caller is going to have to check whether the
Control is Nothing instead of trapping an Exception. I think it's preferable
to throw the Exception (especially as it is done for me). This is a more
violent way to bring the error to the caller's attention, true, but I think it
is preferable.

Regards,
Fergus
 
F

Fergus Cooney

Hi yet again Tom,

Third thoughts.

oType = GetType (SomeCrap) is likely to fail too. Before you know it the
whole thing is a mass of error handling and while it may be 'proper', it could
frighten off many a punter becaue it looks so horrendously complicated. That's
the tightrope we walk - give 'em something which will get them on their way,
or give 'em the full works which they might well reject despite it being
'perfect'. ;-)

Regards,
Fergus
 
T

Tom Spink

I know, I know. It's just that you used DirectCast as opposed to CType, and
it looked a bit suspicious there, without any form of checking.

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

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
F

Fergus Cooney

Hi Tom,

What would CType do for me that DirectCast wouldn't? I ask because I never
use CType on objects.

Regards,
Fergus
 
T

Tom Spink

CType performs coercion when casting one type to another, if the types do
not match. DirectCast casts one type to another, but does not perform
coercion. DirectCast usually performs better than CType. DirectCast will
*only* work if the types are the same, otherwise you'll get a runtime error:

Dim objNumber As Object = 1.5453
Dim intInteger As Integer = DirectCast(objNumber, Integer)

A nice little runtime error is raised from this.

objNumber = 1
intInteger = DirectCast(objNumber, Integer)

No runtime error!! And it performs better than CType.

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

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
F

Fergus Cooney

Hi Tom,

Thanks for that, though I know about the numbers bit. ;-)

What I was wondering is .. what difference is there with <objects>. Why
would DirectCast look suspicious to you where CType wouldn't, when casting a
potential Control? And what extra could CType do that DirectCast can't.

Regards,
Fergus
 
T

Tom Spink

DirectCast should only be used when you are definately positively sure what
you are casting is valid:

Public WithEvents Bar As Goo

Public Sub Foo(ByVal sender As Object, ByVal e As EventArgs) Handles Bar.Baz
Dim Moo As Object

Moo = DirectCast(sender, Goo)
End Sub

In this case, Bar is defined as Goo, and theres no way Foo will be raised
(unless something else calls it) without sender being of type Goo.

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

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 
F

Fergus Cooney

Hi Tom,

Thanks for that, I know that DirectCast requires a match. ;-))

What I was wondering is .. where does CType come into the picture with
regard to objects?

You said that DirectCast 'looked suspicious' to you whereas CType wouldn't
have done. I'm just curious as to why.

Like I say I've never used CType with objects and am wondering what CType
does that DirectCast doesn't.

Regards,
Fergus
 
H

Herfried K. Wagner [MVP]

* "Fergus Cooney said:
What I was wondering is .. what difference is there with <objects>. Why
would DirectCast look suspicious to you where CType wouldn't, when casting a
potential Control? And what extra could CType do that DirectCast can't.

You will find a good explanation in the online help (look for "CType"
and "DirectCast"). If you want to know exactly what's going on, you can
use "ildasm.exe" to inspect the compiled code.
 

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

Top