I am working on a small Chat application to play with Visual studio 2012 and GUI programming. I have taken some classes in c++ but they only cover console programming, and while i am able to get the application working in a console i would like to implement a GUI front end.
That being said i have created the server section of the chat application. It has a simple GUI with a console and a command text box.
I am trying to write in the code to update the console window of the GUI however when i create a secondary class to modify the form i am unable to access the controls.
This is the form1.h file that i am using.and this is the helper class to work with Server commands.
#pragma once
namespace GUI_Chat {
#include "Commands.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace ChatCommands;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
static Form1^ form1_Functions;
Form1(void)
{
InitializeComponent();
form1_Functions = this;
}
void update_Console(System::String^ text)
{
rtbConsole->Text += text + Environment::NewLine;
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::RichTextBox^ rtbConsole;
protected:
private: System::Windows::Forms::TextBox^ txtCommands;
protected:
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->rtbConsole = (gcnew System::Windows::Forms::RichTextBox());
this->txtCommands = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
this->rtbConsole->Enabled = false;
this->rtbConsole->Location = System::Drawing::Point(0, 0);
this->rtbConsole->Name = L"rtbConsole";
this->rtbConsole->Size = System::Drawing::Size(554, 235);
this->rtbConsole->TabIndex = 0;
this->rtbConsole->Text = L"";
this->txtCommands->Location = System::Drawing::Point(0, 241);
this->txtCommands->Name = L"txtCommands";
this->txtCommands->Size = System::Drawing::Size(554, 20);
this->txtCommands->TabIndex = 1;
this->txtCommands->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::txtCommands_KeyDown);
this->AccessibleName = L"frmServer";
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::Black;
this->ClientSize = System::Drawing::Size(554, 261);
this->ControlBox = false;
this->Controls->Add(this->txtCommands);
this->Controls->Add(this->rtbConsole);
this->MaximumSize = System::Drawing::Size(570, 300);
this->MinimizeBox = false;
this->MinimumSize = System::Drawing::Size(570, 300);
this->Name = L"Form1";
this->SizeGripStyle = System::Windows::Forms::SizeGripStyle::Hide;
this->Text = L"Server";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
Chat_Commands CCommands;
void txtCommands_KeyDown( Object^ , System::Windows::Forms::KeyEventArgs^ e )
{
if ( e->KeyCode == Keys::Return )
{
CCommands.command_Selection(txtCommands->Text);
rtbConsole->Text += txtCommands->Text + Environment::NewLine;
txtCommands->Text = "";
}
}
};
}
And this is the Helper File
#pragma managed
namespace ChatCommands {
using namespace System::Windows::Forms;
using namespace GUI_Chat;
public ref class Chat_Commands
{
private:
public:
Chat_Commands(void);
void command_Selection(System::String^ command);
char change_Case(char letter);
void display_Usage(void);
};
Chat_Commands::Chat_Commands()
{
}
void Chat_Commands::command_Selection(System::String^ command)
{
System::String ^lowerCommand;
for each (char letter in command)
{
lowerCommand += (wchar_t) change_Case(letter);
}
if ("exit" == lowerCommand)
{
Application::Exit();
}
if ("?" == lowerCommand)
{
display_Usage();
}
}
char Chat_Commands::change_Case(char letter)
{
if (letter > 'A' && letter < 'Z') {
letter += 32;
}
return letter;
}
void display_Usage(void)
{
Form1::form1_Functions->update_Console("Usage");
}
}
There are allot of commented out lines as i have been working with this for a while and trying different things.
I tried many different ways of resolving this and searched for hours on Google.
Any help that anyone can provide would be appreciated, also any links to GUI programming tutorials would be helpfull.
Thank you