显示在文本框中

display in textbox

本文关键字:文本 显示      更新时间:2023-10-16

我正在尝试学习如何在VS中使用Windows表单应用程序,但我发现了一个问题。我习惯于基于控制台的应用程序。所以问题是:

我有一个表单,我想在文本框中显示属于另一个类的函数结果,我想在按下按钮时执行此操作。例如,这是一个示例类:

#ifndef PRUEBA_H
#define PRUEBA_H
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
class Prueba
{
public:
    void show() 
    {
       cout<<"Thanks"<<endl;
    }
};
#endif

这是按钮的代码:

#include "prueba.h"
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
       Prueba *x = new Prueba();
       textBox1->Text= System::Convert::ToString(x->show());
 }

编译器给了我这个错误

error C2665: 'System::Convert::ToString' : none of the 37 overloads could convert all the argument types    

任何人都可以提供帮助并发布在文本框中显示函数的正确方法吗?

void show() 
{
   cout<<"Thanks"<<endl;
}

此函数将一些文本打印到标准输出,但不返回任何内容。

你需要让它返回一个字符串。

您需要返回一个字符串,而不仅仅是打印到 stdout。像——

string show() 
{
   return "Thanks";
}

也代替

System::Convert::ToString(x->show());

你可能只需要

x->show();

就像其他人说的那样,您需要具有返回类型而不是打印到控制台中。

string show() 
{
    return "Thanks";
}

但是,您还想删除转换。

textBox1->Text = (x->show());

如果这仍然不起作用,那么我建议您尝试使用该函数设置另一个字符串,例如:

string v = x->show();
textBox1->Text = v;

并查看编译器出错的地方。

我终于得到了解决方案

#include <msclrmarshal.h>
#include <msclrmarshal_cppstd.h>
String^ s
s = marshal_as<String^>( what you want to put in the textbox );
textBox->Text += s + Environment::NewLine;