?
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
Medium Priority
?
461 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 2000 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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.
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

765 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