C++ : Appending text in form

标签 c++ winforms forms textbox clr

我已经离开 C++ 一段时间了,这可能只是我的愚蠢,但为什么这会给我一个错误(代码下方的错误)。

代码:

// NetworkServer.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"
#include <winsock2.h>
#include <iostream>
using namespace std;
using namespace NetworkServer;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false); 

// Create the main window and run it
Application::Run(gcnew Form1());


    public void setUsers()
{
    string connectedUsers[] = {"John", "Alex", "Phillip", "Steve"};
    Form1->txt_connectedClients.AppendText(connectedUsers[1]);
}

    return 0;
}

错误:

    1>NetworkServer.cpp(22): error C2143: syntax error : missing ';' before '->'
    1>NetworkServer.cpp(22): error C2143: syntax error : missing ';' before '->'

最佳答案

Form1 是一个类型 名称,您需要一个对象。我看不到代码的上下文,但只要这段代码是在 Form1 类的方法中编写的,那么 this-> 就可以工作。

public ref class Form1 : public System::Windows::Forms::Form
{
   //...
public:
    void setUsers() {
        array<String^>^ connectedUsers = gcnew array<String^> {"John", "Alex", "Phillip", "Steve"};
        this->txt_connectedClients->AppendText(connectedUsers[1]);
    }
};

请注意,您使用的是 C++/CLI 语言,而不是 C++。

关于C++ : Appending text in form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5544625/

相关文章:

c# - 我的绑定(bind)源是否可以告诉我是否发生了更改?

php - MD5 javascript 实现

php - 将垃圾邮件机器人发送到黑洞

javascript - 如何从 HTML 表单中获取键/值数据集

c++ - 有什么方法可以使用 ASYNC MessageBox 吗?

c++ - Openmp渲染bmp图片

c# - Windows 窗体 DataGridView 将 SelectedIndexChanged 事件附加到 ComboBox

winforms - 在Visual Basic Windows窗体中包含媒体

c++ - 从模板函数有条件地返回不同类型的正确方法

C++ UTF-8 实际字符串长度