Reading Control Properties At Runtime

Joined
Jul 30, 2006
Messages
10
Programming Experience
1-3
Ok, heres my problem dudes... i have an app with panels created at runtime, and these panels all have controls on them... the compiler i wrote creates these controls at runtime. heres the issue.. i have a loop to scan through the controls on the panel, get its control type and add the text to the compiler from there.... BUT! it works fine with a textbox, but vb.net wont let me compile with the image property..

Im using this syntax to refer to the controls on the panel

compiler.text = Me.Panels(currentpanel).Controls(currentcontrol).Image

and vb says image is not a member of windows.forms.control

but how can vb tell me that when it will only know wat type of control it is AT RUNTIME?!?!

ive tried using CType to convert the image to a string, but nothing....

any ideas on this one? sorry im not gd at explaining :D

cheers!
 
You have to write code to test the type and if it is a type that has an Image property then you cast it as that type and access the Image property. If you don't test the type then how can you possibly just access the Image property without knowing that the control will have an Image propert?
VB.NET:
Dim ctl As Control = Me.Panels(currentpanel).Controls(currentcontrol)

If TypeOf ctl Is PictureBox Then
    Dim img As Image = DirectCast(ctl, PictureBox).Image
End If
The alternative would be to use reflection and attempt to get a PropertyInfo object for the control's Image property. If an object is created successfully then you know that the Image property exists and you can use the PropertyInfo to get its value. I'd generally not recommend that for this situation though as it doesn't strike me as the type of situation where reflection is appropriate.
 
control types

hey! thanks for the prompt reply.... i had a question, but i worked it out....

the problem i have now is that i use that technique, then i use img.ToString to place it in the text for the compiler.... but when i show a message box of the text i just added to it, it considers the image as "system.drawing.bitmap".... and i need the image filename (C:/image1 etc) :confused:
 
An Image object has no file name as it has no requirement to have originated from a file. If you're using a VB 2005 PictureBox and you loaded the Image using the Load method then you can get the file name from the ImageLocation property. Otherwise the file name is lost forever unless you made a point of storing it somewhere. If that's required then a logical option is to use the Image's Tag property.
 
Back
Top