- Joined
- Jul 26, 2006
- Messages
- 3,634
- Reaction score
- 1,007
I've been struggling with this for a few hours now... Absolutely starting to hate C++ for its strictness with variable types in comparison to PHP ._.
I basically have this simple form where the user selects a folder and I want to replace all spaces in the filenames of the files in that folder to underscores. Sounds easy but I can not work it out. Selecting the folder goes perfectly fine, but in button2_Click I need to do the renaming and every single code snippet on earth uses char* or another Ducking type. I believe I get a string from the folder selection dialog but nothing works with that. Why the duck does it have to be all specific?!
Using Visual Studio 2010 Express, here's my code (Form1.h)
I basically have this simple form where the user selects a folder and I want to replace all spaces in the filenames of the files in that folder to underscores. Sounds easy but I can not work it out. Selecting the folder goes perfectly fine, but in button2_Click I need to do the renaming and every single code snippet on earth uses char* or another Ducking type. I believe I get a string from the folder selection dialog but nothing works with that. Why the duck does it have to be all specific?!
Using Visual Studio 2010 Express, here's my code (Form1.h)
Code:
#pragma once
namespace SpacesToUnderscores2 {
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 Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::FolderBrowserDialog^ folderBrowserDialog1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
bool folderSelected;
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->folderBrowserDialog1 = (gcnew System::Windows::Forms::FolderBrowserDialog());
this->button1 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// folderBrowserDialog1
//
this->folderBrowserDialog1->Description = L"Select the directory";
this->folderBrowserDialog1->ShowNewFolderButton = false;
//
// button1
//
this->button1->Location = System::Drawing::Point(15, 54);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(89, 40);
this->button1->TabIndex = 0;
this->button1->Text = L"Select folder";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(12, 9);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(81, 13);
this->label1->TabIndex = 1;
this->label1->Text = L"Selected folder:";
//
// label2
//
this->label2->AutoSize = true;
this->label2->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D;
this->label2->Location = System::Drawing::Point(15, 22);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(29, 15);
this->label2->TabIndex = 2;
this->label2->Text = L"N/A";
//
// button2
//
this->button2->Location = System::Drawing::Point(110, 54);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(89, 40);
this->button2->TabIndex = 3;
this->button2->Text = L"Spaces to underscores!";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// button3
//
this->button3->Location = System::Drawing::Point(219, 62);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(22, 25);
this->button3->TabIndex = 4;
this->button3->Text = L"\?";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(253, 106);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"SpacesToUnderscores";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
// Bring up a dialog to open a file.
void button1_Click(Object^ /*sender*/, System::EventArgs^ /*e*/)
{
System::Windows::Forms::DialogResult result = folderBrowserDialog1->ShowDialog();
if(result == System::Windows::Forms::DialogResult::OK)
{
this->label2->Text = folderBrowserDialog1->SelectedPath;
folderSelected = true;
}
}
void button2_Click(Object^ /*sender*/, System::EventArgs^ /*e*/)
{
if(!folderSelected)
{
System::Windows::Forms::MessageBox::Show(this, "Select a folder first!", "Error", MessageBoxButtons::OK);
}
else
{
}
}
void button3_Click(Object^ /*sender*/, System::EventArgs^ /*e*/)
{
System::Windows::Forms::MessageBox::Show(this, "This application will replace the spaces in filenames of files in the selected folder with underscores.\n\n(c) Floris den Hartog, 2012", "Info", MessageBoxButtons::OK);
}
};
}