How to assign image to a unknown control
-
Hi all, I am trying to assign image to unknown control as follows. abcd(ByRef element As System.Windows.Forms.Control) { CType(element, PictureBox).Image = frmMain.DefInstance.imlMain("Image1") } But, its giving casting error. Please let me know the solution. Thanks in Advance.
AR Reddy
-
Hi all, I am trying to assign image to unknown control as follows. abcd(ByRef element As System.Windows.Forms.Control) { CType(element, PictureBox).Image = frmMain.DefInstance.imlMain("Image1") } But, its giving casting error. Please let me know the solution. Thanks in Advance.
AR Reddy
if typeof(element)==typeof(PictureBox) CType(element, PictureBox).Image = frmMain.DefInstance.imlMain("Image1") else yadada yadda
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios Discounted or Free Software for Students: DreamSpark - downloads.channel8.msdn.com MSDN Academic Alliance - www.msdnaa.com
-
if typeof(element)==typeof(PictureBox) CType(element, PictureBox).Image = frmMain.DefInstance.imlMain("Image1") else yadada yadda
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios Discounted or Free Software for Students: DreamSpark - downloads.channel8.msdn.com MSDN Academic Alliance - www.msdnaa.com
-
Hi all, I am trying to assign image to unknown control as follows. abcd(ByRef element As System.Windows.Forms.Control) { CType(element, PictureBox).Image = frmMain.DefInstance.imlMain("Image1") } But, its giving casting error. Please let me know the solution. Thanks in Advance.
AR Reddy
For starters, you seem to be mixing C# elements with VB code. Second, assuming that imlMain is an image list, the .Net version does not have a default property. Because of this, you must explicitly reference the Images collection. Third, DefInstance is a hack Microsoft added to the .Net framework to manage code translated from VB6. Assuming that this method is inside the form being referenced, this should work:
Public Sub abcd(ByRef element As System.Windows.Forms.Control) CType(element, PictureBox).Image = Me.imlMain.Images("Image1") End Sub
Properly, though, you should make sure that element really is a PictureBox. The easiest way to do this is with TryCast rather than CType. They do the same basic thing, but TryCast will return Nothing if the conversion fails. Your method would then look like this:
Public Sub abcd(ByRef element As System.Windows.Forms.Control) Dim PB As PictureBox = TryCast(element, PictureBox) If PB IsNot Nothing Then PB.Image = Me.imlMain.Images("Image1") End Sub
-
Hi Thomas, Thanks for the reply. yadada yadda ???? :) what should we do if the element is not a picturebox type? Thanks in advance,
AR Reddy
You don't have to do anything, you just have the initial if statement. When something isn't iffed then the function just ends. Besides, I had to include yadda yadda because I needed to add some technical lingo that no one would understand :) .
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios Discounted or Free Software for Students: DreamSpark - downloads.channel8.msdn.com MSDN Academic Alliance - www.msdnaa.com
-
You don't have to do anything, you just have the initial if statement. When something isn't iffed then the function just ends. Besides, I had to include yadda yadda because I needed to add some technical lingo that no one would understand :) .
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios Discounted or Free Software for Students: DreamSpark - downloads.channel8.msdn.com MSDN Academic Alliance - www.msdnaa.com
-
For starters, you seem to be mixing C# elements with VB code. Second, assuming that imlMain is an image list, the .Net version does not have a default property. Because of this, you must explicitly reference the Images collection. Third, DefInstance is a hack Microsoft added to the .Net framework to manage code translated from VB6. Assuming that this method is inside the form being referenced, this should work:
Public Sub abcd(ByRef element As System.Windows.Forms.Control) CType(element, PictureBox).Image = Me.imlMain.Images("Image1") End Sub
Properly, though, you should make sure that element really is a PictureBox. The easiest way to do this is with TryCast rather than CType. They do the same basic thing, but TryCast will return Nothing if the conversion fails. Your method would then look like this:
Public Sub abcd(ByRef element As System.Windows.Forms.Control) Dim PB As PictureBox = TryCast(element, PictureBox) If PB IsNot Nothing Then PB.Image = Me.imlMain.Images("Image1") End Sub
As far I as I am concerned (since I do this to), a mix of multiple languages to convey a clear message is considered a type of pseudocode. You don't have to know any particular language to understand the basics of pseudocode.
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios Discounted or Free Software for Students: DreamSpark - downloads.channel8.msdn.com MSDN Academic Alliance - www.msdnaa.com
-
As far I as I am concerned (since I do this to), a mix of multiple languages to convey a clear message is considered a type of pseudocode. You don't have to know any particular language to understand the basics of pseudocode.
Regards, Thomas Stockwell Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. Visit my homepage Oracle Studios Discounted or Free Software for Students: DreamSpark - downloads.channel8.msdn.com MSDN Academic Alliance - www.msdnaa.com
Just covering all the bases. ;P