By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,426 Members | 1,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,426 IT Pros & Developers. It's quick & easy.

Icon question

P: n/a
How do I put an icon on a form using code?
Thank you.
Nov 16 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
If you have .ico file

this.Icon = new Icon(@"c:\myIcon.ico");

--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:17***************************@freeler.nl...
How do I put an icon on a form using code?
Thank you.

Nov 16 '05 #2

P: n/a
Hi Shak,

Thank you. That works!

I have two questions if I may:

(1) How do you get around the problem of the client
changing the file in which the application should be installed?
(Yes, I can do an "if exists", but that doesn't solve the problem.)

(2) I have the icon showing in the "Properties" and in the prog
I have this.Icon = ((System.Drawing ... ("$this.icon))); This is
the same as I have in other applications. There I get an icon
showing up, here I don't. Might you please have an explanation?

Many thanks.

**********************************************

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
If you have .ico file

this.Icon = new Icon(@"c:\myIcon.ico");

--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:17***************************@freeler.nl...
How do I put an icon on a form using code?
Thank you.


Nov 16 '05 #3

P: n/a
Adrian,

1. If you are worried about client modifying the icon file do this

a) add the icon file in the solution
b) go to the properties and set Build Action to "Embedded resource"
c) Rebuild your program.

your icon file will be now part of resource. To acess icon you can do
this

Assembly myAssembly =
Assembly.GetAssembly(Type.GetType("MyNameSpace.MyC lass"));
Stream iconStream =
myAssembly.GetManifestResourceStream("MyNameSpace. myIcon.ico");
this.Icon = new Icon(icoStream);

2. When you select icon in the properties of form, you will see it
immediately in design mode. If thats not happening, there might be problem
in ur icon file or may be something else. Try loading the icon in
imagelist. It may give u a clue.
--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:57***************************@freeler.nl...
Hi Shak,

Thank you. That works!

I have two questions if I may:

(1) How do you get around the problem of the client
changing the file in which the application should be installed?
(Yes, I can do an "if exists", but that doesn't solve the problem.)

(2) I have the icon showing in the "Properties" and in the prog
I have this.Icon = ((System.Drawing ... ("$this.icon))); This is
the same as I have in other applications. There I get an icon
showing up, here I don't. Might you please have an explanation?

Many thanks.

**********************************************

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
If you have .ico file

this.Icon = new Icon(@"c:\myIcon.ico");

--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:17***************************@freeler.nl...
How do I put an icon on a form using code?
Thank you.



Nov 16 '05 #4

P: n/a
Hi Shak,

(1.a.) I wasn't worried about the client changing the icon; I was worried
about the client installing the application in a different directory so the
icon wouldn't be found. On second thought I realize that is a good way
to check whether the program is being properly installed. It is an
invoicing ("billing") program that has to work together with an accounting
program, which should have been installed already by the client.
(1.b.) I cannot find the Build Action in any properties box listing.

(2.) The icon is showing up ok in the properties box listing, hence
I was puzzled as to the cause of the icon not appearing on the
form when run. I have done no different in other programs where
there isn't an icon problem. This problem is arizing in the last two
applications I am working on.

Might you have any further suggestions?
Many thanks.
"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Adrian,

1. If you are worried about client modifying the icon file do this

a) add the icon file in the solution
b) go to the properties and set Build Action to "Embedded resource" c) Rebuild your program.

your icon file will be now part of resource. To acess icon you can do
this

Assembly myAssembly =
Assembly.GetAssembly(Type.GetType("MyNameSpace.MyC lass"));
Stream iconStream =
myAssembly.GetManifestResourceStream("MyNameSpace. myIcon.ico");
this.Icon = new Icon(icoStream);

2. When you select icon in the properties of form, you will see it
immediately in design mode. If thats not happening, there might be problem
in ur icon file or may be something else. Try loading the icon in
imagelist. It may give u a clue.
--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:57***************************@freeler.nl...
Hi Shak,

Thank you. That works!

I have two questions if I may:

(1) How do you get around the problem of the client
changing the file in which the application should be installed?
(Yes, I can do an "if exists", but that doesn't solve the problem.)

(2) I have the icon showing in the "Properties" and in the prog
I have this.Icon = ((System.Drawing ... ("$this.icon))); This is
the same as I have in other applications. There I get an icon
showing up, here I don't. Might you please have an explanation?

Many thanks.

**********************************************

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
If you have .ico file

this.Icon = new Icon(@"c:\myIcon.ico");

--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:17***************************@freeler.nl...
> How do I put an icon on a form using code?
> Thank you.
>
>



Nov 16 '05 #5

P: n/a
Adrain

1. After adding icon file into the solution, right click and select
properties. You will see build action which will have default value as
"Content" in properties. Change to embedded resource and follow the code i
gave you in the last post, to read the icon stream.

To explain with different example, when you create a c# application by
default, it creates "App.ico". Right click on that file and check the
properties. You will find "Build Action" in the property grid.

2. What is your Form BorderStyle? You wont see icon, for certain
borderstyles of the form. Try changing that property. I cannot think of any
other possibilities for not seeing the icon in ur Form.

--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:e3**************************@freeler.nl...
Hi Shak,

(1.a.) I wasn't worried about the client changing the icon; I was worried
about the client installing the application in a different directory so the icon wouldn't be found. On second thought I realize that is a good way
to check whether the program is being properly installed. It is an
invoicing ("billing") program that has to work together with an accounting
program, which should have been installed already by the client.
(1.b.) I cannot find the Build Action in any properties box listing.

(2.) The icon is showing up ok in the properties box listing, hence
I was puzzled as to the cause of the icon not appearing on the
form when run. I have done no different in other programs where
there isn't an icon problem. This problem is arizing in the last two
applications I am working on.

Might you have any further suggestions?
Many thanks.
"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Adrian,

1. If you are worried about client modifying the icon file do this

a) add the icon file in the solution
b) go to the properties and set Build Action to "Embedded

resource"
c) Rebuild your program.

your icon file will be now part of resource. To acess icon you can do this

Assembly myAssembly =
Assembly.GetAssembly(Type.GetType("MyNameSpace.MyC lass"));
Stream iconStream =
myAssembly.GetManifestResourceStream("MyNameSpace. myIcon.ico");
this.Icon = new Icon(icoStream);

2. When you select icon in the properties of form, you will see it
immediately in design mode. If thats not happening, there might be problem in ur icon file or may be something else. Try loading the icon in
imagelist. It may give u a clue.
--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:57***************************@freeler.nl...
Hi Shak,

Thank you. That works!

I have two questions if I may:

(1) How do you get around the problem of the client
changing the file in which the application should be installed?
(Yes, I can do an "if exists", but that doesn't solve the problem.)

(2) I have the icon showing in the "Properties" and in the prog
I have this.Icon = ((System.Drawing ... ("$this.icon))); This is
the same as I have in other applications. There I get an icon
showing up, here I don't. Might you please have an explanation?

Many thanks.

**********************************************

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
> If you have .ico file
>
> this.Icon = new Icon(@"c:\myIcon.ico");
>
> --
> Shak
> (Houston)
>
>
> "Adrian" <aa@aa.aa> wrote in message
> news:17***************************@freeler.nl...
> > How do I put an icon on a form using code?
> > Thank you.
> >
> >
>
>



Nov 16 '05 #6

P: n/a
To add more on (2) if you have ControlBox property set to false in form, you
wont see the icon.
--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:e3**************************@freeler.nl...
Hi Shak,

(1.a.) I wasn't worried about the client changing the icon; I was worried
about the client installing the application in a different directory so the icon wouldn't be found. On second thought I realize that is a good way
to check whether the program is being properly installed. It is an
invoicing ("billing") program that has to work together with an accounting
program, which should have been installed already by the client.
(1.b.) I cannot find the Build Action in any properties box listing.

(2.) The icon is showing up ok in the properties box listing, hence
I was puzzled as to the cause of the icon not appearing on the
form when run. I have done no different in other programs where
there isn't an icon problem. This problem is arizing in the last two
applications I am working on.

Might you have any further suggestions?
Many thanks.
"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Adrian,

1. If you are worried about client modifying the icon file do this

a) add the icon file in the solution
b) go to the properties and set Build Action to "Embedded

resource"
c) Rebuild your program.

your icon file will be now part of resource. To acess icon you can do this

Assembly myAssembly =
Assembly.GetAssembly(Type.GetType("MyNameSpace.MyC lass"));
Stream iconStream =
myAssembly.GetManifestResourceStream("MyNameSpace. myIcon.ico");
this.Icon = new Icon(icoStream);

2. When you select icon in the properties of form, you will see it
immediately in design mode. If thats not happening, there might be problem in ur icon file or may be something else. Try loading the icon in
imagelist. It may give u a clue.
--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:57***************************@freeler.nl...
Hi Shak,

Thank you. That works!

I have two questions if I may:

(1) How do you get around the problem of the client
changing the file in which the application should be installed?
(Yes, I can do an "if exists", but that doesn't solve the problem.)

(2) I have the icon showing in the "Properties" and in the prog
I have this.Icon = ((System.Drawing ... ("$this.icon))); This is
the same as I have in other applications. There I get an icon
showing up, here I don't. Might you please have an explanation?

Many thanks.

**********************************************

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:ez**************@TK2MSFTNGP10.phx.gbl...
> If you have .ico file
>
> this.Icon = new Icon(@"c:\myIcon.ico");
>
> --
> Shak
> (Houston)
>
>
> "Adrian" <aa@aa.aa> wrote in message
> news:17***************************@freeler.nl...
> > How do I put an icon on a form using code?
> > Thank you.
> >
> >
>
>



Nov 16 '05 #7

P: n/a

Hi Shak,

Not out of the woods yet I fear.

(1.)
I have included your code.
I have put it in myForm_Load
No compile-time errors
No run-time errors
But no icon on the form.

(2.)
I have the border style set to FixedSingle
ControlBox is showing
In the properties list for myForm, the icon is displayed

For some funny reason one of the classes suddenly couldn't
be found either so I had to create a new class and give it the
code of the original class. That solved that one.

So I did the same with the icon file. That didn't help.

Is there something spooky going on here?

Adrian.
*********************************************
"Shakir Hussain" <sh**@nodomain.com> wrote in message
news:OH**************@TK2MSFTNGP09.phx.gbl...
Adrain

1. After adding icon file into the solution, right click and select
properties. You will see build action which will have default value as
"Content" in properties. Change to embedded resource and follow the code i
gave you in the last post, to read the icon stream.

To explain with different example, when you create a c# application by
default, it creates "App.ico". Right click on that file and check the
properties. You will find "Build Action" in the property grid.

2. What is your Form BorderStyle? You wont see icon, for certain
borderstyles of the form. Try changing that property. I cannot think of any other possibilities for not seeing the icon in ur Form.

--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:e3**************************@freeler.nl...
Hi Shak,

(1.a.) I wasn't worried about the client changing the icon; I was worried
about the client installing the application in a different directory so

the
icon wouldn't be found. On second thought I realize that is a good way
to check whether the program is being properly installed. It is an
invoicing ("billing") program that has to work together with an accounting program, which should have been installed already by the client.
(1.b.) I cannot find the Build Action in any properties box listing.

(2.) The icon is showing up ok in the properties box listing, hence
I was puzzled as to the cause of the icon not appearing on the
form when run. I have done no different in other programs where
there isn't an icon problem. This problem is arizing in the last two
applications I am working on.

Might you have any further suggestions?
Many thanks.
"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Adrian,

1. If you are worried about client modifying the icon file do this

a) add the icon file in the solution
b) go to the properties and set Build Action to "Embedded

resource"
c) Rebuild your program.

your icon file will be now part of resource. To acess icon you can

do this

Assembly myAssembly =
Assembly.GetAssembly(Type.GetType("MyNameSpace.MyC lass"));
Stream iconStream =
myAssembly.GetManifestResourceStream("MyNameSpace. myIcon.ico");
this.Icon = new Icon(icoStream);

2. When you select icon in the properties of form, you will see it
immediately in design mode. If thats not happening, there might be problem in ur icon file or may be something else. Try loading the icon in
imagelist. It may give u a clue.
--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:57***************************@freeler.nl...
> Hi Shak,
>
> Thank you. That works!
>
> I have two questions if I may:
>
> (1) How do you get around the problem of the client
> changing the file in which the application should be installed?
> (Yes, I can do an "if exists", but that doesn't solve the problem.)
>
> (2) I have the icon showing in the "Properties" and in the prog
> I have this.Icon = ((System.Drawing ... ("$this.icon))); This is
> the same as I have in other applications. There I get an icon
> showing up, here I don't. Might you please have an explanation?
>
> Many thanks.
>
> **********************************************
>
> "Shakir Hussain" <sh**@fakedomain.com> wrote in message
> news:ez**************@TK2MSFTNGP10.phx.gbl...
> > If you have .ico file
> >
> > this.Icon = new Icon(@"c:\myIcon.ico");
> >
> > --
> > Shak
> > (Houston)
> >
> >
> > "Adrian" <aa@aa.aa> wrote in message
> > news:17***************************@freeler.nl...
> > > How do I put an icon on a form using code?
> > > Thank you.
> > >
> > >
> >
> >
>
>




Nov 16 '05 #8

P: n/a
Can you send me the project to sh***********@yahoo.com? I will investigate
wats going on. Zip the project if you can.

--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:c4***************************@freeler.nl...

Hi Shak,

Not out of the woods yet I fear.

(1.)
I have included your code.
I have put it in myForm_Load
No compile-time errors
No run-time errors
But no icon on the form.

(2.)
I have the border style set to FixedSingle
ControlBox is showing
In the properties list for myForm, the icon is displayed

For some funny reason one of the classes suddenly couldn't
be found either so I had to create a new class and give it the
code of the original class. That solved that one.

So I did the same with the icon file. That didn't help.

Is there something spooky going on here?

Adrian.
*********************************************
"Shakir Hussain" <sh**@nodomain.com> wrote in message
news:OH**************@TK2MSFTNGP09.phx.gbl...
Adrain

1. After adding icon file into the solution, right click and select
properties. You will see build action which will have default value as
"Content" in properties. Change to embedded resource and follow the code i
gave you in the last post, to read the icon stream.

To explain with different example, when you create a c# application by
default, it creates "App.ico". Right click on that file and check the
properties. You will find "Build Action" in the property grid.

2. What is your Form BorderStyle? You wont see icon, for certain
borderstyles of the form. Try changing that property. I cannot think of

any
other possibilities for not seeing the icon in ur Form.

--
Shak
(Houston)
"Adrian" <aa@aa.aa> wrote in message
news:e3**************************@freeler.nl...
Hi Shak,

(1.a.) I wasn't worried about the client changing the icon; I was worried about the client installing the application in a different directory so the
icon wouldn't be found. On second thought I realize that is a good way
to check whether the program is being properly installed. It is an
invoicing ("billing") program that has to work together with an accounting program, which should have been installed already by the client.
(1.b.) I cannot find the Build Action in any properties box listing.

(2.) The icon is showing up ok in the properties box listing, hence
I was puzzled as to the cause of the icon not appearing on the
form when run. I have done no different in other programs where
there isn't an icon problem. This problem is arizing in the last two
applications I am working on.

Might you have any further suggestions?
Many thanks.
"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
> Adrian,
>
> 1. If you are worried about client modifying the icon file do this
>
> a) add the icon file in the solution
> b) go to the properties and set Build Action to "Embedded
resource"
> c) Rebuild your program.
>
> your icon file will be now part of resource. To acess icon you
can do
> this
>
> Assembly myAssembly =
> Assembly.GetAssembly(Type.GetType("MyNameSpace.MyC lass"));
> Stream iconStream =
> myAssembly.GetManifestResourceStream("MyNameSpace. myIcon.ico");
> this.Icon = new Icon(icoStream);
>
> 2. When you select icon in the properties of form, you will see it
> immediately in design mode. If thats not happening, there might be

problem
> in ur icon file or may be something else. Try loading the icon in
> imagelist. It may give u a clue.
>
>
> --
> Shak
> (Houston)
>
>
> "Adrian" <aa@aa.aa> wrote in message
> news:57***************************@freeler.nl...
> > Hi Shak,
> >
> > Thank you. That works!
> >
> > I have two questions if I may:
> >
> > (1) How do you get around the problem of the client
> > changing the file in which the application should be installed?
> > (Yes, I can do an "if exists", but that doesn't solve the

problem.) > >
> > (2) I have the icon showing in the "Properties" and in the prog
> > I have this.Icon = ((System.Drawing ... ("$this.icon))); This is
> > the same as I have in other applications. There I get an icon
> > showing up, here I don't. Might you please have an explanation?
> >
> > Many thanks.
> >
> > **********************************************
> >
> > "Shakir Hussain" <sh**@fakedomain.com> wrote in message
> > news:ez**************@TK2MSFTNGP10.phx.gbl...
> > > If you have .ico file
> > >
> > > this.Icon = new Icon(@"c:\myIcon.ico");
> > >
> > > --
> > > Shak
> > > (Houston)
> > >
> > >
> > > "Adrian" <aa@aa.aa> wrote in message
> > > news:17***************************@freeler.nl...
> > > > How do I put an icon on a form using code?
> > > > Thank you.
> > > >
> > > >
> > >
> > >
> >
> >
>
>



Nov 16 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.