Prev: Writing to text file
Next: Windows Service
From: zzz on 2 Mar 2010 03:52 Hello, I have a button control (button1) on form. In my project I create a class mybutton that inherits by button class. Now, I want to change button1 class in mybutton. How can I do it? Thanks I tried this, but it doesnt work Dim a As New MyButton a = CType(button1, MyButton) Me.Controls.Add(a) Me.Controls.Remove(button1) Thanks
From: Armin Zingler on 2 Mar 2010 07:00 Am 02.03.2010 09:52, schrieb zzz: > Hello, I have a button control (button1) on form. > In my project I create a class mybutton that inherits by button class. > Now, I want to change button1 class in mybutton. How can I do it? > Thanks > > I tried this, but it doesnt work > Dim a As New MyButton > > a = CType(button1, MyButton) > > Me.Controls.Add(a) > > Me.Controls.Remove(button1) > > Thanks CType can cast or convert. Casting requires that the referenced object is of the destination type or is derived from the destination type. This is not true in your case above. Converting means creating a new instance of the destination type. VB can not know how to create a new MyButton instance from a Button instance. However, You can create a new MyButton instance by defining a constructor that takes a Button object as an argument. In the constructor, you can initialize your MyButton object by copying the data from the Button object and initializing addition fields. -- Armin
From: zzz on 2 Mar 2010 09:19 "Armin Zingler" <az.nospam(a)freenet.de> ha scritto nel messaggio news:egKzTGguKHA.4220(a)TK2MSFTNGP05.phx.gbl... > Converting means creating a new instance of the destination type. > VB can not know how to create a new MyButton instance from a Button > instance. However, You can create a new MyButton instance by defining a > constructor that takes a Button object as an argument. In the > constructor, you can initialize your MyButton object by copying the > data from the Button object and initializing addition fields. > > Thanks. One question more: how can I copy all the references of the object? Like attributes, handlers and so on? The goal is replace every button control with the new mybutton control....so i 'd like that also Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click becomes """" handles mybutton1.click Thanks
From: Armin Zingler on 2 Mar 2010 10:55 Am 02.03.2010 15:19, schrieb zzz: > "Armin Zingler" <az.nospam(a)freenet.de> ha scritto nel messaggio > news:egKzTGguKHA.4220(a)TK2MSFTNGP05.phx.gbl... > >> Converting means creating a new instance of the destination type. >> VB can not know how to create a new MyButton instance from a Button >> instance. However, You can create a new MyButton instance by defining a >> constructor that takes a Button object as an argument. In the >> constructor, you can initialize your MyButton object by copying the >> data from the Button object and initializing addition fields. >> >> > Thanks. > One question more: how can I copy all the references of the object? Like > attributes, handlers and so on? > The goal is replace every button control with the new mybutton control....so > i 'd like that also > Private Sub button1_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles button1.Click > > becomes > > """" handles mybutton1.click I don't know where to start asking. ;-) Some thoughts: - all references: you have to do it manually - attributes: they are attached at compile time - handlers: Use Removehandler/Addhandler to remove/add handlers to events of objects. If you don't want to have one method handle the event of an object all the time, don't use the Handles keyword. Anyway, what's your final goal? There may be simpler ways to achieve it. -- Armin
From: zzz on 2 Mar 2010 11:11
"Armin Zingler" <az.nospam(a)freenet.de> ha scritto nel messaggio news:eOTQ8GiuKHA.4636(a)TK2MSFTNGP06.phx.gbl... > Anyway, what's your final goal? There may be simpler ways to achieve it. > I'd like to replace a kind of controls, for example to have my own skin. I can create a mybutton class that inherits by button class, but it s different because in the paint event, it redraws the button like I want. Doing it for many controls, I can have my customized graphic controls The idea was looking at skincrafter.net( dll that allows to cutomize skin in windows form, free registering visual studio express), and I wanted to create something of similar. If I can override the On paint of all my controls, I could easily get it .....;) Thanks |