Using OpenFileDialog with windows form not opening - winforms

I am making a GUI with a windows form and OpenFileDialog is not opening on button click.
I am using the following code and nothing happens when I click on my button designated as button1.
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
Stream^ myStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->InitialDirectory = "c:\\";
openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 1;
openFileDialog1->RestoreDirectory = true;
if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
if ((myStream = openFileDialog1->OpenFile()) != nullptr)
{
String^ strfilename = openFileDialog1->FileName;
String^ Readfile = File::ReadAllText(strfilename);
MessageBox::Show(Readfile); // This is a test line
//myStream->Close();
}
}
}

Related

How do I code a Save button in WPF to save CSV Files after importing them in my application and editing it?

Im using this Import Methode, and after editing the File in my application I want to have a button to save the File as a "new File" so the source doesnt get changed only a edited duplication is created.
//import button
private void btn_Import_Click(object sender, RoutedEventArgs e)
{
//delete the filename from the textbox so they dont overlap
tbx_FileName.Clear();
//openFileDialog for file Import
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.InitialDirectory = #"C:\";
var result = dlg.ShowDialog();
tbx_FileName.Text = dlg.FileName;
DataContext = CSVTable.ReadFile(dlg.FileName);
}
//save button
private void btn_Save_Click(object sender, RoutedEventArgs e)
{
//saveFileDialog for file Save
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.InitialDirectory = #"C:\";
dlg.Filter = "CSV file (*.csv)|*.csv|All Files (*.*)|*.*";
var result = dlg.ShowDialog();
if (result.HasValue && result.Value)
{
//Save the file, assuming the DataContext is plain text (i.e. string)
File.WriteAllText(dlg.FileName, DataContext);
}
}

Inserting Excel File in provided folder

So basically I am creating a application to where I want to insert an excel sheet inside of the folder path provided.
Here is my Code:
private void BSave_Click(object sender, EventArgs e)
{
FolderBrowserDialog ofd = new FolderBrowserDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
TextOutputFile.Text = ofd.SelectedPath;
}
}

C++ - save image from form to file

I'm new in programming Windows Form Application. I want to capture my Windows form (with the function CaptureScreen()) and save it to a file by clicking on a button using the SaveFileDialog. Here my code:
private: System::Void CaptureScreen()
{
Drawing::Graphics^ myGraphics = this->CreateGraphics();
memoryImage = gcnew Drawing::Bitmap(Size.Width, Size.Height, myGraphics);
Drawing::Graphics^ memoryGraphics = Drawing::Graphics::FromImage(memoryImage);
memoryGraphics->CopyFromScreen(this->Location.X, this->Location.Y, 0, 0, this->Size);
}
private: System::Void btnSave_Click(System::Object^ sender, System::EventArgs^ e)
{
SaveFileDialog^ saveDiag2 = gcnew SaveFileDialog();
saveDiag2->Filter = "Dateityp PNG (*.PNG)|*.PNG|All files (*.*)|*.*";
saveDiag2->FilterIndex = 1;
saveDiag2->RestoreDirectory = true;
if (saveDiag2->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
String^ savePath = saveDiag2->FileName;
if (saveDiag2->OpenFile() != nullptr)
{
try
{
CaptureScreen();
if (memoryImage != nullptr)
{
memoryImage->Save(savePath, Imaging::ImageFormat::Png);
}
}
catch (Exception^)
{
MessageBox::Show("There was a problem saving the file."
"Check the file permissions.");
}
}
}
}
I always get the Exception displayed. What am I doing wrong?
Hans Passant is right. You should dispose IDisposible objects. In C++ CLI you can do it with help of msclr::auto_handle smart pointer.
#include <msclr/auto_handle.h>
private: System::Void CaptureScreen()
{
msclr::auto_handle<Drawing::Graphics> myGraphics(this->CreateGraphics());
memoryImage = gcnew Drawing::Bitmap(Size.Width, Size.Height, myGraphics.get());
msclr::auto_handle<Drawing::Graphics> memoryGraphics(Drawing::Graphics::FromImage(memoryImage));
memoryGraphics->CopyFromScreen(this->Location.X, this->Location.Y, 0, 0, this->Size);
}
Also you can retrieve usefull information from Exception object:
catch (Exception^ exception)
{
MessageBox::Show("There was a problem saving the file: " + exception->ToString(),
"Check the file permissions.");
}

Opening media file with button press

I have one button on my wpf called 'Open Media' with click event and a MediaElement to play media.
I want to open a media file by pressing that button.
It's giving me run time exception 'System.NotSupportedException' on line 'mePlayer.Play()'. How to achieve this using following code:
private void OpenMediaButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openMedia = new OpenFileDialog();
openMedia.Title = "Open Media";
openMedia.Filter = "mp4 files(*.mp4)|*.mp4";
openMedia.InitialDirectory = #"C:\";
openMedia.RestoreDirectory = true;
if (openMedia.ShowDialog() == true)
{
mePlayer.Source = new Uri(openMedia.FileName);
mePlayer.Position = TimeSpan.FromSeconds(1);
mePlayer.Play();
}
}

Cannot delete file because it is used by another process

I'm getting this exception
The process cannot access the file 'myfile.zip' because it is being used by another process.
When I try to delete a file. I understand the error, but I'm not sure what other process could be using the file.
I'm downloading the file via WebClient asynchronously, but I cancel the download before trying to delete it, which means that process should relinquish it, no?
Here are the relevant methods. It's a simple file-downloader:
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
string downloadFile = textBox1.Text.Trim();
if (e.Key == Key.Return && downloadFile != "")
{
var dlg = new SaveFileDialog();
dlg.FileName = Path.GetFileName(downloadFile);
dlg.DefaultExt = Path.GetExtension(downloadFile);
var result = dlg.ShowDialog();
if(result.Value)
{
textBox1.Text = "";
textBox1.Focus();
_saveFile = dlg.FileName;
progressBar1.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => progressBar1.Foreground = new SolidColorBrush(Color.FromRgb(0, 255, 0))));
_webClient.DownloadFileAsync(new Uri(downloadFile), _saveFile);
}
}
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (_webClient.IsBusy && _saveFile != null)
{
var result = MessageBox.Show("Download in progress. Are you sure you want to exit?", "Exit?", MessageBoxButton.YesNo, MessageBoxImage.Warning);
if (result == MessageBoxResult.Yes)
{
_webClient.CancelAsync();
File.Delete(_saveFile);
}
else
{
e.Cancel = true;
}
}
}
You need to wait when downloading realy canceled. When call _webClient.CancelAsync(); next operator executes immediatley before webClient canceled.
May be you need delete the file in callback of CancelAsync(...)

Resources