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
425 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

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The greatest common divisor (gcd) of two positive integers is their largest common divisor. Let's consider two numbers 12 and 20. The divisors of 12 are 1, 2, 3, 4, 6, 12 The divisors of 20 are 1, 2, 4, 5, 10 20 The highest number among the c…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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