kirya_shkolnik

OpenFileDialog

May 28th, 2021 (edited)
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.47 KB | None | 0 0
  1. void FileProc(StreamReader^ sr, StreamWriter^ sw, ListBox^ lstInFile, ListBox^ lstOutFile, TextBox^ mincountTB) {
  2.     String^ InBuffer = "";
  3.     String^ OutBuffer = "";
  4.     int count = 0;                          // Количество изменений в строке
  5.     int mincount = 1000000;                 // Минимальное количество изменений
  6.     String^ mincountString = "";            // Строка с минимальным количеством изменений
  7.     try {
  8.         while (InBuffer = sr->ReadLine()) {
  9.             if (InBuffer->Length == 0) continue;
  10.             lstInFile->Items->Add(InBuffer);
  11.             count = 0;                          // Количество изменений в строке
  12.             OutBuffer = task(InBuffer, count);
  13.             sw->WriteLine(OutBuffer);
  14.             lstOutFile->Items->Add(OutBuffer);
  15.             if (count < mincount && count > 0) { // Поиск строки с минимальным количеством изменений
  16.                 mincount = count;
  17.                 mincountString = OutBuffer;
  18.             }
  19.         }
  20.         if (mincountString == "") mincountString = "Изменений не было.";
  21.         mincountTB->Text = mincountString;
  22.     }
  23.     catch (Exception ^ ex) {
  24.         MessageBox::Show(ex->Message, "Ошибка", MessageBoxButtons::OK, MessageBoxIcon::Error);
  25.     }
  26. }
  27.  
  28.     private: System::Void Button_Start_Click(System::Object^ sender, System::EventArgs^ e) {
  29.         listBox_input->Items->Clear();
  30.         listBox_output->Items->Clear();
  31.         String^ InFile = textBox_input->Text;
  32.         String^ OutFile = textBox_output->Text;
  33.         StreamReader^ sr = nullptr;
  34.         StreamWriter^ sw = nullptr;
  35.         try {
  36.             sr = gcnew StreamReader(InFile);
  37.             sw = gcnew StreamWriter(OutFile, false);
  38.         }
  39.         catch (Exception ^ ex) {
  40.             MessageBox::Show(ex->Message, "Ошибка", MessageBoxButtons::OK, MessageBoxIcon::Error);
  41.             if (sr) sr->Close();
  42.             if (sw) sw->Close();
  43.             return;
  44.         }
  45.         FileProc(sr, sw, listBox_input, listBox_output, textBox_mincount);
  46.         sr->Close();
  47.         sw->Close();
  48.  
  49.     }
  50.  
  51. private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
  52.     listBox_input->Items->Clear();
  53.     listBox_output->Items->Clear();
  54.     textBox_input->Clear();
  55.     textBox_output->Clear();
  56.     button_Start->Enabled = false;
  57. }
  58. private: System::Void Button_openInput_Click(System::Object^ sender, System::EventArgs^ e) {
  59.     OpenFileDialog^ openFileDialog = gcnew OpenFileDialog;
  60.     openFileDialog->Title = "Открытие входногого файла";
  61.     openFileDialog->InitialDirectory = "C:\\";
  62.     openFileDialog->Filter = "Text files (*.txt)|*.txt";
  63.     openFileDialog->FilterIndex = 1;
  64.     openFileDialog->ShowReadOnly = true;
  65.     openFileDialog->ReadOnlyChecked = true;
  66.     openFileDialog->RestoreDirectory = true;
  67.     if (openFileDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK) textBox_input->Text = openFileDialog->FileName;
  68.     else  textBox_input->Text = "";
  69.     button_Start->Enabled = textBox_input->Text->Length > 0 && textBox_output->Text->Length > 0;
  70. }
  71.  
  72. private: System::Void Button_openOutput_Click(System::Object^ sender, System::EventArgs^ e) {
  73.     SaveFileDialog^ saveFileDialog = gcnew SaveFileDialog;
  74.     saveFileDialog->Title = "Открытие выходногого файла";
  75.     saveFileDialog->Filter = "Text files (*.txt)|*.txt";
  76.     saveFileDialog->FilterIndex = 1;
  77.     saveFileDialog->OverwritePrompt = true;
  78.     if (saveFileDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK) textBox_output->Text = saveFileDialog->FileName;
  79.     else  textBox_input->Text = "";
  80.     button_Start->Enabled = textBox_input->Text->Length > 0 && textBox_output->Text->Length > 0;
  81. }
Add Comment
Please, Sign In to add comment