Winforms visualc&x2B+;隐藏表单后重新加载表单以查看其数据
我有一个包含多个表单的程序,我需要允许用户在表单之间来回移动。问题是,当我在单击“下一步”后隐藏form1并继续到form2,然后按“上一步”返回到form1时,我键入的所有数据都消失了 我曾想过将所有内容写入文本文件,然后将所有数据读回。但是这是很多额外的代码,我认为这是没有必要的 代码: 表格1: 表格2:Winforms visualc&x2B+;隐藏表单后重新加载表单以查看其数据,winforms,visual-studio,c++-cli,refresh,Winforms,Visual Studio,C++ Cli,Refresh,我有一个包含多个表单的程序,我需要允许用户在表单之间来回移动。问题是,当我在单击“下一步”后隐藏form1并继续到form2,然后按“上一步”返回到form1时,我键入的所有数据都消失了 我曾想过将所有内容写入文本文件,然后将所有数据读回。但是这是很多额外的代码,我认为这是没有必要的 代码: 表格1: 表格2: private: System::Void back_Pg1_Click(System::Object^ sender, System::EventArgs^ e) { th
private: System::Void back_Pg1_Click(System::Object^ sender, System::EventArgs^ e) {
this->Hide();
PreviousForm->Show();
有额外的代码需要使代码来回运行。但这不是问题所在。我只想知道刷新Form1而不是加载新Form1的代码
谢谢你的帮助。我希望我说得够清楚了。我正在使用Visual Studio Community 2015 两种可能的解决方案是: 1) 要直接回答这个问题,一种可能的解决方案是使用ShowDialog()(不在事件处理程序中)而不是Show()
做一些类似于:
namespace formSwitchTest
{
ref class tcFormBase : System::Windows::Forms::Form
{
public:
tcFormBase(
System::String ^ aTitle,
System::Windows::Threading::Dispatcher ^ aMainDispatcher) :
mpcForm(nullptr),
mMainDispatcher(aMainDispatcher)
{
this->SuspendLayout();
this->AutoSize = true;
mpcSwitchButton = gcnew System::Windows::Forms::Button();
mpcSwitchButton->AutoSize = true;
mpcSwitchButton->Click += gcnew System::EventHandler(this, &formSwitchTest::tcFormBase::ButtonClicked);
mpcSwitchButton->Text = "Click to leave ";
mpcSwitchButton->Text += aTitle;
mpcSwitchButton->Dock = System::Windows::Forms::DockStyle::Top;
mpcExitButton = gcnew System::Windows::Forms::Button();
mpcExitButton->AutoSize = true;
mpcExitButton->Click += gcnew System::EventHandler(this, &formSwitchTest::tcFormBase::Exit);
mpcExitButton->Text = "click to exit";
mpcExitButton->Dock = System::Windows::Forms::DockStyle::Bottom;
mpcTextBox = gcnew System::Windows::Forms::TextBox();
mpcTextBox->Width = 200;
mpcTextBox->AutoSize = true;
mpcTextBox->Text = "enter text here";
mpcTextBox->Dock = System::Windows::Forms::DockStyle::Left;
this->Controls->Add(mpcTextBox);
this->Controls->Add(mpcSwitchButton);
this->Controls->Add(mpcExitButton);
this->ResumeLayout();
this->PerformLayout();
}
void SetOtherForm(System::Windows::Forms::Form ^ apcForm)
{
mpcForm = apcForm;
// save this action to perform multiple times later
mShowOtherForm = gcnew System::Action(this, &formSwitchTest::tcFormBase::ShowOtherForm);
}
void ShowDialog()
{
System::Windows::Forms::Form::ShowDialog();
}
private:
void ShowOtherForm()
{
// perform the blocking ShowDialog() because it works better than Show()
mpcForm->ShowDialog();
}
void Exit(System::Object ^ apcSender, System::EventArgs ^ apcArgs)
{
// stop the blocking ShowDialog()
this->Hide();
// tell the dispatcher to stop, which will stop the Run() call
mMainDispatcher->BeginInvokeShutdown(System::Windows::Threading::DispatcherPriority::Normal);
}
void ButtonClicked(System::Object ^ apcSender, System::EventArgs ^ apcArgs)
{
// stop the blocking ShowDialog()
this->Hide();
// BeginInvoke() to prevent blocking
mMainDispatcher->BeginInvoke(mShowOtherForm);
}
System::Windows::Forms::Form ^ mpcForm;
System::Windows::Forms::Button ^ mpcSwitchButton;
System::Windows::Forms::Button ^ mpcExitButton;
System::Windows::Forms::TextBox ^ mpcTextBox;
System::Windows::Threading::Dispatcher ^ mMainDispatcher;
System::Action ^ mShowOtherForm;
};
}
int main(void)
{
System::Windows::Threading::Dispatcher ^ lpcMainDispatcher =
System::Windows::Threading::Dispatcher::CurrentDispatcher;
// the only reason for each form being of the same base is for ease of the test
formSwitchTest::tcFormBase ^ lpcMainForm = gcnew formSwitchTest::tcFormBase(
"main", lpcMainDispatcher);
formSwitchTest::tcFormBase ^ lpcSecondForm = gcnew formSwitchTest::tcFormBase(
"second", lpcMainDispatcher);
// exchange pointers to each other
lpcMainForm->SetOtherForm(lpcSecondForm);
lpcSecondForm->SetOtherForm(lpcMainForm);
// begin invoke to put the start action in the queue
lpcMainDispatcher->BeginInvoke(gcnew System::Action(lpcMainForm, &formSwitchTest::tcFormBase::ShowDialog));
// process the dispatcher queue and wait for it to be shut down
System::Windows::Threading::Dispatcher::Run();
}
2) 使用UserControl类而不是表单可能会更好,更接近您想要的内容namespace formSwitchTest
{
ref class tcCtrlBase : System::Windows::Forms::UserControl
{
public:
tcCtrlBase(
System::String ^ aTitle,
System::Windows::Forms::Form ^ apcContainerForm) :
mpcCtrl(nullptr),
mpcContainerForm(apcContainerForm)
{
this->SuspendLayout();
this->AutoSize = true;
mpcSwitchButton = gcnew System::Windows::Forms::Button();
mpcSwitchButton->AutoSize = true;
mpcSwitchButton->Click += gcnew System::EventHandler(this, &formSwitchTest::tcCtrlBase::ButtonClicked);
mpcSwitchButton->Text = "Click to leave ";
mpcSwitchButton->Text += aTitle;
mpcSwitchButton->Dock = System::Windows::Forms::DockStyle::Top;
mpcExitButton = gcnew System::Windows::Forms::Button();
mpcExitButton->AutoSize = true;
mpcExitButton->Click += gcnew System::EventHandler(this, &formSwitchTest::tcCtrlBase::Exit);
mpcExitButton->Text = "click to exit";
mpcExitButton->Dock = System::Windows::Forms::DockStyle::Bottom;
mpcTextBox = gcnew System::Windows::Forms::TextBox();
mpcTextBox->Width = 200;
mpcTextBox->AutoSize = true;
mpcTextBox->Text = "enter text here";
mpcTextBox->Dock = System::Windows::Forms::DockStyle::Left;
this->Controls->Add(mpcTextBox);
this->Controls->Add(mpcSwitchButton);
this->Controls->Add(mpcExitButton);
this->ResumeLayout();
this->PerformLayout();
}
void SetOtherCtrl(System::Windows::Forms::UserControl ^ apcCtrl)
{
mpcCtrl = apcCtrl;
}
private:
void ShowOtherForm()
{
mpcCtrl->Show();
}
void Exit(System::Object ^ apcSender, System::EventArgs ^ apcArgs)
{
mpcContainerForm->Hide();
}
void ButtonClicked(System::Object ^ apcSender, System::EventArgs ^ apcArgs)
{
this->Hide();
mpcCtrl->Show();
}
System::Windows::Forms::UserControl ^ mpcCtrl;
System::Windows::Forms::Button ^ mpcSwitchButton;
System::Windows::Forms::Button ^ mpcExitButton;
System::Windows::Forms::TextBox ^ mpcTextBox;
System::Windows::Forms::Form ^ mpcContainerForm;
};
}
int main(void)
{
System::Windows::Forms::Form ^ lpcContainerForm = gcnew System::Windows::Forms::Form();
formSwitchTest::tcCtrlBase ^ lpcMainForm = gcnew formSwitchTest::tcCtrlBase(
"main", lpcContainerForm);
formSwitchTest::tcCtrlBase ^ lpcSecondForm = gcnew formSwitchTest::tcCtrlBase(
"second", lpcContainerForm);
lpcMainForm->SetOtherCtrl(lpcSecondForm);
lpcSecondForm->SetOtherCtrl(lpcMainForm);
lpcContainerForm->AutoSize = true;
lpcContainerForm->Controls->Add(lpcMainForm);
lpcContainerForm->Controls->Add(lpcSecondForm);
lpcContainerForm->ShowDialog();
}
两种可能的解决办法是: 1) 要直接回答这个问题,一种可能的解决方案是使用ShowDialog()(不在事件处理程序中)而不是Show()
做一些类似于:
namespace formSwitchTest
{
ref class tcFormBase : System::Windows::Forms::Form
{
public:
tcFormBase(
System::String ^ aTitle,
System::Windows::Threading::Dispatcher ^ aMainDispatcher) :
mpcForm(nullptr),
mMainDispatcher(aMainDispatcher)
{
this->SuspendLayout();
this->AutoSize = true;
mpcSwitchButton = gcnew System::Windows::Forms::Button();
mpcSwitchButton->AutoSize = true;
mpcSwitchButton->Click += gcnew System::EventHandler(this, &formSwitchTest::tcFormBase::ButtonClicked);
mpcSwitchButton->Text = "Click to leave ";
mpcSwitchButton->Text += aTitle;
mpcSwitchButton->Dock = System::Windows::Forms::DockStyle::Top;
mpcExitButton = gcnew System::Windows::Forms::Button();
mpcExitButton->AutoSize = true;
mpcExitButton->Click += gcnew System::EventHandler(this, &formSwitchTest::tcFormBase::Exit);
mpcExitButton->Text = "click to exit";
mpcExitButton->Dock = System::Windows::Forms::DockStyle::Bottom;
mpcTextBox = gcnew System::Windows::Forms::TextBox();
mpcTextBox->Width = 200;
mpcTextBox->AutoSize = true;
mpcTextBox->Text = "enter text here";
mpcTextBox->Dock = System::Windows::Forms::DockStyle::Left;
this->Controls->Add(mpcTextBox);
this->Controls->Add(mpcSwitchButton);
this->Controls->Add(mpcExitButton);
this->ResumeLayout();
this->PerformLayout();
}
void SetOtherForm(System::Windows::Forms::Form ^ apcForm)
{
mpcForm = apcForm;
// save this action to perform multiple times later
mShowOtherForm = gcnew System::Action(this, &formSwitchTest::tcFormBase::ShowOtherForm);
}
void ShowDialog()
{
System::Windows::Forms::Form::ShowDialog();
}
private:
void ShowOtherForm()
{
// perform the blocking ShowDialog() because it works better than Show()
mpcForm->ShowDialog();
}
void Exit(System::Object ^ apcSender, System::EventArgs ^ apcArgs)
{
// stop the blocking ShowDialog()
this->Hide();
// tell the dispatcher to stop, which will stop the Run() call
mMainDispatcher->BeginInvokeShutdown(System::Windows::Threading::DispatcherPriority::Normal);
}
void ButtonClicked(System::Object ^ apcSender, System::EventArgs ^ apcArgs)
{
// stop the blocking ShowDialog()
this->Hide();
// BeginInvoke() to prevent blocking
mMainDispatcher->BeginInvoke(mShowOtherForm);
}
System::Windows::Forms::Form ^ mpcForm;
System::Windows::Forms::Button ^ mpcSwitchButton;
System::Windows::Forms::Button ^ mpcExitButton;
System::Windows::Forms::TextBox ^ mpcTextBox;
System::Windows::Threading::Dispatcher ^ mMainDispatcher;
System::Action ^ mShowOtherForm;
};
}
int main(void)
{
System::Windows::Threading::Dispatcher ^ lpcMainDispatcher =
System::Windows::Threading::Dispatcher::CurrentDispatcher;
// the only reason for each form being of the same base is for ease of the test
formSwitchTest::tcFormBase ^ lpcMainForm = gcnew formSwitchTest::tcFormBase(
"main", lpcMainDispatcher);
formSwitchTest::tcFormBase ^ lpcSecondForm = gcnew formSwitchTest::tcFormBase(
"second", lpcMainDispatcher);
// exchange pointers to each other
lpcMainForm->SetOtherForm(lpcSecondForm);
lpcSecondForm->SetOtherForm(lpcMainForm);
// begin invoke to put the start action in the queue
lpcMainDispatcher->BeginInvoke(gcnew System::Action(lpcMainForm, &formSwitchTest::tcFormBase::ShowDialog));
// process the dispatcher queue and wait for it to be shut down
System::Windows::Threading::Dispatcher::Run();
}
2) 使用UserControl类而不是表单可能会更好,更接近您想要的内容namespace formSwitchTest
{
ref class tcCtrlBase : System::Windows::Forms::UserControl
{
public:
tcCtrlBase(
System::String ^ aTitle,
System::Windows::Forms::Form ^ apcContainerForm) :
mpcCtrl(nullptr),
mpcContainerForm(apcContainerForm)
{
this->SuspendLayout();
this->AutoSize = true;
mpcSwitchButton = gcnew System::Windows::Forms::Button();
mpcSwitchButton->AutoSize = true;
mpcSwitchButton->Click += gcnew System::EventHandler(this, &formSwitchTest::tcCtrlBase::ButtonClicked);
mpcSwitchButton->Text = "Click to leave ";
mpcSwitchButton->Text += aTitle;
mpcSwitchButton->Dock = System::Windows::Forms::DockStyle::Top;
mpcExitButton = gcnew System::Windows::Forms::Button();
mpcExitButton->AutoSize = true;
mpcExitButton->Click += gcnew System::EventHandler(this, &formSwitchTest::tcCtrlBase::Exit);
mpcExitButton->Text = "click to exit";
mpcExitButton->Dock = System::Windows::Forms::DockStyle::Bottom;
mpcTextBox = gcnew System::Windows::Forms::TextBox();
mpcTextBox->Width = 200;
mpcTextBox->AutoSize = true;
mpcTextBox->Text = "enter text here";
mpcTextBox->Dock = System::Windows::Forms::DockStyle::Left;
this->Controls->Add(mpcTextBox);
this->Controls->Add(mpcSwitchButton);
this->Controls->Add(mpcExitButton);
this->ResumeLayout();
this->PerformLayout();
}
void SetOtherCtrl(System::Windows::Forms::UserControl ^ apcCtrl)
{
mpcCtrl = apcCtrl;
}
private:
void ShowOtherForm()
{
mpcCtrl->Show();
}
void Exit(System::Object ^ apcSender, System::EventArgs ^ apcArgs)
{
mpcContainerForm->Hide();
}
void ButtonClicked(System::Object ^ apcSender, System::EventArgs ^ apcArgs)
{
this->Hide();
mpcCtrl->Show();
}
System::Windows::Forms::UserControl ^ mpcCtrl;
System::Windows::Forms::Button ^ mpcSwitchButton;
System::Windows::Forms::Button ^ mpcExitButton;
System::Windows::Forms::TextBox ^ mpcTextBox;
System::Windows::Forms::Form ^ mpcContainerForm;
};
}
int main(void)
{
System::Windows::Forms::Form ^ lpcContainerForm = gcnew System::Windows::Forms::Form();
formSwitchTest::tcCtrlBase ^ lpcMainForm = gcnew formSwitchTest::tcCtrlBase(
"main", lpcContainerForm);
formSwitchTest::tcCtrlBase ^ lpcSecondForm = gcnew formSwitchTest::tcCtrlBase(
"second", lpcContainerForm);
lpcMainForm->SetOtherCtrl(lpcSecondForm);
lpcSecondForm->SetOtherCtrl(lpcMainForm);
lpcContainerForm->AutoSize = true;
lpcContainerForm->Controls->Add(lpcMainForm);
lpcContainerForm->Controls->Add(lpcSecondForm);
lpcContainerForm->ShowDialog();
}