[英]Save and Open file Dialogs
I AM USING C++. 我正在使用C ++。 I am currently designing a program with windows forms.
我目前正在设计一个带有Windows窗体的程序。 I am having a problem writing event handlers.
我在编写事件处理程序时遇到问题。 especially the click event handler for save and open file.
特别是用于保存和打开文件的click事件处理程序。 I have searched the net and can find no decent explanation on how to write these event handlers.
我在网上搜索,找不到关于如何编写这些事件处理程序的正当解释。 So i am asking for a well defined definition.
因此,我要求一个定义明确的定义。
Please do not send me to Microsoft as they do not provide a sample of completed event handlers only partially. 请不要将我发送给Microsoft,因为他们不会仅部分提供完整的事件处理程序示例。
Since it's not obvious at a glance that Visual Studio 2012 does support C++/CLI Winform applications, even though the OP certainly knows this, for other readers, here's how I created one just to answer this question: 由于一眼就看不到Visual Studio 2012是否支持C ++ / CLI Winform应用程序,即使OP确实知道这一点,对于其他读者来说,我还是通过以下方式创建了该应用程序来回答此问题:
In Visual Studio 2012, in the New Project dialog I selected [Visual C++ > CLR > CLR Empty Project]. 在Visual Studio 2012中,在“新建项目”对话框中,选择了[Visual C ++> CLR> CLR空项目]。
In the new project I added an ordinary C++ main
function and a Windows Form. 在新项目中,我添加了一个普通的C ++
main
函数和一个Windows窗体。
In the linker settings I changed it from console subsystem to GUI subsystem (and due to the non-standard behavior of Microsoft's linker, adjusted the entry point to mainCRTStartup
). 在链接器设置中,我将其从控制台子系统更改为GUI子系统(并且由于Microsoft链接器的非标准行为,因此将入口点调整为
mainCRTStartup
)。
That said, below is shown the generated Winform header, with just the event handler code, which fires up an Open File dialog, added manually. 就是说,下面显示了生成的Winform标头,其中仅包含事件处理程序代码,它会触发手动打开的“打开文件”对话框。
The long comment line ------
shows how to connect up an event handler. 长长的注释行
------
显示了如何连接事件处理程序。
The code at the bottom shows both how to code up an event handler, and how to run the open file dialog. 底部的代码显示了如何编写事件处理程序以及如何运行打开文件对话框。
#pragma once
namespace CppWindowsFormsApplication {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for MainWindow
/// </summary>
public ref class MainWindow : public System::Windows::Forms::Form
{
public:
MainWindow(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MainWindow()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(149, 208);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"&Open file";
this->button1->UseVisualStyleBackColor = true;
// ---------------------------------------------------- !Adds event handler -------------------
this->button1->Click += gcnew System::EventHandler(this, &MainWindow::button1_Click);
//
// MainWindow
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->button1);
this->Name = L"MainWindow";
this->Text = L"MainWindow";
this->ResumeLayout(false);
}
#pragma endregion
private:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
OpenFileDialog^ sfd = gcnew OpenFileDialog();
sfd->Filter = "Text Files|*.txt|All Files|*.*";
if( sfd->ShowDialog() != System::Windows::Forms::DialogResult::OK )
{
return;
}
//MessageBox::Show( sfd->FileName );
MessageBox::Show( "OK" );
}
};
}
I didn't fix the indenting etc., since I didn't have the time; 我没有修复缩进等,因为我没有时间。 nearly all of this code was generated by Visual Studio 2012's RAD ( Rabid Application Development ) feature.
几乎所有这些代码都是由Visual Studio 2012的RAD( 狂暴应用程序开发 )功能生成的。
Related to this question's history, now removed: 与此问题的历史相关,现已删除:
I didn't really have the time to answer this question. 我真的没有时间回答这个问题。 I would much rather have let someone else answer it (and reap some reputation from it).
我宁愿让其他人回答它(并从中获得一些声誉)。 It is such a simple question that I'm sure if it wasn't closed so fast, any number of readers, with more time than I had, would have answered it.
这个问题很简单,我可以确定它的关闭速度是否如此之快,因此,任何读者,比我有更多的时间,都会回答这个问题。
But answering it, with code posted elsewhere, and forsaking some other matters, was the only way I saw to have it reopened. 但是回答它,在其他地方发布代码,放弃其他事情,是我看到重新打开它的唯一方法。
I sincerely ask every reader to please don't vote to close what you don't understand . 我真诚地要求每个读者不要投票来关闭你不理解的东西 。
Only vote to close a question when you thoroughly understand the subject matter and can prove to yourself and others that the community is better off with the question closed. 仅当您完全理解主题并可以向自己和他人证明关闭问题后社区会变得更好时,才投票关闭问题。
Failure to understand a question does not necessarily mean that the question is ungood. 不理解问题并不一定意味着问题是解精。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.