Checking for null icon

  • Thread starter Thread starter Clive Dixon
  • Start date Start date
C

Clive Dixon

If I create a form, not supplying an icon, if I get the value of the Icon
property I get the default icon back (even though no icon is being displayed
in the form). How can I determine whether the icon is set in a form or not?
I have been trying using PInvoke with Win32 APIs with no success.
 
really catchy. You can't set icon null for a Form. Only way is to see if it
is default or not

VJ
 
So the follow up question is "how can I tell that the Icon returned by
System.Windows.Forms.Form.Icon is the default icon or not?" It doesn't have
to be a neat solution, as I only need this information in NUnit test code. I
guess I would have to do something like create a dummy form and compare
handles (assuming that the handle is the same for all instances of the icon
in all forms).
 
Oh no Clive.. that sounds a hack.. may not work. So yours is a test bed app
that says oops you missed a icon for the Form?.. ahh...
Did u google and see if you get anything?

- keep the handle as last resort! (sometimes hack is a good practice,
although I hate it)
VJ
 
Actually I'm looking to check whether a form which *shouldn't* have an icon
displayed (it has FormBorderStyle = FixedDialog and the icon is not set,
which means no icon is displayed, not even the default one) has *not*
accidentally acquired one. We have localised forms, which means that the
visual designer will always modify the InitializeComponent to set the Icon
to something from the .resx. Hence every time somebody modifies one of these
localised forms, the visual designer adds back in code (thanks, Microsoft!)
to explicitly set the icon to a copy of the default icon, and thus it now
appears in the dialog. I want to write some NUnit tests to check when this
has happened and someone has forgotten to correct it, 'cos I'm getting fed
up.

I Googled and couldn't find anything useful. I tried my nasty hack comparing
the handle against a dummy form, and it appears to work, i.e. the handles
are the same if I do not set the icon, but if I do, they are different. Yes,
it is *very* yucky...
 
You could try name-hiding the "Icon" property.

like this:
class YourForm : Form
{
new public Icon Icon
{
get { return null; }
set { }
}
}

Name hiding is NOT polymorphic.
If you have a Form reference, your code will not be executed.

The InitializeCompoenent function calls
this.Icon = (get something form resources);

However, if you do this, the default icon still appears on the form.

I think you may just be better off using the Form.ShowIcon property.

--Matt
 
Belivie it or not this exists from VB days.. and nothing has been done. Well
maybe there is a inherent problem or reason to do.. I would love to learn.
One possibility I can think of is the association of the ControlBox to the
Icon, Maybe it has something to do with this.

Handle sounds ok to me.

VJ
 
ShowIcon: That's in 2.0 - we are still stuck with 1.1 unfortunately. Besides
which, the docs say

"This property has no effect if FormBorderStyle is set to FixedDialog.",

which is the case in my situation.
 
Back
Top