473,396 Members | 2,011 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,396 software developers and data experts.

How add unknown control at runtime

Hi,

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

Thaks.
Nov 20 '05 #1
22 1843
I want create a control where i only have your type into a string.
Example:
c = "System.Windows.Forms.TextBox"

How can i do?
"José Teixeira Junior" <te*************@stinfo.com.br> escreveu na mensagem
news:O0**************@TK2MSFTNGP11.phx.gbl...
Hi,

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

Thaks.

Nov 20 '05 #2
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
Nov 20 '05 #3
* "José Teixeira Junior" <te*************@stinfo.com.br> scripsit:
How i can add one new unknown control at runtime in my form1?


Please don't multipost.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #4
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
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:ue**************@TK2MSFTNGP12.phx.gbl... 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

Nov 20 '05 #5
Hi Tom,

What shall I do when I've trapped the error?

Regards,
Fergus
Nov 20 '05 #6
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


Nov 20 '05 #7
Hi Herfried,

Multipost? - did I miss the other one?

Regards,
Fergus
Nov 20 '05 #8
Hi Fergus,

Thank you for its help.
It worked as wanted.

Regards,
Morpheu
"Fergus Cooney" <fi******@tesco.net> escreveu na mensagem
news:ue**************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #9
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
Nov 20 '05 #10
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
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:#f**************@TK2MSFTNGP09.phx.gbl...
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

Nov 20 '05 #11
Hi Tom,

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

Regards,
Fergus
Nov 20 '05 #12
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
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
Hi Tom,

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

Regards,
Fergus

Nov 20 '05 #13
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
Nov 20 '05 #14
* "Fergus Cooney" <fi******@tesco.net> scripsit:
Multipost? - did I miss the other one?


In an other group.

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #15
Hi Herfried,

Thanks.

I hate that.

Regards,
Fergus
Nov 20 '05 #16
* "Fergus Cooney" <fi******@tesco.net> scripsit:
I hate that.


You are not alone.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #17
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
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:OS**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #18
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

Nov 20 '05 #19
* "Fergus Cooney" <fi******@tesco.net> scripsit:
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.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #20
Hi Fergus,

The reason I said it looked suspicious is that my trained eye ;-) spots
things that could be potential errors. IMO, it would be better to use CType
in this case because you maybe unsure of type conversion. Of course, there's
absolutely nothing wrong with DirectCast, I use it all the type, where I
can. But IMO it just scares me slightly to see it being used when types
could be potentially different.

That's all, I probably haven't said "Your code is fantastic" yet, so here we
go:

Oh by the way, your code is fantastic!!

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

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:#f**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #21
Hi Tom,

Thanks for that, I know about using CType for ambiguity. ;-)))

ROFL. I'm playing, of course.

I'm like you in my sensitivity with these two, although it's often the
other way round - are you sure it needs a CType - couldn't a DirectCast be use
instead! But for objects? I don't think that CType can do anything to coerce
an object from one type to another. It's either DirectCastable or not. Then
again, maybe I'm wrong? [As an aside - for such a common tool, why couldn't
they have just called it Cast? And what and where is IndirectCast?]
~~ nothing wrong with DirectCast,
~~ I use it all the <type>, where I can.

Isn't the Mind wonderful the way it create typos. Perfectly in tune with
what you're saying.
~~ Oh by the way, your code is fantastic!!

ROFL. That's very kind of you. Some of it, maybe, but not this code I'm
afraid. :-(

Here's what Jon Skeet had to suggest about this routine in a different
newsgroup.

\\ That looks complicated and slightly tricksy to me.
\\ You can make the code simpler than that:
\\
\\ Dim formsAssembly as Assembly = GetType (Form).Assembly
\\ Dim oType as Type = formsAssembly.GetType _
\\ ("System.Windows.Forms."& sTypeName)
\\ Return DirectCase (Activator.CreateInstance (oType), Control)

I'm not the master of Type and Assembly yet - still learning. ;-)

Regards,
Fergus
Nov 20 '05 #22
MSDN states that CType tries to perform coercion, I cannot remember the
page, however. Perhaps a google will reveal more.

Type (whoops, I mean _time_) for a snooze

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

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
"Fergus Cooney" <fi******@tesco.net> wrote in message
news:OI**************@TK2MSFTNGP09.phx.gbl...
Hi Tom,

Thanks for that, I know about using CType for ambiguity. ;-)))

ROFL. I'm playing, of course.

I'm like you in my sensitivity with these two, although it's often the
other way round - are you sure it needs a CType - couldn't a DirectCast be use instead! But for objects? I don't think that CType can do anything to coerce an object from one type to another. It's either DirectCastable or not. Then again, maybe I'm wrong? [As an aside - for such a common tool, why couldn't they have just called it Cast? And what and where is IndirectCast?]
~~ nothing wrong with DirectCast,
~~ I use it all the <type>, where I can.

Isn't the Mind wonderful the way it create typos. Perfectly in tune with what you're saying.
~~ Oh by the way, your code is fantastic!!

ROFL. That's very kind of you. Some of it, maybe, but not this code I'm afraid. :-(

Here's what Jon Skeet had to suggest about this routine in a different
newsgroup.

\\ That looks complicated and slightly tricksy to me.
\\ You can make the code simpler than that:
\\
\\ Dim formsAssembly as Assembly = GetType (Form).Assembly
\\ Dim oType as Type = formsAssembly.GetType _
\\ ("System.Windows.Forms."& sTypeName)
\\ Return DirectCase (Activator.CreateInstance (oType), Control)

I'm not the master of Type and Assembly yet - still learning. ;-)

Regards,
Fergus

Nov 20 '05 #23

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

Similar topics

7
by: Ivan Debono | last post by:
Hi, I keep getting an Unknown runtime error on line 3 below: 1 If oField.Type <> 136 Then 'adChapter 2 If Right(oField.name, 3) = "_id" Then 3 For Each...
7
by: José Teixeira Junior | last post by:
I need add one new unknown control at runtime and the only one information that i have is one string with type of control. Example: c = "System.Windows.Forms.TextBox" How i can create one...
5
by: TT (Tom Tempelaere) | last post by:
Hi, Once in a while my application throws an NullReferenceException at startup, however it appears to be occurring in an unknown module. If I debug it and ask to 'break', then there is no source...
5
by: Lars-Erik Aabech | last post by:
Hi! Guess it's my day again.. Tried to deploy a test release of a new asp.net web today, and got a terrible error. The web is running swell on three development computers, but when it's copied...
6
by: Morpheu | last post by:
Hello, I have other problem with the unknown control added at runtime: When i use a mdiformchild, the control don't appear. In a normal form, it works. Somebody? Thank you in advance....
4
by: Andy in S. Jersey | last post by:
I would like to create an unknown number of Arraylists, meaning, I don't know how many I should create until runtime. I will be reading a table, and 0,1,2, or more fields have to be put into...
7
by: John | last post by:
Hi Everyone, I'm having this extremely annoying problem with Internet Explorer 6, giving me an error message saying "unknown runtime error" whenever I try to alter the contents of a <divelement...
21
by: sheldonlg | last post by:
I have googled for '"Internet Explorer" "Unknown runtime error"' and not found anything useful. I have the following (for simplicity of presentation here): <div><table><tr><th...
2
by: comalco2000 | last post by:
If I place objects (i.e. image box controls containing graphics) onto a form at random during runtime, how can I then detect a button_down mouse event when I hover over one of those new controls if I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.