From: zzz on

"Armin Zingler" <az.nospam(a)freenet.de> ha scritto nel messaggio
news:eadMfotuKHA.4464(a)TK2MSFTNGP04.phx.gbl...
>> Me.Controls.Remove(sender)
>
> Me.Controls.Remove(DirectCast(sender, Control))

No need, it already works also with Me.Controls.Remove(sender)

> Public Sub assegna_proprieta(ByVal source_obj As Object, ByVal dest_obj
> As Object)
>
> changed: ByRef -> ByVal

Thanks .....I have just learnt something about passing an object byval or
byref ;)

>
>I'm curious if copying all property
> values works. At runtime, using Reflection is more work than explicitly
> copying
> values bit by bit. Anyway, your decision.

Something is wrong and it doesn't work.Some property of the Parent is not
good for the child, so I've modified the sub copying 4 only properties:
{"Name", "Location", "Size", "Text"} but I'd like to understand the reason.
If Mybutton inherits by button, I'd expect that every value of the parent is
good also for the child....I used reflection because I want to use this code
for different objects like button, listbox, radiobox and so on. by
reflection the program will copy only all the right attributes from parent
to child object....


The last step is replacing all the events of the old object with the new
object so the question is:
if I have this code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

how can I get the sub name (Button1_Click) related to button1.click event
using reflection? getting the same sub name, I must only create an handler
of the event for the new object and the code is already written inside the
sub.
Thanks and sorry for the long post


Public Sub assegna_eventi(ByVal source_obj As Object, ByVal dest_obj As
Object)
Dim tx As Type = source_obj.GetType()
Dim events() As EventInfo = tx.GetEvents()
For Each eve As EventInfo In events
Dim pi As System.Reflection.EventInfo =
dest_obj.GetType().GetEvent(eve.Name)
If pi IsNot Nothing Then
Try
Dim src As System.Reflection.EventInfo =
source_obj.GetType().GetEvent(eve.Name)
Debug.Print(src.Name)


From: Armin Zingler on
Am 03.03.2010 15:57, schrieb zzz:
>> I'm curious if copying all property
>> values works. At runtime, using Reflection is more work than explicitly
>> copying
>> values bit by bit. Anyway, your decision.
>
> Something is wrong and it doesn't work.Some property of the Parent is not
> good for the child,

For example?

> so I've modified the sub copying 4 only properties:
> {"Name", "Location", "Size", "Text"} but I'd like to understand the reason.
> If Mybutton inherits by button, I'd expect that every value of the parent is
> good also for the child....

In general, yes. However, some properties are readonly. And/Or for some
properties it doesn't make sense to copy them, for example the "Handle"
property.



> I used reflection because I want to use this code
> for different objects like button, listbox, radiobox and so on. by
> reflection the program will copy only all the right attributes from parent
> to child object....

In your prev message you asked "why?" (it's more work). Now you know why. :)

Now I see that with "attributes" you mean properties. In my 1st post I thought
you're referring to http://msdn.microsoft.com/en-us/library/5x6cd29c.aspx


> The last step is replacing all the events of the old object with the new
> object so the question is:
> if I have this code:
> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles Button1.Click
>
> how can I get the sub name (Button1_Click) related to button1.click event
> using reflection? getting the same sub name, I must only create an handler
> of the event for the new object and the code is already written inside the
> sub.
> Thanks and sorry for the long post

Well, it could be solved this way but IMO you're making it much more
complicated than necessary. I still don't understand why you don't want to
_always_ use your own control classes. By setting their properties you
can change their look. No need for creating, copying, searching,
reflectioning... ;)


> Public Sub assegna_eventi(ByVal source_obj As Object, ByVal dest_obj As
> Object)
> Dim tx As Type = source_obj.GetType()
> Dim events() As EventInfo = tx.GetEvents()
> For Each eve As EventInfo In events
> Dim pi As System.Reflection.EventInfo =
> dest_obj.GetType().GetEvent(eve.Name)
> If pi IsNot Nothing Then
> Try
> Dim src As System.Reflection.EventInfo =
> source_obj.GetType().GetEvent(eve.Name)
> Debug.Print(src.Name)


--
Armin
From: zzz on

"Armin Zingler" <az.nospam(a)freenet.de> ha scritto nel messaggio
news:%23$O22buuKHA.2436(a)TK2MSFTNGP04.phx.gbl...
> Am 03.03.2010 15:57, schrieb zzz:
>>> I'm curious if copying all property
>>> values works. At runtime, using Reflection is more work than explicitly
>>> copying
>>> values bit by bit. Anyway, your decision.
>>
>> Something is wrong and it doesn't work.Some property of the Parent is not
>> good for the child,
>
> For example?

I don't know...the program doesn't work well and the button is not created.
I should investigate more to see which property gives me problems

>
>> so I've modified the sub copying 4 only properties:
>> {"Name", "Location", "Size", "Text"} but I'd like to understand the
>> reason.
>> If Mybutton inherits by button, I'd expect that every value of the parent
>> is
>> good also for the child....
>
> In general, yes. However, some properties are readonly. And/Or for some
> properties it doesn't make sense to copy them, for example the "Handle"
> property.
>
>
>
CUT

>> how can I get the sub name (Button1_Click) related to button1.click
>> event
>> using reflection? getting the same sub name, I must only create an
>> handler
>> of the event for the new object and the code is already written inside
>> the
>> sub.
>> Thanks and sorry for the long post
>
> Well, it could be solved this way but IMO you're making it much more
> complicated than necessary. I still don't understand why you don't want to
> _always_ use your own control classes. By setting their properties you
> can change their look. No need for creating, copying, searching,
> reflectioning... ;)

Suppose that I have alrteady created a complex program and I wish to change
the style of controls. I should insert my classes of customized components
like mybutton,myradiobutton and so on and set them manually step by step
with high probability of errors.
Instead if I create this class, i only have to drag it on every form and it
will custom every components auomatically.
Is there a fast way to customize the appareance of already existing controls
on form?
I'd like to know how skincrafter works ...
BTW how can get the name the sub name related with the event? ;)
Thanks a lot for your help



>
>
>> Public Sub assegna_eventi(ByVal source_obj As Object, ByVal dest_obj As
>> Object)
>> Dim tx As Type = source_obj.GetType()
>> Dim events() As EventInfo = tx.GetEvents()
>> For Each eve As EventInfo In events
>> Dim pi As System.Reflection.EventInfo =
>> dest_obj.GetType().GetEvent(eve.Name)
>> If pi IsNot Nothing Then
>> Try
>> Dim src As System.Reflection.EventInfo =
>> source_obj.GetType().GetEvent(eve.Name)
>> Debug.Print(src.Name)
>
>
> --
> Armin


From: zzz on

> Is there a fast way to customize the appareance of already existing
> controls
> on form?

Could I add an new handler to all the controls about the event paint and
simply changing the appareance of every control?
Thanks


Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
For Each t As Control In Me.Controls
If t.GetType() Is GetType(Button) Then
AddHandler t.Paint, AddressOf my_Paint
End If
Next
End Sub


From: Armin Zingler on
Am 03.03.2010 16:53, schrieb zzz:
> BTW how can get the name the sub name related with the event? ;)

Names are made for programmers. They are resolved by the compiler.

Finding the event handlers of an event of an object is not easy
because the way events are managed is an implementation detail
of the class. What you can retrieve by using reflection is an EventInfo
object. It's GetAddMethod/GetRemoveMethod methods only return
the methods that add/remove an event handler to/from an event.
What these GetAddMethod/GetRemoveMethod do is unknown to the
outside.

For example, the Control class stores all event handlers
in a System.ComponentModel.EventHandlerList object. You'd have
to retrieve that list. Then, a private shared field (called Eventclick
in the case of the click event) is used to identify the event.
An EventHandler object is returned and invoked. As EventHandler
is derived from MultiCastDelegate, all event handlers are called.
The EventHandler's GetInvocationList methods returns the event
handlers. These are Delegates. A delegate's Method property returns
the method, i.e the event handler, being called, and it's
Target property points to the object on which the call is made
(it's Nothing for shared methods). So, here we are!

But again, that's an implementation detail and could change
with a later version or SP only.



The rest of the topic (about replacing controls) is over my head.

--
Armin
First  |  Prev  |  Next  |  Last
Pages: 1 2 3 4
Prev: Writing to text file
Next: Windows Service