Windows Develop Bookmark and Share   
 index > Windows Forms General > Exception if image from resource manager is not correct
 

Exception if image from resource manager is not correct

Hi,

I have used the below code to get a bitmap. from the resourcemanager. How can I throw an exception if the image name from the database does not correspond with the image name in resource manager? Currently it will just leave a transparent space on the toolstripbutton.
You can access resource by name through: ResourceManager.GetObject(String name);

For example:
Bitmap buttonA = (Bitmap)Properties.Resources.ResourceManager.GetObject("mybuttonA");


Scorgi  Tuesday, September 08, 2009 1:53 PM
Hi Scorgi,

The code snippet below shows three methods, you can only select one of them:
//1. Throw an exception.
throw new ArgumentException(msg);
//2. Show a message.
MessageBox.Show(msg);
//3. Bind a default bitmap to the button.
map = (Bitmap)Properties.Resources.ResourceManager.GetObject("default");

For example, if you want to show an exception if the image is null, please follow:
//1. Throw an exception.
throw new ArgumentException(msg);

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byScorgi Monday, September 21, 2009 7:40 AM
  •  
Aland Li  Monday, September 21, 2009 6:41 AM
Hi Scorgi,

You said Currently it will just leave a transparent space on the toolstripbutton.
It is because if a image does not exist, it would return null. In other words, the bitmap buttonA would be null.

From my experience, there are three ways to cope with such a case:
1. Throw an exception.
2. Show a message.
3. Bind a default bitmap to the button.
This is the code snippet:
Bitmap map = (Bitmap)Properties.Resources.ResourceManager.GetObject("mybuttonA");
if (map == null)
{
    string msg = "The image mybuttonA cannot be found!";
    //1. Throw an exception.
    throw new ArgumentException(msg);
    //2. Show a message.
    MessageBox.Show(msg);
    //3. Bind a default bitmap to the button.
    map = (Bitmap)Properties.Resources.ResourceManager.GetObject("default");
}
Let me know if this does not help.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Thursday, September 10, 2009 8:56 AM
Hi Aland,

Thank you very much!

I have used the property image as this.

 tsbtn.Image = (Bitmap)Properties.Resources.ResourceManager.GetObject(toolrtStrip.IconName);
When I try to use your code I get unreachable code messagebox.show.

Hope you can help.

I appreciate it very much.
Scorgi  Monday, September 21, 2009 6:24 AM
Hi Scorgi,

Can the image referenced by toolrtStrip.IconName be found in the main resources? Could you please provide more information about the error?

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Monday, September 21, 2009 6:27 AM
tsbtn.Image = (Bitmap)Properties.Resources.ResourceManager.GetObject(toolrtStrip.IconName);
                        
                        
                            if (tsbtn.Image == null)
                            {
                                string msg = string.Format("The image was not found! {0}", toolrtStrip.IconName);

                                throw new ArgumentException(msg);
                                MessageBox.Show(msg);
                                //3. Bind a default bitmap to the button.
                                tsbtn.Image = (Bitmap)Properties.Resources.ResourceManager.GetObject("default");


                            }
Hi Alan,

yes the image can be referenced by toolrrStrip.IconName. The total code is as follows.

I receive a warning for MessageBox.Show unreachable code detected.

Scorgi  Monday, September 21, 2009 6:38 AM
Hi Scorgi,

The code snippet below shows three methods, you can only select one of them:
//1. Throw an exception.
throw new ArgumentException(msg);
//2. Show a message.
MessageBox.Show(msg);
//3. Bind a default bitmap to the button.
map = (Bitmap)Properties.Resources.ResourceManager.GetObject("default");

For example, if you want to show an exception if the image is null, please follow:
//1. Throw an exception.
throw new ArgumentException(msg);

Regards,
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
  • Marked As Answer byScorgi Monday, September 21, 2009 7:40 AM
  •  
Aland Li  Monday, September 21, 2009 6:41 AM
Hi Aland,

Thank you very much
Scorgi  Monday, September 21, 2009 7:40 AM

You can use google to search for other answers

Custom Search

More Threads

• DataGridView and swopping between EditModes
• ActiveX Controls and license requests from .net
• disable back feature on WebBrowser control
• treeview control find third childnodes
• OverflowButton of toolStrip
• Form Resolution Issues
• Application hangs when sending strings out serial port. (Neeewbiiiie)
• PrintPreviewDialog zoom option in C#
• Controling minimum size of forms with custom form
• How to speed up datagridview