Windows Forms Program

Hello everyone,

I am trying to write a program an I've gotten stuck. It is fairly simple. I have a windows form and a 'classTester01' class. What I am trying to do is make it so that when the button is clicked, the text on the button is changed to the value in the variable 'buffer'. The problem is I keep getting these errors. I have included my code and I appreciate any help you can offer.

I would also like to know if this is the best way to do it; having one class for the character and using the windows form to manipulate the data in an instance of that class.

Thanks again,

c:\users\support\documents\visual studio 2010\projects\classtest01\classtest01\classTester01.h(16): error C4368: cannot define 'buffer' as a member of managed 'classTester01': mixed types are not supported

c:\users\support\documents\visual studio 2010\projects\classtest01\classtest01\mainForm.h(96): error C2678: binary '=' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)

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
// ClassTest01.cpp : main project file.

#include "stdafx.h"
#include <iostream>
#include "classTester01.h"
#include "mainForm.h"

using namespace std;
using namespace ClassTest01;

int main(array<System::String ^> ^args)
{
    cout << "Hello World";
	classTester01^ newTester = gcnew classTester01;

	mainForm^ newForm = gcnew mainForm();	
	newForm->ShowDialog();

	cout << endl << newTester->displayNumber() << endl;
	newTester->numberChanger();

	cout << newTester->displayNumber() << endl;

	cin.get();
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once

#include <string>

using namespace std;

ref class classTester01
{
public:
	classTester01(void);
	void numberChanger(void);
	int displayNumber(void);
	std::string convertInt(int);

	int number;
	string buffer;
};


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

#include "StdAfx.h"
#include "classTester01.h"
#include <string>
#include <sstream>


classTester01::classTester01(void)
{
	number = 7;
	buffer = "Clicked";
}

void classTester01::numberChanger(void)
{
	number = 21;
}

int classTester01::displayNumber(void)
{
	return number;
}

std::string convertInt(int num)
{
    std::ostringstream ostr; //output string stream
    ostr << num; //use the string stream just like cout,
    //except the stream prints not to stdout but to a string.

    std::string theNumberString = ostr.str(); //the str() function of the stream 

	return theNumberString;
    //returns the string.
}


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

#pragma once

#include "classTester01.h"
#include <string>
#include <stdlib.h>
#include <sstream>

namespace ClassTest01 {

	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 std;

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

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~mainForm()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	private: System::Diagnostics::EventLog^  eventLog1;
	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->button1 = (gcnew System::Windows::Forms::Button());
			this->eventLog1 = (gcnew System::Diagnostics::EventLog());
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->eventLog1))->BeginInit();
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(101, 127);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &mainForm::button1_Click);
			// 
			// eventLog1
			// 
			this->eventLog1->SynchronizingObject = this;
			// 
			// mainForm
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(292, 273);
			this->Controls->Add(this->button1);
			this->Name = L"mainForm";
			this->Text = L"mainForm";
			(cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->eventLog1))->EndInit();
			this->ResumeLayout(false);

		}
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

				 classTester01^ newTester = gcnew classTester01;
				 
				 newTester->buffer = newTester->convertInt(12);

				 button1->Text = newTester->buffer;

			 }
	};
}

Your classTester01 class is a managed class and it seems that it cannot hold std::string datatype.

The solution is probably to either stick with standard C++ language (drop support for C++/CLI Mucrosoft proprietary language) or use C++/CLI own string class.
Thanks modoran,

I'm not sure how or why the class is managed but it wasn't my intention ( I am quite green...). How do I 'stick with standard C++ language'? Does that mean I can't use window like object and must use the console?
You can mix managed and unmanaged code, but it can be tricky. Change the declaration of buffer to a std::wstring* (System::Strings don't have a ctor to convert from a const char* but does for const wchar_t*) and initialize it in the ctor.
Also button1->Text = newTester->buffer; this won't work, you need to convert a std::string to a System::String like
1
2
3
*buffer = L"test";
System::String^ b = gcnew System::String(buffer->c_str());
((Button^)sender)->Text = b;
where buffer is a std::wstring*
Last edited on
Topic archived. No new replies allowed.