User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 391,666 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,878 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 557 | Replies: 2 | Solved
Reply
Join Date: Jan 2008
Posts: 1,403
Reputation: VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough 
Rep Power: 6
Solved Threads: 179
VernonDozier VernonDozier is offline Offline
Nearly a Posting Virtuoso

Cannot see console output in Visual C++

  #1  
May 22nd, 2008
I am trying to learn Visual C++, particularly the GUI components, so I tried to make a simple program where a button is displayed to a frame and when the button is pressed, a cout statement is invoked which displays a message to the console saying that the button has been pressed. I created a new project of type "Windows Forms Application", then dragged a button onto the GUI, brought up the properties and added a "button_1_Click" function. Within that function I added a simple cout statement. The problem is that I see nothing displayed when I click that button. Here is my code (auto-generated except for a couple of lines - cout line is line 89):

  1. #pragma once
  2.  
  3. #include <iostream>
  4.  
  5.  
  6. namespace trialprogram1
  7. {
  8. using namespace System;
  9. using namespace System::ComponentModel;
  10. using namespace System::Collections;
  11. using namespace System::Windows::Forms;
  12. using namespace System::Data;
  13. using namespace System::Drawing;
  14.  
  15. /// <summary>
  16. /// Summary for Form1
  17. ///
  18. /// WARNING: If you change the name of this class, you will need to change the
  19. /// 'Resource File Name' property for the managed resource compiler tool
  20. /// associated with all .resx files this class depends on. Otherwise,
  21. /// the designers will not be able to interact properly with localized
  22. /// resources associated with this form.
  23. /// </summary>
  24. public ref class Form1 : public System::Windows::Forms::Form
  25. {
  26. public:
  27. Form1(void)
  28. {
  29. InitializeComponent();
  30. //
  31. //TODO: Add the constructor code here
  32. //
  33. }
  34.  
  35. protected:
  36. /// <summary>
  37. /// Clean up any resources being used.
  38. /// </summary>
  39. ~Form1()
  40. {
  41. if (components)
  42. {
  43. delete components;
  44. }
  45. }
  46. private: System::Windows::Forms::Button^ button1;
  47. protected:
  48.  
  49. private:
  50. /// <summary>
  51. /// Required designer variable.
  52. /// </summary>
  53. System::ComponentModel::Container ^components;
  54.  
  55. #pragma region Windows Form Designer generated code
  56. /// <summary>
  57. /// Required method for Designer support - do not modify
  58. /// the contents of this method with the code editor.
  59. /// </summary>
  60. void InitializeComponent(void)
  61. {
  62. this->button1 = (gcnew System::Windows::Forms::Button());
  63. this->SuspendLayout();
  64. //
  65. // button1
  66. //
  67. this->button1->Location = System::Drawing::Point(39, 23);
  68. this->button1->Name = L"button1";
  69. this->button1->Size = System::Drawing::Size(75, 23);
  70. this->button1->TabIndex = 0;
  71. this->button1->Text = L"button1";
  72. this->button1->UseVisualStyleBackColor = true;
  73. this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
  74. //
  75. // Form1
  76. //
  77. this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
  78. this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  79. this->ClientSize = System::Drawing::Size(292, 266);
  80. this->Controls->Add(this->button1);
  81. this->Name = L"Form1";
  82. this->Text = L"Form1";
  83. this->ResumeLayout(false);
  84.  
  85. }
  86. #pragma endregion
  87. private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
  88. {
  89. std::cout << "button1 has been pushed\n";
  90. }
  91. };
  92. }
  93.  

  1. // trialprogram1.cpp : main project file.
  2.  
  3. #include "stdafx.h"
  4. #include "Form1.h"
  5.  
  6. using namespace trialprogram1;
  7.  
  8. [STAThreadAttribute]
  9. int main(array<System::String ^> ^args)
  10. {
  11. // Enabling Windows XP visual effects before any controls are created
  12. Application::EnableVisualStyles();
  13. Application::SetCompatibleTextRenderingDefault(false);
  14.  
  15. // Create the main window and run it
  16. Application::Run(gcnew Form1());
  17. return 0;
  18. }

I put a breakpoint on line 89 to make sure that function is being called when the button is pressed, and it is. However, I see no output when I click this button.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,809
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 11
Solved Threads: 184
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Cannot see console output in Visual C++

  #2  
May 22nd, 2008
You should have gotten a runtime exception for trying it...

Anyway, Windows apps don't have consoles by default. At the beginning of your program you'll have to
AllocConsole();

then... you'll have to hook the standard streams up to the console:
http://www.halcyon.com/~ast/dload/guicon.htm
(yoinks!)
Its easier than it looks.

Have fun.
Reply With Quote  
Join Date: Jan 2008
Posts: 1,403
Reputation: VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough VernonDozier is a jewel in the rough 
Rep Power: 6
Solved Threads: 179
VernonDozier VernonDozier is offline Offline
Nearly a Posting Virtuoso

Re: Cannot see console output in Visual C++

  #3  
May 22nd, 2008
Originally Posted by Duoas View Post
You should have gotten a runtime exception for trying it...

Anyway, Windows apps don't have consoles by default. At the beginning of your program you'll have to
AllocConsole();

then... you'll have to hook the standard streams up to the console:
http://www.halcyon.com/~ast/dload/guicon.htm
(yoinks!)
Its easier than it looks.

Have fun.

All right, that worked! And on the first try for once. It was interesting that this was mostly C code in that link. Basically I cut and pasted guicon.h and guicon.cpp verbatim, then added a call to RedirectToConsole () before any cout statements. I may fiddle with it, but then again, if it ain't broke, don't fix it. Thanks for the link.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 1:49 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC