我刚接触C++/CLI并使用WinForms。我有一个GUI程序来运行C可执行文件(使用Visual Code g编译和构建),它从.dat文件中获取输入(整型、双精度、字符型bool)。使用GUI,您可以编辑输入。这是通过使用ToolStripMenu单击并打开辅助WinForm来完成的。我使用ToolStripMenu是因为我将有多个WinForm来编辑输入。基本设置为:
我的表单.cpp
#include <fstream>
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThread]
void main(array<String^>^ args) {
std::ifstream myfile1("datafile1.dat");
if (myfile1.is_open()) {
myfile >> [...];
// This is where the vars are defined.
// They are declared only in separate header files.
}
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Project_name::MyForm form;
Application::Run(% form);
}
我的表单.h
#pragma once
#include "ParForm.h" // WinForm to open using ToolStripMenuItem
namespace Project_name {
// All the code for all Controls
// This is where the ToolStripMenu is created with its Items
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
System::Diagnostics::Process::Start("PATH\\rooster.exe");
}
private: System::Void ToolStripMenuItem1_Click(System::Object^ sender, System::EventArgs^ e) {
ParForm ^Par = gcnew ParForm();
Par->ShowDialog();
}
}
参数格式.h
#pragma once
#include "parameters.h" // This is where the vars are declared
namespace Project_name {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class ParForm : public System::Windows::Forms::Form {
public:
ParForm(void) {
InitializeComponent();
}
protected:
~ParForm() {
if (components) {
delete components;
}
}
private: System::Windows::Forms::TextBox^ TextBox1;
private: System::ComponentModel::IContainer^ components;
private:
#pragma region Windows Form Designer generated code
void InitializeComponent(void) {
this->TextBox1 = (gcnew System::Windows::Forms::TextBox());
//
// TextBox1
//
this->TextBox1->Location = System::Drawing::Point(220, 62);
this->TextBox1->Margin = System::Windows::Forms::Padding(3, 2, 3, 2);
this->TextBox1->Name = L"TextBox1";
this->TextBox1->Size = System::Drawing::Size(100, 22);
this->TextBox1->TabIndex = 32;
//
// ParForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(727, 678);
this->Controls->Add(this->TextBox1);
this->Margin = System::Windows::Forms::Padding(3, 2, 3, 2);
this->MaximumSize = System::Drawing::Size(727, 678);
this->MinimumSize = System::Drawing::Size(727, 678);
this->Name = L"ParForm";
this->Text = L"Parameters";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
};
}
我想要的是当使用ToolStripMenuItem打开WinForm时,在TextBox-〉Text中显示变量的值。所以我写了一个双精度浮点数this->TextBox1->Text = Convert::ToString(var)
。当我运行程序(调试模式)时,这一切都工作正常。所有的值都正确显示,我的OK和Cancel按钮按预期工作(更改变量值并保存到datafile.dat)。然后运行使用修改后的值的c++可执行文件也工作正常。
然而,有时候当我停止调试(因为一个错误)时,我输入的代码行被删除了。是什么导致了这种行为?我有一种感觉,这与调用时值是如何加载到WinForms中的有关。顺序不对吗?
"我的努力"
我不知道为什么会发生这种情况。我试着在谷歌上搜索它,并检查StackOverflow论坛的帖子,但我没有得到有用的点击。我检查了TextBox类的文档,看看是否有一个属性或方法来“预加载”文本,但我使用的方法应该是我在互联网上找到的。
1条答案
按热度按时间nwnhqdif1#
问题已经解决了,感谢@Jimi和@HansPassant的评论。我在InitializeComponent中添加了代码,我应该在那里使用构造函数区域。不知道它是如何工作的。谢谢大家!
对于其他:在
public: MyForm(void)
中调用InitializeComponent()
后重新初始化控件