I have a method inside a class that will receive a PictureBox and a String, so the user can select the image for the PictureBox and at the same time for the program to know what file plus extension of the chosen file for later use.
The string it will be set, for example as 1.png but on the where I call this method the string it will be as "" rly don't understand why this is happening.
On the GerirDoc.cs
I define the string as String _imgFile = ""
at the beginning of the Form and have this code:
DocImg docImg = new DocImg();
docImg.selectImage(_imgFile, this.pictureBoxDoc);
The class DocImg
class DocImg
{
public int Hwnd { get; private set; }
public void selectImage(String imgFile, PictureBox imgBox)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "Escolher imagem";
openFileDialog.Filter = "Image files (*.jpg, *.jpeg, *.png) | *.jpg; *.jpeg; *.png";
openFileDialog.Multiselect = false;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
imgFile = openFileDialog.SafeFileName;
DialogResult dialogResult = MessageBox.Show("Deseja passar a imagem para o aparelho se tiver ligado ao computador?", "Informação", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
imgBox.Image = Image.FromFile(openFileDialog.FileName);
switch (dialogResult)
{
case DialogResult.Yes:
Shell shell = new Shell();
Folder folder = shell.BrowseForFolder((int)Hwnd, "Selecione o caminho para a pasta \"Imagens\"", 0, 0);
if (folder != null)
{
FolderItem _destinationDir = (folder as Folder3).Self;
if (String.Equals(_destinationDir.Name, "Imagens"))
{
try
{
folder.CopyHere(openFileDialog.FileName, null);
MessageBox.Show("Imagem guardada com sucesso", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
else
{
MessageBox.Show("A pasta de destino tem que ser a pasta \"Imagens\" que se está dentro de adbRetail");
}
}
break;
case DialogResult.No:
MessageBox.Show("De lembrar que a imagem só ira aparecer correctamente se tiver na pasta correcta do aparelho", "Informação", MessageBoxButtons.OK, MessageBoxIcon.Information);
break;
}
}
}
}
I did put a breakpoint just after imgFile = openFileDialog.SafeFileName;
and the variable imgFile as the value of the filename, ex 1.png, but on GerirDoc.cs after choosing the file of the image, _imgFile doesn't have any value.
On GerirDoc.cs I only put _imgFile = "" when the form is created. Why is this happening ? Since I send the string inside it and since in DocImg if the user chose a image it should have the filename, ex 1.png and not be empty