Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2007

Tip: Looking for answers? Try searching our database.

How to upgrade code from Console::WriteLine() to Message box

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Allen Maki - 02 Jun 2007 10:51 GMT
/*

Console::WriteLine() I am trying to use message box instead of
Console::WriteLine() to upgrade my code from just using the console to using
GUI.

In the bottom 2 lines, #1 works, but I have hard time to make # 2 works. Can
anybody give a hand here to make the information to be shown on the message
box instead of being shown on the console.

I am using visual C++ .NET 2003

Thanks

*/

#include "stdafx.h"

#include <gcroot.h>

#using <mscorlib.dll>

using namespace System;

using namespace System::Runtime::InteropServices;

typedef void* HWND;

[DllImportAttribute("User32.dll", CharSet=CharSet::Auto)]

extern "C" int MessageBox(HWND hw, String* text,

String* caption, unsigned int type);

__gc class MClass

{

public:

int val;

MClass(int n) //costructor

{

val = n;

}

};

class UClass

{

public :

gcroot<MClass*> mc;

UClass(MClass* pmc) //constructor

{

mc = pmc;

}

int getValue() //get function

{

return mc->val;

}

};

int _tmain()

{

Console::WriteLine(S"Testing...");

//Create a managed object and assign 3 to it

MClass* pm = new MClass (3);

//Create an unmanaged object and assign the value of the manged object (pm)
to it

UClass uc(pm);

Console::WriteLine(S"Value is {0}", __box(uc.getValue())); //<-------- This
will work # 1

//MessageBox(0, uc.getValue(), S"Message Box...", 0 ); //<------- this will
not work # 2

return 0;

}
Peter Duniho - 02 Jun 2007 11:46 GMT
> [...]
> //MessageBox(0, uc.getValue(), S"Message Box...", 0 ); //<------- this  
> will
> not work # 2

There are a variety of ways to do what you want, but the basic idea is  
this: you need to pass a fully-formed string to the MessageBox.Show()  
method.  Off the top of my head, something like this should work:

    MessageBox.Show(String.Format(S"Value is {0}", __box(uc.getValue())));

Please note that I do very little managed C++ stuff; there may be a more  
readable way to write the above.  I pretty much just based it on what you  
already wrote, along with what I know about MessageBox.  But hopefully you  
get the idea.

Pete
Morten Wennevik [C# MVP] - 02 Jun 2007 18:43 GMT
Hi,

I cannot comment on your code as I have never done managed C++, but to go from a console application to a windows application, you need to reference System.Windows.Forms.dll, inherit from System.Windows.Forms.Form, attach a message loop (in C# done with Application.Run(new MyForm()); from the static Main method), and output the compiled code as winexe (it will still run if you compile it to a console application but it will then open a console window before the WinForm shows).

Signature

Happy coding!
Morten Wennevik [C# MVP]


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.