我有一个包含多个表单的程序,我需要允许用户在表单之间来回切换。问题是当我在点击“下一步”后隐藏form1并继续执行form2然后按“返回”返回到form1时,我输入的所有数据都消失了。
我曾考虑将所有内容写入文本文件,然后重新读取所有数据。但这是很多额外的代码,我认为这是不必要的。
代码: Form1中:
public: System::Void to_page_2_Click(System::Object^ sender, System::EventArgs^ e) {
Page2 ^ p2 = gcnew Page2(this);
p2->Show();
this->Hide();
}
窗体2:
private: System::Void back_Pg1_Click(System::Object^ sender, System::EventArgs^ e) {
this->Hide();
PreviousForm->Show();
需要额外的代码才能使代码能够来回运行。但那不是问题。我只想知道刷新Form1而不是加载新Form1的代码。
感谢您的帮助。我希望我很清楚。我正在使用Visual Studio Community 2015.
答案 0 :(得分:0)
两种可能的解决方案是:
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();
}