Updating a Form From a seperate class

I have been looking around for two days on how to simply update the text field of a RichTextBox on a form called Form1 from a separate class called Chat_Commands.

I am trying to play with some coding for c++ GUI as i have not learned it and only done basic C++ in school. What i wanted to do was have a separate file with a new class to control a TCP chat server.

The form code is:

Form1.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  #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;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}
		void delPassData(System::String^ text);

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}

		void ctrl_Update(System::String^ text)
		{
			rtbConsole->Text += text + Environment::NewLine;
		}

	private: System::Windows::Forms::RichTextBox^  rtbConsole;
	protected: 

	private: System::Windows::Forms::TextBox^  txtCommands;
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->rtbConsole = (gcnew System::Windows::Forms::RichTextBox());
			this->txtCommands = (gcnew System::Windows::Forms::TextBox());
			this->SuspendLayout();
			// 
			// rtbConsole
			// 
			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"";
			// 
			// txtCommands
			// 
			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->TextChanged += gcnew System::EventHandler(this, &Form1::txtCommands_TextChanged);
			this->txtCommands->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &Form1::txtCommands_KeyDown);

			// 
			// Form1
			// 
			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^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e )
		{
				
					// Determine whether the keystroke is an Enter
					if ( e->KeyCode == Keys::Return )
					{
						CCommands.command_Selection(txtCommands->Text);
						// DEBUG: Change text of rtbConsole to the value of txtCommands
						//rtbConsole->Text += console_Data + Environment::NewLine;
						txtCommands->Text = "";
					}
				
		}
				
	};

	void Form1::delPassData(System::String^ text)
	{
		rtbConsole->Text = text;
	}
}

Commands.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
namespace ChatCommands {

	using namespace System::Windows::Forms;
	using namespace GUI_Chat;

	public ref class Chat_Commands
	{

		enum class caseDirection { upper, lower };

		// Private section
	private:
		
		// Public section
	public:
		
		//void GUI_Chat_Commands(void);
		Chat_Commands(void);
		void command_Selection(System::String^ command);
		char change_Case(char letter, caseDirection caseType);
		void display_Usage(void);
		

	};

	//enum caseDirection { upper, lower };	
	// Constructor
	Chat_Commands::Chat_Commands() { }

	// Determine what command is being entered.
	void Chat_Commands::command_Selection(System::String^ command)
	{
		System::String ^lowerCommand;
		
		for each (char letter in command)
		{
			lowerCommand += (wchar_t) change_Case(letter, caseDirection::lower);
		}

		if ("exit" == lowerCommand)
		{
			Application::Exit();
		}
		if ("?" == lowerCommand)
		{
			display_Usage();
		}
	}

	char Chat_Commands::change_Case(char letter, caseDirection caseType )
	{
		switch (caseType)
		{
		case caseDirection::upper:
			// This changes to uppercase
			if (letter > 96 && letter < 122)  // Check if the letter is lowercase
			{
				letter -= 32;	// Change the letter to uppercase.
			}

			break;
		case caseDirection::lower:
			// This changes to Lowercase
			if (letter > 'A' && letter < 'Z') // Check if the letter is uppercase
			{
				letter += 32;
			}
			
			break;
		default:
			break;
		}
		return letter;
				
	}
	/*
	ref class ManagedGlobals {
		public:
		static Form1^ mainForm = gcnew Form1;	

	};
	*/
	void Chat_Commands::display_Usage(void)
	{
		Form1^ frm1 = gcnew Form1;
		// **** TODO: Impliment some way of being able to intereact with the rtbConsole control on Form1.
		//ManagedGlobals::mainForm->update_rtbConsole("Usage");
		//<Form1^>* pointer = (gcroot<ManagedClass^>*)(input);
		frm1->delPassData("Usage");

	}

};


Can anyone tell me how to get the helper class to update data on the form in Visual studio 2012?

As you can see in the function at the bottom of Commands.h I just simply want to send the text back to a function on Form1 that will update the control.
This is not C++, this is C++/CLI, which is basically a Microsoft-tainted C++. Have you tried looking as Microsoft's resources? MSDN is a good (but confusing) place to look.
I would much rather practice with true C++ any suggestions on where to get some reference material such as a ebook for learning GUI programming?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Foo {
  int x;
public:
  void set( int y ) { x = y; }
};

class Bar {
public:
  void gaz( Foo & f, int y ) { f.set( y ); }
};

int main()
{
  Foo z;
  Bar w;
  w.gaz( z, 42 );
  return 0;
}

Foo is a type of objects that have feature x. The x could be "content of a text field". The interface of Foo allows manipulation of the feature. Surely, the text field of RichTextBox does not change automagickally; someone uses its interface.

Bar is a type of objects too. It happens to encapsulate a procedure gaz, which accesses (uses) the interface of the type Foo objects.

If you have a "form", then it must somehow reference its "boxes". The program must have a list of its forms. Nothing should prevent a procedure to put stuff into a box that it finds from a form, if it can find the form from the application. The boxes being GUI elements is irrelevant (unless the interface of the GUI elements is bizarre).
Topic archived. No new replies allowed.