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
410 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
Comment Utility
Just use the ToString()

MyEnum.EnumItem.Tostring()
0
 
LVL 30

Accepted Solution

by:
Alexandre Simões earned 500 total points
Comment Utility
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
Comment Utility
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
 
LVL 30

Expert Comment

by:Alexandre Simões
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Closing Comment

by:John500
Comment Utility
Thanks again!
0
 

Author Comment

by:John500
Comment Utility
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
Comment Utility
Sorry, I guess that was the problem.

0
 
LVL 30

Expert Comment

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now