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
443 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

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 …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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