Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to assign image to a unknown control

How to assign image to a unknown control

Scheduled Pinned Locked Moved Visual Basic
helptutorial
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AR Reddy
    wrote on last edited by
    #1

    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

    T G 2 Replies Last reply
    0
    • A 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

      T Offline
      T Offline
      Thomas Stockwell
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • T Thomas Stockwell

        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

        A Offline
        A Offline
        AR Reddy
        wrote on last edited by
        #3

        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

        T 1 Reply Last reply
        0
        • A 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

          G Offline
          G Offline
          Gregory Gadow
          wrote on last edited by
          #4

          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
          
          T 1 Reply Last reply
          0
          • A AR Reddy

            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

            T Offline
            T Offline
            Thomas Stockwell
            wrote on last edited by
            #5

            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

            A 1 Reply Last reply
            0
            • T Thomas Stockwell

              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

              A Offline
              A Offline
              AR Reddy
              wrote on last edited by
              #6

              Thanks! :)

              AR Reddy

              1 Reply Last reply
              0
              • G Gregory Gadow

                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
                
                T Offline
                T Offline
                Thomas Stockwell
                wrote on last edited by
                #7

                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

                G 1 Reply Last reply
                0
                • T Thomas Stockwell

                  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

                  G Offline
                  G Offline
                  Gregory Gadow
                  wrote on last edited by
                  #8

                  Just covering all the bases. ;P

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups