Solved

How to call an enum in order to obtain the message rather than the number associated with the message?

Posted on 2008-10-30
8
434 Views
Last Modified: 2013-12-17
Greetings:

I have a list of Windows messages looking something like this:

namespace AppObjects.WindowsUtilities
{
      public enum WindowsMessages: int
      {
            WM_NULL = 0x0000,
            WM_CREATE = 0x0001,
            WM_DESTROY = 0x0002,
            WM_MOVE = 0x0003,
            WM_SIZE = 0x0005,
            WM_ACTIVATE = 0x0006,
            WM_SETFOCUS = 0x0007,
            WM_KILLFOCUS = 0x0008,
                                ...
                                CCM_FIRST = 0x2000,                          // Common control shared messages
            CCM_LAST =(CCM_FIRST + 0x200),
            CCM_SETBKCOLOR = (CCM_FIRST + 1),
}

With a WndProc that looks like this:

protected override void WndProc(ref Message m)
        {
                 switch (m.Msg)
                {
                    case WM_ACTIVATEAPP:
                        // do something ....
                        break;
                    default:
                        break;
                }
            WriteToFile("C:\\log.txt", "Windows message:  " + m.Msg.ToString() );
            base.WndProc(ref m);
        }

... as it is now the message number is written out.  How would this change to write out the actual message instead of using AppObjects.WindowsUtilities?
What would the syntax be to declare and access the enum object?

Thanks!
0
Comment
Question by:John500
  • 4
  • 4
8 Comments
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 22844619
Just use the ToString()

MyEnum.EnumItem.Tostring()
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
ID: 22844692
The above solution only works if you know the enum item you want to display the name.
As you only have the message at the bottom of your code you need to get the item by value and the apply the ToString()

Like this:

Enum.GetName(typeof(WindowsMessages), m.Msg).ToString()
0
 

Author Comment

by:John500
ID: 22844803
Great!

Can you suggest how I'd handle the cases that can't be found?  In other words, if the message hasn't been entered in the list, how should I handle it to revert to the older procedure that writes only the message number?

Thanks!
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 22844948
Sure, when the value isn't found the return of that GetName is null, so use something like this:
			string s = Enum.GetName(typeof(WindowsMessages), m.Msg);
			if (s == null)
				MessageBox.Show("Message not found!");
			else
				MessageBox.Show(s);

Open in new window

0
 

Author Closing Comment

by:John500
ID: 31511820
Thanks again!
0
 

Author Comment

by:John500
ID: 22845264
I guess we didn't nail it just yet.  I'm getting the following exception:

The value passed in must be an enum base or an underlying type for an enum, such as an Int32.  Parameter name:  value

I get this for the following line:

string s = Enum.GetName(typeof(WindowsMessages), m.Msg);

actually I added to yours:

string s = Enum.GetName(typeof(WindowsMessages), m.Msg.ToString());

I'm guessing what I added is not the problem though.  Your thoughts?

0
 

Author Comment

by:John500
ID: 22845300
Sorry, I guess that was the problem.

0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 22846106
What's the type of m.Msg?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question